blob: b6306b2a9c20873a4f18e67ae709b8c1be27ff62 [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 Hughes1f359b02011-07-17 14:27:17 -070032#define CHECK_OP(LHS, RHS, OP) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070033 for (::art::EagerEvaluator<typeof(LHS), typeof(RHS)> _values(LHS, RHS); !(_values.lhs OP _values.rhs); ) \
34 ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
35 << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
36 << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
Elliott Hughes1f359b02011-07-17 14:27:17 -070037
38#define CHECK_EQ(x, y) CHECK_OP(x, y, ==)
39#define CHECK_NE(x, y) CHECK_OP(x, y, !=)
40#define CHECK_LE(x, y) CHECK_OP(x, y, <=)
41#define CHECK_LT(x, y) CHECK_OP(x, y, <)
42#define CHECK_GE(x, y) CHECK_OP(x, y, >=)
43#define CHECK_GT(x, y) CHECK_OP(x, y, >)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070044
45#define CHECK_STROP(s1, s2, sense) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070046 if ((strcmp(s1, s2) == 0) != sense) \
47 LOG(FATAL) << "Check failed: " \
48 << "\"" << s1 << "\"" \
49 << (sense ? " == " : " != ") \
50 << "\"" << s2 << "\""
Carl Shapiro6c21dc12011-06-20 15:20:52 -070051
Elliott Hughes1f359b02011-07-17 14:27:17 -070052#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
53#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
54
Elliott Hughes8d768a92011-09-14 16:35:25 -070055#define CHECK_PTHREAD_CALL(call, args, what) \
56 do { \
57 int rc = call args; \
58 if (rc != 0) { \
59 errno = rc; \
60 PLOG(FATAL) << # call << " failed for " << what; \
61 } \
62 } while (false)
63
Carl Shapiro6c21dc12011-06-20 15:20:52 -070064#ifndef NDEBUG
65
66#define DCHECK(x) CHECK(x)
67#define DCHECK_EQ(x, y) CHECK_EQ(x, y)
68#define DCHECK_NE(x, y) CHECK_NE(x, y)
69#define DCHECK_LE(x, y) CHECK_LE(x, y)
70#define DCHECK_LT(x, y) CHECK_LT(x, y)
71#define DCHECK_GE(x, y) CHECK_GE(x, y)
72#define DCHECK_GT(x, y) CHECK_GT(x, y)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070073#define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2)
74#define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070075
76#else // NDEBUG
77
78#define DCHECK(condition) \
79 while (false) \
80 CHECK(condition)
81
82#define DCHECK_EQ(val1, val2) \
83 while (false) \
84 CHECK_EQ(val1, val2)
85
86#define DCHECK_NE(val1, val2) \
87 while (false) \
88 CHECK_NE(val1, val2)
89
90#define DCHECK_LE(val1, val2) \
91 while (false) \
92 CHECK_LE(val1, val2)
93
94#define DCHECK_LT(val1, val2) \
95 while (false) \
96 CHECK_LT(val1, val2)
97
98#define DCHECK_GE(val1, val2) \
99 while (false) \
100 CHECK_GE(val1, val2)
101
102#define DCHECK_GT(val1, val2) \
103 while (false) \
104 CHECK_GT(val1, val2)
105
106#define DCHECK_STREQ(str1, str2) \
107 while (false) \
108 CHECK_STREQ(str1, str2)
109
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700110#define DCHECK_STRNE(str1, str2) \
111 while (false) \
112 CHECK_STRNE(str1, str2)
113
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700114#endif
115
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700116#define LOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, -1).stream()
117#define PLOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, errno).stream()
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700118
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700119#define LG LOG(INFO)
120
Elliott Hughes8d768a92011-09-14 16:35:25 -0700121#define UNIMPLEMENTED(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
Elliott Hughes53b61312011-08-12 18:28:20 -0700122
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700123//
124// Implementation details beyond this point.
125//
126
127namespace art {
128
129template <typename LHS, typename RHS>
130struct EagerEvaluator {
131 EagerEvaluator(LHS lhs, RHS rhs) : lhs(lhs), rhs(rhs) { }
132 LHS lhs;
133 RHS rhs;
134};
135
136class LogMessage {
137 public:
138 LogMessage(const char* file, int line, LogSeverity severity, int error);
139 ~LogMessage();
140 std::ostream& stream();
141
142 private:
143 void LogLine(const char*);
144
145 std::stringstream buffer_;
146 const char* file_;
147 int line_number_;
148 LogSeverity severity_;
149 int errno_;
150
151 DISALLOW_COPY_AND_ASSIGN(LogMessage);
152};
153
154} // namespace art
155
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700156#endif // ART_SRC_LOGGING_H_