blob: 814195c7fa223e0cd0b17bf8e557048ba505a052 [file] [log] [blame]
Elliott Hughes42ee1422011-09-06 12:33:32 -07001/*
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 */
Carl Shapiro6c21dc12011-06-20 15:20:52 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_LOGGING_H_
18#define ART_RUNTIME_BASE_LOGGING_H_
Carl Shapiro6c21dc12011-06-20 15:20:52 -070019
Elliott Hugheseb4f6142011-07-15 17:43:51 -070020#include <cerrno>
21#include <cstring>
Carl Shapiro6c21dc12011-06-20 15:20:52 -070022#include <iostream> // NOLINT
Ian Rogers700a4022014-05-19 16:49:03 -070023#include <memory>
Elliott Hugheseb4f6142011-07-15 17:43:51 -070024#include <sstream>
Brian Carlstromaf1b8922012-11-27 15:19:57 -080025#include <signal.h>
Mingyao Yang42d65c52014-04-18 16:49:39 -070026#include <vector>
Ian Rogers700a4022014-05-19 16:49:03 -070027
Elliott Hughes76160052012-12-12 16:31:20 -080028#include "base/macros.h"
Elliott Hugheseb4f6142011-07-15 17:43:51 -070029#include "log_severity.h"
Carl Shapiro6c21dc12011-06-20 15:20:52 -070030
31#define CHECK(x) \
Ian Rogerscaab8c42011-10-12 12:11:18 -070032 if (UNLIKELY(!(x))) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070033 ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
Elliott Hughes710a0cb2011-08-16 14:32:37 -070034 << "Check failed: " #x << " "
Elliott Hugheseb4f6142011-07-15 17:43:51 -070035
Elliott Hughes1f359b02011-07-17 14:27:17 -070036#define CHECK_OP(LHS, RHS, OP) \
Mathieu Chartier9b3c3cd2013-08-12 17:41:54 -070037 for (auto _values = ::art::MakeEagerEvaluator(LHS, RHS); \
Elliott Hughes362f9bc2011-10-17 18:56:41 -070038 UNLIKELY(!(_values.lhs OP _values.rhs)); /* empty */) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070039 ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
40 << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
41 << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
Elliott Hughes1f359b02011-07-17 14:27:17 -070042
43#define CHECK_EQ(x, y) CHECK_OP(x, y, ==)
44#define CHECK_NE(x, y) CHECK_OP(x, y, !=)
45#define CHECK_LE(x, y) CHECK_OP(x, y, <=)
46#define CHECK_LT(x, y) CHECK_OP(x, y, <)
47#define CHECK_GE(x, y) CHECK_OP(x, y, >=)
48#define CHECK_GT(x, y) CHECK_OP(x, y, >)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070049
50#define CHECK_STROP(s1, s2, sense) \
Ian Rogerscaab8c42011-10-12 12:11:18 -070051 if (UNLIKELY((strcmp(s1, s2) == 0) != sense)) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070052 LOG(FATAL) << "Check failed: " \
53 << "\"" << s1 << "\"" \
54 << (sense ? " == " : " != ") \
55 << "\"" << s2 << "\""
Carl Shapiro6c21dc12011-06-20 15:20:52 -070056
Elliott Hughes1f359b02011-07-17 14:27:17 -070057#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
58#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
59
Elliott Hughes8d768a92011-09-14 16:35:25 -070060#define CHECK_PTHREAD_CALL(call, args, what) \
61 do { \
62 int rc = call args; \
63 if (rc != 0) { \
64 errno = rc; \
65 PLOG(FATAL) << # call << " failed for " << what; \
66 } \
67 } while (false)
68
Carl Shapiro6c21dc12011-06-20 15:20:52 -070069#ifndef NDEBUG
70
71#define DCHECK(x) CHECK(x)
72#define DCHECK_EQ(x, y) CHECK_EQ(x, y)
73#define DCHECK_NE(x, y) CHECK_NE(x, y)
74#define DCHECK_LE(x, y) CHECK_LE(x, y)
75#define DCHECK_LT(x, y) CHECK_LT(x, y)
76#define DCHECK_GE(x, y) CHECK_GE(x, y)
77#define DCHECK_GT(x, y) CHECK_GT(x, y)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070078#define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2)
79#define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070080
81#else // NDEBUG
82
83#define DCHECK(condition) \
84 while (false) \
85 CHECK(condition)
86
87#define DCHECK_EQ(val1, val2) \
88 while (false) \
89 CHECK_EQ(val1, val2)
90
91#define DCHECK_NE(val1, val2) \
92 while (false) \
93 CHECK_NE(val1, val2)
94
95#define DCHECK_LE(val1, val2) \
96 while (false) \
97 CHECK_LE(val1, val2)
98
99#define DCHECK_LT(val1, val2) \
100 while (false) \
101 CHECK_LT(val1, val2)
102
103#define DCHECK_GE(val1, val2) \
104 while (false) \
105 CHECK_GE(val1, val2)
106
107#define DCHECK_GT(val1, val2) \
108 while (false) \
109 CHECK_GT(val1, val2)
110
111#define DCHECK_STREQ(str1, str2) \
112 while (false) \
113 CHECK_STREQ(str1, str2)
114
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700115#define DCHECK_STRNE(str1, str2) \
116 while (false) \
117 CHECK_STRNE(str1, str2)
118
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700119#endif
120
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700121#define LOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, -1).stream()
122#define PLOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, errno).stream()
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700123
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700124#define LG LOG(INFO)
125
Elliott Hughes8d768a92011-09-14 16:35:25 -0700126#define UNIMPLEMENTED(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
Elliott Hughes53b61312011-08-12 18:28:20 -0700127
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800128#define VLOG_IS_ON(module) UNLIKELY(::art::gLogVerbosity.module)
129#define VLOG(module) if (VLOG_IS_ON(module)) ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream()
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700130#define VLOG_STREAM(module) ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream()
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800131
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700132//
133// Implementation details beyond this point.
134//
135
136namespace art {
137
138template <typename LHS, typename RHS>
139struct EagerEvaluator {
140 EagerEvaluator(LHS lhs, RHS rhs) : lhs(lhs), rhs(rhs) { }
141 LHS lhs;
142 RHS rhs;
143};
144
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800145// We want char*s to be treated as pointers, not strings. If you want them treated like strings,
146// you'd need to use CHECK_STREQ and CHECK_STRNE anyway to compare the characters rather than their
147// addresses. We could express this more succinctly with std::remove_const, but this is quick and
148// easy to understand, and works before we have C++0x. We rely on signed/unsigned warnings to
149// protect you against combinations not explicitly listed below.
150#define EAGER_PTR_EVALUATOR(T1, T2) \
151 template <> struct EagerEvaluator<T1, T2> { \
152 EagerEvaluator(T1 lhs, T2 rhs) \
153 : lhs(reinterpret_cast<const void*>(lhs)), \
154 rhs(reinterpret_cast<const void*>(rhs)) { } \
155 const void* lhs; \
156 const void* rhs; \
157 }
158EAGER_PTR_EVALUATOR(const char*, const char*);
159EAGER_PTR_EVALUATOR(const char*, char*);
160EAGER_PTR_EVALUATOR(char*, const char*);
161EAGER_PTR_EVALUATOR(char*, char*);
162EAGER_PTR_EVALUATOR(const unsigned char*, const unsigned char*);
163EAGER_PTR_EVALUATOR(const unsigned char*, unsigned char*);
164EAGER_PTR_EVALUATOR(unsigned char*, const unsigned char*);
165EAGER_PTR_EVALUATOR(unsigned char*, unsigned char*);
166EAGER_PTR_EVALUATOR(const signed char*, const signed char*);
167EAGER_PTR_EVALUATOR(const signed char*, signed char*);
168EAGER_PTR_EVALUATOR(signed char*, const signed char*);
169EAGER_PTR_EVALUATOR(signed char*, signed char*);
170
Mathieu Chartier9b3c3cd2013-08-12 17:41:54 -0700171template <typename LHS, typename RHS>
172EagerEvaluator<LHS, RHS> MakeEagerEvaluator(LHS lhs, RHS rhs) {
173 return EagerEvaluator<LHS, RHS>(lhs, rhs);
174}
175
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700176// This indirection greatly reduces the stack impact of having
177// lots of checks/logging in a function.
178struct LogMessageData {
179 public:
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800180 LogMessageData(const char* file, int line, LogSeverity severity, int error);
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700181 std::ostringstream buffer;
Ian Rogersc4ee12e2013-05-16 11:19:53 -0700182 const char* const file;
183 const int line_number;
184 const LogSeverity severity;
185 const int error;
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700186
187 private:
188 DISALLOW_COPY_AND_ASSIGN(LogMessageData);
189};
190
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700191class LogMessage {
192 public:
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800193 LogMessage(const char* file, int line, LogSeverity severity, int error)
194 : data_(new LogMessageData(file, line, severity, error)) {
195 }
Sebastien Hertz74c07042013-05-17 14:04:12 +0200196
Ian Rogers719d1a32014-03-06 12:13:39 -0800197 ~LogMessage(); // TODO: enable LOCKS_EXCLUDED(Locks::logging_lock_).
Sebastien Hertz74c07042013-05-17 14:04:12 +0200198
199 std::ostream& stream() {
200 return data_->buffer;
201 }
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700202
203 private:
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800204 static void LogLine(const LogMessageData& data, const char*);
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700205
Ian Rogers700a4022014-05-19 16:49:03 -0700206 const std::unique_ptr<LogMessageData> data_;
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700207
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800208 friend void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context);
Ian Rogersc4ee12e2013-05-16 11:19:53 -0700209 friend class Mutex;
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700210 DISALLOW_COPY_AND_ASSIGN(LogMessage);
211};
212
Elliott Hughese0918552011-10-28 17:18:29 -0700213// A convenience to allow any class with a "Dump(std::ostream& os)" member function
214// but without an operator<< to be used as if it had an operator<<. Use like this:
215//
216// os << Dumpable<MyType>(my_type_instance);
217//
218template<typename T>
219class Dumpable {
220 public:
221 explicit Dumpable(T& value) : value_(value) {
222 }
223
224 void Dump(std::ostream& os) const {
225 value_.Dump(os);
226 }
227
228 private:
229 T& value_;
Shih-wei Liao24782c62012-01-08 12:46:11 -0800230
Elliott Hughese0918552011-10-28 17:18:29 -0700231 DISALLOW_COPY_AND_ASSIGN(Dumpable);
232};
233
234template<typename T>
235std::ostream& operator<<(std::ostream& os, const Dumpable<T>& rhs) {
236 rhs.Dump(os);
237 return os;
238}
239
Mathieu Chartierafe49982014-03-27 10:55:04 -0700240template<typename T>
241class ConstDumpable {
242 public:
243 explicit ConstDumpable(const T& value) : value_(value) {
244 }
245
246 void Dump(std::ostream& os) const {
247 value_.Dump(os);
248 }
249
250 private:
251 const T& value_;
252
253 DISALLOW_COPY_AND_ASSIGN(ConstDumpable);
254};
255
256template<typename T>
257std::ostream& operator<<(std::ostream& os, const ConstDumpable<T>& rhs) {
258 rhs.Dump(os);
259 return os;
260}
261
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700262// Helps you use operator<< in a const char*-like context such as our various 'F' methods with
263// format strings.
264template<typename T>
265class ToStr {
266 public:
Elliott Hughes74847412012-06-20 18:10:21 -0700267 explicit ToStr(const T& value) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700268 std::ostringstream os;
269 os << value;
270 s_ = os.str();
271 }
272
273 const char* c_str() const {
274 return s_.c_str();
275 }
276
277 const std::string& str() const {
278 return s_;
279 }
280
281 private:
282 std::string s_;
283 DISALLOW_COPY_AND_ASSIGN(ToStr);
284};
285
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800286// The members of this struct are the valid arguments to VLOG and VLOG_IS_ON in code,
287// and the "-verbose:" command line argument.
288struct LogVerbosity {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700289 bool class_linker; // Enabled with "-verbose:class".
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800290 bool compiler;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800291 bool gc;
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700292 bool heap;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800293 bool jdwp;
294 bool jni;
295 bool monitor;
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700296 bool profiler;
297 bool signals;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800298 bool startup;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700299 bool third_party_jni; // Enabled with "-verbose:third-party-jni".
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800300 bool threads;
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700301 bool verifier;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800302};
303
304extern LogVerbosity gLogVerbosity;
Brian Carlstrom81b88712012-11-05 19:21:30 -0800305
Mingyao Yang42d65c52014-04-18 16:49:39 -0700306extern std::vector<std::string> gVerboseMethods;
307
Brian Carlstrom81b88712012-11-05 19:21:30 -0800308// Used on fatal exit. Prevents recursive aborts. Allows us to disable
309// some error checking to ensure fatal shutdown makes forward progress.
Ian Rogersf08e4732013-04-09 09:45:49 -0700310extern unsigned int gAborting;
Brian Carlstrom81b88712012-11-05 19:21:30 -0800311
Elliott Hughes0d39c122012-06-06 16:41:17 -0700312extern void InitLogging(char* argv[]);
313
314extern const char* GetCmdLine();
315extern const char* ProgramInvocationName();
316extern const char* ProgramInvocationShortName();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800317
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700318} // namespace art
319
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700320#endif // ART_RUNTIME_BASE_LOGGING_H_