logging.h revision a6e1c126299586932ecd3c1133a55a6f8e1107fc
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_BASE_LOGGING_H_
18#define ART_RUNTIME_BASE_LOGGING_H_
19
20#include <ostream>
21
22#include "base/macros.h"
23
24namespace art {
25
26enum LogSeverity {
27  VERBOSE,
28  DEBUG,
29  INFO,
30  WARNING,
31  ERROR,
32  FATAL,
33  INTERNAL_FATAL,  // For Runtime::Abort.
34};
35
36// The members of this struct are the valid arguments to VLOG and VLOG_IS_ON in code,
37// and the "-verbose:" command line argument.
38struct LogVerbosity {
39  bool class_linker;  // Enabled with "-verbose:class".
40  bool collector;
41  bool compiler;
42  bool deopt;
43  bool gc;
44  bool heap;
45  bool jdwp;
46  bool jit;
47  bool jni;
48  bool monitor;
49  bool oat;
50  bool profiler;
51  bool signals;
52  bool simulator;
53  bool startup;
54  bool third_party_jni;  // Enabled with "-verbose:third-party-jni".
55  bool threads;
56  bool verifier;
57  bool image;
58};
59
60// Global log verbosity setting, initialized by InitLogging.
61extern LogVerbosity gLogVerbosity;
62
63// 0 if not abort, non-zero if an abort is in progress. Used on fatal exit to prevents recursive
64// aborts. Global declaration allows us to disable some error checking to ensure fatal shutdown
65// makes forward progress.
66extern unsigned int gAborting;
67
68// Configure logging based on ANDROID_LOG_TAGS environment variable.
69// We need to parse a string that looks like
70//
71//      *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
72//
73// The tag (or '*' for the global level) comes first, followed by a colon
74// and a letter indicating the minimum priority level we're expected to log.
75// This can be used to reveal or conceal logs with specific tags.
76extern void InitLogging(char* argv[]);
77
78// Returns the command line used to invoke the current tool or null if InitLogging hasn't been
79// performed.
80extern const char* GetCmdLine();
81
82// The command used to start the ART runtime, such as "/system/bin/dalvikvm". If InitLogging hasn't
83// been performed then just returns "art"
84extern const char* ProgramInvocationName();
85
86// A short version of the command used to start the ART runtime, such as "dalvikvm". If InitLogging
87// hasn't been performed then just returns "art"
88extern const char* ProgramInvocationShortName();
89
90// Logs a message to logcat on Android otherwise to stderr. If the severity is FATAL it also causes
91// an abort. For example: LOG(FATAL) << "We didn't expect to reach here";
92#define LOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, -1).stream()
93
94// A variant of LOG that also logs the current errno value. To be used when library calls fail.
95#define PLOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, errno).stream()
96
97// Marker that code is yet to be implemented.
98#define UNIMPLEMENTED(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
99
100// Is verbose logging enabled for the given module? Where the module is defined in LogVerbosity.
101#define VLOG_IS_ON(module) UNLIKELY(::art::gLogVerbosity.module)
102
103// Variant of LOG that logs when verbose logging is enabled for a module. For example,
104// VLOG(jni) << "A JNI operation was performed";
105#define VLOG(module) \
106  if (VLOG_IS_ON(module)) \
107    ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream()
108
109// Return the stream associated with logging for the given module.
110#define VLOG_STREAM(module) ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream()
111
112// Check whether condition x holds and LOG(FATAL) if not. The value of the expression x is only
113// evaluated once. Extra logging can be appended using << after. For example,
114// CHECK(false == true) results in a log message of "Check failed: false == true".
115#define CHECK(x) \
116  if (UNLIKELY(!(x))) \
117    ::art::LogMessage(__FILE__, __LINE__, ::art::FATAL, -1).stream() \
118        << "Check failed: " #x << " "
119
120// Helper for CHECK_xx(x,y) macros.
121#define CHECK_OP(LHS, RHS, OP) \
122  for (auto _values = ::art::MakeEagerEvaluator(LHS, RHS); \
123       UNLIKELY(!(_values.lhs OP _values.rhs)); /* empty */) \
124    ::art::LogMessage(__FILE__, __LINE__, ::art::FATAL, -1).stream() \
125        << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
126        << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
127
128
129// Check whether a condition holds between x and y, LOG(FATAL) if not. The value of the expressions
130// x and y is evaluated once. Extra logging can be appended using << after. For example,
131// CHECK_NE(0 == 1, false) results in "Check failed: false != false (0==1=false, false=false) ".
132#define CHECK_EQ(x, y) CHECK_OP(x, y, ==)
133#define CHECK_NE(x, y) CHECK_OP(x, y, !=)
134#define CHECK_LE(x, y) CHECK_OP(x, y, <=)
135#define CHECK_LT(x, y) CHECK_OP(x, y, <)
136#define CHECK_GE(x, y) CHECK_OP(x, y, >=)
137#define CHECK_GT(x, y) CHECK_OP(x, y, >)
138
139// Helper for CHECK_STRxx(s1,s2) macros.
140#define CHECK_STROP(s1, s2, sense) \
141  if (UNLIKELY((strcmp(s1, s2) == 0) != sense)) \
142    LOG(::art::FATAL) << "Check failed: " \
143        << "\"" << s1 << "\"" \
144        << (sense ? " == " : " != ") \
145        << "\"" << s2 << "\""
146
147// Check for string (const char*) equality between s1 and s2, LOG(FATAL) if not.
148#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
149#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
150
151// Perform the pthread function call(args), LOG(FATAL) on error.
152#define CHECK_PTHREAD_CALL(call, args, what) \
153  do { \
154    int rc = call args; \
155    if (rc != 0) { \
156      errno = rc; \
157      PLOG(::art::FATAL) << # call << " failed for " << what; \
158    } \
159  } while (false)
160
161// CHECK that can be used in a constexpr function. For example,
162//    constexpr int half(int n) {
163//      return
164//          DCHECK_CONSTEXPR(n >= 0, , 0)
165//          CHECK_CONSTEXPR((n & 1) == 0), << "Extra debugging output: n = " << n, 0)
166//          n / 2;
167//    }
168#define CHECK_CONSTEXPR(x, out, dummy) \
169  (UNLIKELY(!(x))) ? (LOG(::art::FATAL) << "Check failed: " << #x out, dummy) :
170
171
172// DCHECKs are debug variants of CHECKs only enabled in debug builds. Generally CHECK should be
173// used unless profiling identifies a CHECK as being in performance critical code.
174#if defined(NDEBUG)
175static constexpr bool kEnableDChecks = false;
176#else
177static constexpr bool kEnableDChecks = true;
178#endif
179
180#define DCHECK(x) if (::art::kEnableDChecks) CHECK(x)
181#define DCHECK_EQ(x, y) if (::art::kEnableDChecks) CHECK_EQ(x, y)
182#define DCHECK_NE(x, y) if (::art::kEnableDChecks) CHECK_NE(x, y)
183#define DCHECK_LE(x, y) if (::art::kEnableDChecks) CHECK_LE(x, y)
184#define DCHECK_LT(x, y) if (::art::kEnableDChecks) CHECK_LT(x, y)
185#define DCHECK_GE(x, y) if (::art::kEnableDChecks) CHECK_GE(x, y)
186#define DCHECK_GT(x, y) if (::art::kEnableDChecks) CHECK_GT(x, y)
187#define DCHECK_STREQ(s1, s2) if (::art::kEnableDChecks) CHECK_STREQ(s1, s2)
188#define DCHECK_STRNE(s1, s2) if (::art::kEnableDChecks) CHECK_STRNE(s1, s2)
189#if defined(NDEBUG)
190#define DCHECK_CONSTEXPR(x, out, dummy)
191#else
192#define DCHECK_CONSTEXPR(x, out, dummy) CHECK_CONSTEXPR(x, out, dummy)
193#endif
194
195// Temporary class created to evaluate the LHS and RHS, used with MakeEagerEvaluator to infer the
196// types of LHS and RHS.
197template <typename LHS, typename RHS>
198struct EagerEvaluator {
199  EagerEvaluator(LHS l, RHS r) : lhs(l), rhs(r) { }
200  LHS lhs;
201  RHS rhs;
202};
203
204// Helper function for CHECK_xx.
205template <typename LHS, typename RHS>
206static inline EagerEvaluator<LHS, RHS> MakeEagerEvaluator(LHS lhs, RHS rhs) {
207  return EagerEvaluator<LHS, RHS>(lhs, rhs);
208}
209
210// Explicitly instantiate EagerEvalue for pointers so that char*s aren't treated as strings. To
211// compare strings use CHECK_STREQ and CHECK_STRNE. We rely on signed/unsigned warnings to
212// protect you against combinations not explicitly listed below.
213#define EAGER_PTR_EVALUATOR(T1, T2) \
214  template <> struct EagerEvaluator<T1, T2> { \
215    EagerEvaluator(T1 l, T2 r) \
216        : lhs(reinterpret_cast<const void*>(l)), \
217          rhs(reinterpret_cast<const void*>(r)) { } \
218    const void* lhs; \
219    const void* rhs; \
220  }
221EAGER_PTR_EVALUATOR(const char*, const char*);
222EAGER_PTR_EVALUATOR(const char*, char*);
223EAGER_PTR_EVALUATOR(char*, const char*);
224EAGER_PTR_EVALUATOR(char*, char*);
225EAGER_PTR_EVALUATOR(const unsigned char*, const unsigned char*);
226EAGER_PTR_EVALUATOR(const unsigned char*, unsigned char*);
227EAGER_PTR_EVALUATOR(unsigned char*, const unsigned char*);
228EAGER_PTR_EVALUATOR(unsigned char*, unsigned char*);
229EAGER_PTR_EVALUATOR(const signed char*, const signed char*);
230EAGER_PTR_EVALUATOR(const signed char*, signed char*);
231EAGER_PTR_EVALUATOR(signed char*, const signed char*);
232EAGER_PTR_EVALUATOR(signed char*, signed char*);
233
234// Data for the log message, not stored in LogMessage to avoid increasing the stack size.
235class LogMessageData;
236
237// A LogMessage is a temporarily scoped object used by LOG and the unlikely part of a CHECK. The
238// destructor will abort if the severity is FATAL.
239class LogMessage {
240 public:
241  LogMessage(const char* file, unsigned int line, LogSeverity severity, int error);
242
243  ~LogMessage();  // TODO: enable REQUIRES(!Locks::logging_lock_).
244
245  // Returns the stream associated with the message, the LogMessage performs output when it goes
246  // out of scope.
247  std::ostream& stream();
248
249  // The routine that performs the actual logging.
250  static void LogLine(const char* file, unsigned int line, LogSeverity severity, const char* msg);
251
252  // A variant of the above for use with little stack.
253  static void LogLineLowStack(const char* file, unsigned int line, LogSeverity severity,
254                              const char* msg);
255
256 private:
257  const std::unique_ptr<LogMessageData> data_;
258
259  DISALLOW_COPY_AND_ASSIGN(LogMessage);
260};
261
262// Allows to temporarily change the minimum severity level for logging.
263class ScopedLogSeverity {
264 public:
265  explicit ScopedLogSeverity(LogSeverity level);
266  ~ScopedLogSeverity();
267
268 private:
269  LogSeverity old_;
270};
271
272}  // namespace art
273
274#endif  // ART_RUNTIME_BASE_LOGGING_H_
275