blob: 4a15f43052eee94129eed32578260f1b7e3ae631 [file] [log] [blame]
Dan Alberte3ea0582015-03-13 23:06:01 -07001/*
2 * Copyright (C) 2015 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 BASE_LOGGING_H
18#define BASE_LOGGING_H
19
Dan Albert2f638c02015-05-04 16:40:38 -070020#ifdef ERROR
21#error ERROR is already defined. If this is Windows code, #define NOGDI before \
22including anything.
23#endif
24
25#ifdef _WIN32
Spencer Low5d6ee462015-05-06 16:13:42 -070026#ifndef NOGDI
Dan Albert2f638c02015-05-04 16:40:38 -070027#define NOGDI // Suppress the evil ERROR macro.
28#endif
Spencer Low5d6ee462015-05-06 16:13:42 -070029#endif
Dan Albert2f638c02015-05-04 16:40:38 -070030
Dan Albert1f65c492015-03-27 11:20:14 -070031#include <functional>
Dan Alberte3ea0582015-03-13 23:06:01 -070032#include <memory>
33#include <ostream>
34
35#include "base/macros.h"
36
37namespace android {
38namespace base {
39
40enum LogSeverity {
41 VERBOSE,
42 DEBUG,
43 INFO,
44 WARNING,
45 ERROR,
46 FATAL,
47};
48
Dan Albertab5c8822015-03-27 11:20:14 -070049enum LogId {
Dan Albert1f65c492015-03-27 11:20:14 -070050 DEFAULT,
Dan Albertab5c8822015-03-27 11:20:14 -070051 MAIN,
52 SYSTEM,
53};
54
Dan Albert1f65c492015-03-27 11:20:14 -070055typedef std::function<void(LogId, LogSeverity, const char*, const char*,
56 unsigned int, const char*)> LogFunction;
57
58extern void StderrLogger(LogId, LogSeverity, const char*, const char*,
59 unsigned int, const char*);
60
61#ifdef __ANDROID__
62// We expose this even though it is the default because a user that wants to
63// override the default log buffer will have to construct this themselves.
64class LogdLogger {
65 public:
66 explicit LogdLogger(LogId default_log_id = android::base::MAIN);
67
68 void operator()(LogId, LogSeverity, const char* tag, const char* file,
69 unsigned int line, const char* message);
70
71 private:
72 LogId default_log_id_;
73};
74#endif
75
Dan Alberte3ea0582015-03-13 23:06:01 -070076// Configure logging based on ANDROID_LOG_TAGS environment variable.
77// We need to parse a string that looks like
78//
79// *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
80//
81// The tag (or '*' for the global level) comes first, followed by a colon and a
82// letter indicating the minimum priority level we're expected to log. This can
83// be used to reveal or conceal logs with specific tags.
Dan Albert1f65c492015-03-27 11:20:14 -070084extern void InitLogging(char* argv[], LogFunction&& logger);
85
86// Configures logging using the default logger (logd for the device, stderr for
87// the host).
Dan Alberte3ea0582015-03-13 23:06:01 -070088extern void InitLogging(char* argv[]);
89
Dan Albert1f65c492015-03-27 11:20:14 -070090// Replace the current logger.
91extern void SetLogger(LogFunction&& logger);
92
Dan Alberte3ea0582015-03-13 23:06:01 -070093// Logs a message to logcat on Android otherwise to stderr. If the severity is
94// FATAL it also causes an abort. For example:
95//
96// LOG(FATAL) << "We didn't expect to reach here";
Dan Albert1f65c492015-03-27 11:20:14 -070097#define LOG(severity) \
98 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \
Dan Albertab5c8822015-03-27 11:20:14 -070099 ::android::base::severity, -1).stream()
100
101// Logs a message to logcat with the specified log ID on Android otherwise to
102// stderr. If the severity is FATAL it also causes an abort.
103#define LOG_TO(dest, severity) \
104 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::dest, \
105 ::android::base::severity, -1).stream()
Dan Alberte3ea0582015-03-13 23:06:01 -0700106
107// A variant of LOG that also logs the current errno value. To be used when
108// library calls fail.
Dan Albert1f65c492015-03-27 11:20:14 -0700109#define PLOG(severity) \
110 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \
Dan Albertab5c8822015-03-27 11:20:14 -0700111 ::android::base::severity, errno).stream()
112
113// Behaves like PLOG, but logs to the specified log ID.
114#define PLOG_TO(dest, severity) \
115 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::dest, \
116 ::android::base::severity, errno).stream()
Dan Alberte3ea0582015-03-13 23:06:01 -0700117
118// Marker that code is yet to be implemented.
119#define UNIMPLEMENTED(level) \
120 LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
121
122// Check whether condition x holds and LOG(FATAL) if not. The value of the
123// expression x is only evaluated once. Extra logging can be appended using <<
124// after. For example:
125//
126// CHECK(false == true) results in a log message of
127// "Check failed: false == true".
Dan Albert1f65c492015-03-27 11:20:14 -0700128#define CHECK(x) \
129 if (UNLIKELY(!(x))) \
130 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \
131 ::android::base::FATAL, -1).stream() \
Dan Albertab5c8822015-03-27 11:20:14 -0700132 << "Check failed: " #x << " "
Dan Alberte3ea0582015-03-13 23:06:01 -0700133
134// Helper for CHECK_xx(x,y) macros.
Dan Albert1f65c492015-03-27 11:20:14 -0700135#define CHECK_OP(LHS, RHS, OP) \
136 for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS); \
137 UNLIKELY(!(_values.lhs OP _values.rhs)); \
138 /* empty */) \
139 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \
140 ::android::base::FATAL, -1).stream() \
141 << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
Dan Alberte3ea0582015-03-13 23:06:01 -0700142 << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
143
144// Check whether a condition holds between x and y, LOG(FATAL) if not. The value
145// of the expressions x and y is evaluated once. Extra logging can be appended
146// using << after. For example:
147//
148// CHECK_NE(0 == 1, false) results in
149// "Check failed: false != false (0==1=false, false=false) ".
150#define CHECK_EQ(x, y) CHECK_OP(x, y, == )
151#define CHECK_NE(x, y) CHECK_OP(x, y, != )
152#define CHECK_LE(x, y) CHECK_OP(x, y, <= )
153#define CHECK_LT(x, y) CHECK_OP(x, y, < )
154#define CHECK_GE(x, y) CHECK_OP(x, y, >= )
155#define CHECK_GT(x, y) CHECK_OP(x, y, > )
156
157// Helper for CHECK_STRxx(s1,s2) macros.
158#define CHECK_STROP(s1, s2, sense) \
159 if (UNLIKELY((strcmp(s1, s2) == 0) != sense)) \
160 LOG(FATAL) << "Check failed: " \
161 << "\"" << s1 << "\"" \
162 << (sense ? " == " : " != ") << "\"" << s2 << "\""
163
164// Check for string (const char*) equality between s1 and s2, LOG(FATAL) if not.
165#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
166#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
167
168// Perform the pthread function call(args), LOG(FATAL) on error.
169#define CHECK_PTHREAD_CALL(call, args, what) \
170 do { \
171 int rc = call args; \
172 if (rc != 0) { \
173 errno = rc; \
174 PLOG(FATAL) << #call << " failed for " << what; \
175 } \
176 } while (false)
177
178// CHECK that can be used in a constexpr function. For example:
179//
180// constexpr int half(int n) {
181// return
182// DCHECK_CONSTEXPR(n >= 0, , 0)
183// CHECK_CONSTEXPR((n & 1) == 0),
184// << "Extra debugging output: n = " << n, 0)
185// n / 2;
186// }
187#define CHECK_CONSTEXPR(x, out, dummy) \
188 (UNLIKELY(!(x))) \
189 ? (LOG(FATAL) << "Check failed: " << #x out, dummy) \
190 :
191
192// DCHECKs are debug variants of CHECKs only enabled in debug builds. Generally
193// CHECK should be used unless profiling identifies a CHECK as being in
194// performance critical code.
195#if defined(NDEBUG)
196static constexpr bool kEnableDChecks = false;
197#else
198static constexpr bool kEnableDChecks = true;
199#endif
200
201#define DCHECK(x) \
202 if (::android::base::kEnableDChecks) CHECK(x)
203#define DCHECK_EQ(x, y) \
204 if (::android::base::kEnableDChecks) CHECK_EQ(x, y)
205#define DCHECK_NE(x, y) \
206 if (::android::base::kEnableDChecks) CHECK_NE(x, y)
207#define DCHECK_LE(x, y) \
208 if (::android::base::kEnableDChecks) CHECK_LE(x, y)
209#define DCHECK_LT(x, y) \
210 if (::android::base::kEnableDChecks) CHECK_LT(x, y)
211#define DCHECK_GE(x, y) \
212 if (::android::base::kEnableDChecks) CHECK_GE(x, y)
213#define DCHECK_GT(x, y) \
214 if (::android::base::kEnableDChecks) CHECK_GT(x, y)
215#define DCHECK_STREQ(s1, s2) \
216 if (::android::base::kEnableDChecks) CHECK_STREQ(s1, s2)
217#define DCHECK_STRNE(s1, s2) \
218 if (::android::base::kEnableDChecks) CHECK_STRNE(s1, s2)
219#if defined(NDEBUG)
220#define DCHECK_CONSTEXPR(x, out, dummy)
221#else
222#define DCHECK_CONSTEXPR(x, out, dummy) CHECK_CONSTEXPR(x, out, dummy)
223#endif
224
225// Temporary class created to evaluate the LHS and RHS, used with
226// MakeEagerEvaluator to infer the types of LHS and RHS.
227template <typename LHS, typename RHS>
228struct EagerEvaluator {
229 EagerEvaluator(LHS l, RHS r) : lhs(l), rhs(r) {
230 }
231 LHS lhs;
232 RHS rhs;
233};
234
235// Helper function for CHECK_xx.
236template <typename LHS, typename RHS>
237static inline EagerEvaluator<LHS, RHS> MakeEagerEvaluator(LHS lhs, RHS rhs) {
238 return EagerEvaluator<LHS, RHS>(lhs, rhs);
239}
240
241// Explicitly instantiate EagerEvalue for pointers so that char*s aren't treated
242// as strings. To compare strings use CHECK_STREQ and CHECK_STRNE. We rely on
243// signed/unsigned warnings to protect you against combinations not explicitly
244// listed below.
245#define EAGER_PTR_EVALUATOR(T1, T2) \
246 template <> \
247 struct EagerEvaluator<T1, T2> { \
248 EagerEvaluator(T1 l, T2 r) \
249 : lhs(reinterpret_cast<const void*>(l)), \
250 rhs(reinterpret_cast<const void*>(r)) { \
251 } \
252 const void* lhs; \
253 const void* rhs; \
254 }
255EAGER_PTR_EVALUATOR(const char*, const char*);
256EAGER_PTR_EVALUATOR(const char*, char*);
257EAGER_PTR_EVALUATOR(char*, const char*);
258EAGER_PTR_EVALUATOR(char*, char*);
259EAGER_PTR_EVALUATOR(const unsigned char*, const unsigned char*);
260EAGER_PTR_EVALUATOR(const unsigned char*, unsigned char*);
261EAGER_PTR_EVALUATOR(unsigned char*, const unsigned char*);
262EAGER_PTR_EVALUATOR(unsigned char*, unsigned char*);
263EAGER_PTR_EVALUATOR(const signed char*, const signed char*);
264EAGER_PTR_EVALUATOR(const signed char*, signed char*);
265EAGER_PTR_EVALUATOR(signed char*, const signed char*);
266EAGER_PTR_EVALUATOR(signed char*, signed char*);
267
268// Data for the log message, not stored in LogMessage to avoid increasing the
269// stack size.
270class LogMessageData;
271
272// A LogMessage is a temporarily scoped object used by LOG and the unlikely part
273// of a CHECK. The destructor will abort if the severity is FATAL.
274class LogMessage {
275 public:
Dan Albertab5c8822015-03-27 11:20:14 -0700276 LogMessage(const char* file, unsigned int line, LogId id,
277 LogSeverity severity, int error);
Dan Alberte3ea0582015-03-13 23:06:01 -0700278
279 ~LogMessage();
280
281 // Returns the stream associated with the message, the LogMessage performs
282 // output when it goes out of scope.
283 std::ostream& stream();
284
285 // The routine that performs the actual logging.
Dan Albertab5c8822015-03-27 11:20:14 -0700286 static void LogLine(const char* file, unsigned int line, LogId id,
287 LogSeverity severity, const char* msg);
Dan Alberte3ea0582015-03-13 23:06:01 -0700288
Dan Alberte3ea0582015-03-13 23:06:01 -0700289 private:
290 const std::unique_ptr<LogMessageData> data_;
291
292 DISALLOW_COPY_AND_ASSIGN(LogMessage);
293};
294
295// Allows to temporarily change the minimum severity level for logging.
296class ScopedLogSeverity {
297 public:
298 explicit ScopedLogSeverity(LogSeverity level);
299 ~ScopedLogSeverity();
300
301 private:
302 LogSeverity old_;
303};
304
305} // namespace base
306} // namespace android
307
308#endif // BASE_LOGGING_H