blob: 2f735992209d0720945e0e30e62f09ff4a46a42f [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
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070017#ifndef ART_SRC_LOGGING_H_
18#define ART_SRC_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
Elliott Hugheseb4f6142011-07-15 17:43:51 -070023#include <sstream>
24#include "log_severity.h"
25#include "macros.h"
Carl Shapiro6c21dc12011-06-20 15:20:52 -070026
27#define CHECK(x) \
Ian Rogerscaab8c42011-10-12 12:11:18 -070028 if (UNLIKELY(!(x))) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070029 ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
Elliott Hughes710a0cb2011-08-16 14:32:37 -070030 << "Check failed: " #x << " "
Elliott Hugheseb4f6142011-07-15 17:43:51 -070031
Elliott Hughes1f359b02011-07-17 14:27:17 -070032#define CHECK_OP(LHS, RHS, OP) \
Ian Rogerscaab8c42011-10-12 12:11:18 -070033 for (::art::EagerEvaluator<typeof(LHS), typeof(RHS)> _values(LHS, RHS); \
34 UNLIKELY(!(_values.lhs OP _values.rhs)); ) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070035 ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
36 << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
37 << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
Elliott Hughes1f359b02011-07-17 14:27:17 -070038
39#define CHECK_EQ(x, y) CHECK_OP(x, y, ==)
40#define CHECK_NE(x, y) CHECK_OP(x, y, !=)
41#define CHECK_LE(x, y) CHECK_OP(x, y, <=)
42#define CHECK_LT(x, y) CHECK_OP(x, y, <)
43#define CHECK_GE(x, y) CHECK_OP(x, y, >=)
44#define CHECK_GT(x, y) CHECK_OP(x, y, >)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070045
46#define CHECK_STROP(s1, s2, sense) \
Ian Rogerscaab8c42011-10-12 12:11:18 -070047 if (UNLIKELY((strcmp(s1, s2) == 0) != sense)) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070048 LOG(FATAL) << "Check failed: " \
49 << "\"" << s1 << "\"" \
50 << (sense ? " == " : " != ") \
51 << "\"" << s2 << "\""
Carl Shapiro6c21dc12011-06-20 15:20:52 -070052
Elliott Hughes1f359b02011-07-17 14:27:17 -070053#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
54#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
55
Elliott Hughes8d768a92011-09-14 16:35:25 -070056#define CHECK_PTHREAD_CALL(call, args, what) \
57 do { \
58 int rc = call args; \
59 if (rc != 0) { \
60 errno = rc; \
61 PLOG(FATAL) << # call << " failed for " << what; \
62 } \
63 } while (false)
64
Carl Shapiro6c21dc12011-06-20 15:20:52 -070065#ifndef NDEBUG
66
67#define DCHECK(x) CHECK(x)
68#define DCHECK_EQ(x, y) CHECK_EQ(x, y)
69#define DCHECK_NE(x, y) CHECK_NE(x, y)
70#define DCHECK_LE(x, y) CHECK_LE(x, y)
71#define DCHECK_LT(x, y) CHECK_LT(x, y)
72#define DCHECK_GE(x, y) CHECK_GE(x, y)
73#define DCHECK_GT(x, y) CHECK_GT(x, y)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070074#define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2)
75#define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070076
77#else // NDEBUG
78
79#define DCHECK(condition) \
80 while (false) \
81 CHECK(condition)
82
83#define DCHECK_EQ(val1, val2) \
84 while (false) \
85 CHECK_EQ(val1, val2)
86
87#define DCHECK_NE(val1, val2) \
88 while (false) \
89 CHECK_NE(val1, val2)
90
91#define DCHECK_LE(val1, val2) \
92 while (false) \
93 CHECK_LE(val1, val2)
94
95#define DCHECK_LT(val1, val2) \
96 while (false) \
97 CHECK_LT(val1, val2)
98
99#define DCHECK_GE(val1, val2) \
100 while (false) \
101 CHECK_GE(val1, val2)
102
103#define DCHECK_GT(val1, val2) \
104 while (false) \
105 CHECK_GT(val1, val2)
106
107#define DCHECK_STREQ(str1, str2) \
108 while (false) \
109 CHECK_STREQ(str1, str2)
110
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700111#define DCHECK_STRNE(str1, str2) \
112 while (false) \
113 CHECK_STRNE(str1, str2)
114
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700115#endif
116
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700117#define LOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, -1).stream()
118#define PLOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, errno).stream()
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700119
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700120#define LG LOG(INFO)
121
Elliott Hughes8d768a92011-09-14 16:35:25 -0700122#define UNIMPLEMENTED(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
Elliott Hughes53b61312011-08-12 18:28:20 -0700123
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700124//
125// Implementation details beyond this point.
126//
127
128namespace art {
129
130template <typename LHS, typename RHS>
131struct EagerEvaluator {
132 EagerEvaluator(LHS lhs, RHS rhs) : lhs(lhs), rhs(rhs) { }
133 LHS lhs;
134 RHS rhs;
135};
136
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700137// This indirection greatly reduces the stack impact of having
138// lots of checks/logging in a function.
139struct LogMessageData {
140 public:
141 LogMessageData(int line, LogSeverity severity, int error)
142 : file(NULL),
143 line_number(line),
144 severity(severity),
145 error(error) {
146 }
147
148 std::ostringstream buffer;
149 const char* file;
150 int line_number;
151 LogSeverity severity;
152 int error;
153
154 private:
155 DISALLOW_COPY_AND_ASSIGN(LogMessageData);
156};
157
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700158class LogMessage {
159 public:
160 LogMessage(const char* file, int line, LogSeverity severity, int error);
161 ~LogMessage();
162 std::ostream& stream();
163
164 private:
165 void LogLine(const char*);
166
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700167 LogMessageData* data_;
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700168
169 DISALLOW_COPY_AND_ASSIGN(LogMessage);
170};
171
172} // namespace art
173
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700174#endif // ART_SRC_LOGGING_H_