blob: e358db605783b5d7ea649e328177846d1ea499c5 [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) \
Elliott Hugheseb4f6142011-07-15 17:43:51 -070028 if (!(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 Hughesf5a7a472011-10-07 14:31:02 -070032namespace art {
33
34template <typename LHS, typename RHS>
35struct EagerEvaluator {
36 EagerEvaluator(LHS lhs, RHS rhs) : lhs(lhs), rhs(rhs) { }
37 LHS lhs;
38 RHS rhs;
39};
40
41
42class LogMessage {
43 public:
44 LogMessage(const char* file, int line, LogSeverity severity, int error);
45 ~LogMessage();
46 std::ostream& stream();
47
48 private:
49 void LogLine(const char*);
50
51 std::stringstream buffer_;
52 const char* file_;
53 int line_number_;
54 LogSeverity severity_;
55 int errno_;
56
57 DISALLOW_COPY_AND_ASSIGN(LogMessage);
58};
59
60} // namespace art
61
Elliott Hughes1f359b02011-07-17 14:27:17 -070062#define CHECK_OP(LHS, RHS, OP) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070063 for (::art::EagerEvaluator<typeof(LHS), typeof(RHS)> _values(LHS, RHS); !(_values.lhs OP _values.rhs); ) \
64 ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
65 << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
66 << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
Elliott Hughes1f359b02011-07-17 14:27:17 -070067
68#define CHECK_EQ(x, y) CHECK_OP(x, y, ==)
69#define CHECK_NE(x, y) CHECK_OP(x, y, !=)
70#define CHECK_LE(x, y) CHECK_OP(x, y, <=)
71#define CHECK_LT(x, y) CHECK_OP(x, y, <)
72#define CHECK_GE(x, y) CHECK_OP(x, y, >=)
73#define CHECK_GT(x, y) CHECK_OP(x, y, >)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070074
75#define CHECK_STROP(s1, s2, sense) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070076 if ((strcmp(s1, s2) == 0) != sense) \
77 LOG(FATAL) << "Check failed: " \
78 << "\"" << s1 << "\"" \
79 << (sense ? " == " : " != ") \
80 << "\"" << s2 << "\""
Carl Shapiro6c21dc12011-06-20 15:20:52 -070081
Elliott Hughes1f359b02011-07-17 14:27:17 -070082#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
83#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
84
Elliott Hughes8d768a92011-09-14 16:35:25 -070085#define CHECK_PTHREAD_CALL(call, args, what) \
86 do { \
87 int rc = call args; \
88 if (rc != 0) { \
89 errno = rc; \
90 PLOG(FATAL) << # call << " failed for " << what; \
91 } \
92 } while (false)
93
Carl Shapiro6c21dc12011-06-20 15:20:52 -070094#ifndef NDEBUG
95
96#define DCHECK(x) CHECK(x)
97#define DCHECK_EQ(x, y) CHECK_EQ(x, y)
98#define DCHECK_NE(x, y) CHECK_NE(x, y)
99#define DCHECK_LE(x, y) CHECK_LE(x, y)
100#define DCHECK_LT(x, y) CHECK_LT(x, y)
101#define DCHECK_GE(x, y) CHECK_GE(x, y)
102#define DCHECK_GT(x, y) CHECK_GT(x, y)
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700103#define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2)
104#define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2)
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700105
106#else // NDEBUG
107
108#define DCHECK(condition) \
109 while (false) \
110 CHECK(condition)
111
112#define DCHECK_EQ(val1, val2) \
113 while (false) \
114 CHECK_EQ(val1, val2)
115
116#define DCHECK_NE(val1, val2) \
117 while (false) \
118 CHECK_NE(val1, val2)
119
120#define DCHECK_LE(val1, val2) \
121 while (false) \
122 CHECK_LE(val1, val2)
123
124#define DCHECK_LT(val1, val2) \
125 while (false) \
126 CHECK_LT(val1, val2)
127
128#define DCHECK_GE(val1, val2) \
129 while (false) \
130 CHECK_GE(val1, val2)
131
132#define DCHECK_GT(val1, val2) \
133 while (false) \
134 CHECK_GT(val1, val2)
135
136#define DCHECK_STREQ(str1, str2) \
137 while (false) \
138 CHECK_STREQ(str1, str2)
139
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700140#define DCHECK_STRNE(str1, str2) \
141 while (false) \
142 CHECK_STRNE(str1, str2)
143
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700144#endif
145
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700146#define LOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, -1).stream()
147#define PLOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, errno).stream()
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700148
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700149#define LG LOG(INFO)
150
Elliott Hughes8d768a92011-09-14 16:35:25 -0700151#define UNIMPLEMENTED(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
Elliott Hughes53b61312011-08-12 18:28:20 -0700152
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700153#endif // ART_SRC_LOGGING_H_