blob: 5169e2ac4d958e6877baa76e62b5d3c28b9cfc56 [file] [log] [blame]
Carl Shapiro6c21dc12011-06-20 15:20:52 -07001// Copyright 2010 Google
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070014#ifndef ART_SRC_LOGGING_H_
15#define ART_SRC_LOGGING_H_
Carl Shapiro6c21dc12011-06-20 15:20:52 -070016
Elliott Hugheseb4f6142011-07-15 17:43:51 -070017#include <cerrno>
18#include <cstring>
Carl Shapiro6c21dc12011-06-20 15:20:52 -070019#include <iostream> // NOLINT
Elliott Hugheseb4f6142011-07-15 17:43:51 -070020#include <sstream>
21#include "log_severity.h"
22#include "macros.h"
Carl Shapiro6c21dc12011-06-20 15:20:52 -070023
24#define CHECK(x) \
Elliott Hugheseb4f6142011-07-15 17:43:51 -070025 if (!(x)) \
Elliott Hughes710a0cb2011-08-16 14:32:37 -070026 LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
27 << "Check failed: " #x << " "
Elliott Hugheseb4f6142011-07-15 17:43:51 -070028
Elliott Hughes1f359b02011-07-17 14:27:17 -070029#define CHECK_OP(LHS, RHS, OP) \
30 do { \
31 typeof (LHS) _lhs = (LHS); \
32 typeof (RHS) _rhs = (RHS); \
33 if (!(_lhs OP _rhs)) { \
34 LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
35 << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
36 << " (" #LHS "=" << _lhs << ", " #RHS "=" << _rhs << ")"; \
37 } \
38 } while (false)
39
40#define CHECK_EQ(x, y) CHECK_OP(x, y, ==)
41#define CHECK_NE(x, y) CHECK_OP(x, y, !=)
42#define CHECK_LE(x, y) CHECK_OP(x, y, <=)
43#define CHECK_LT(x, y) CHECK_OP(x, y, <)
44#define CHECK_GE(x, y) CHECK_OP(x, y, >=)
45#define CHECK_GT(x, y) CHECK_OP(x, y, >)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070046
47#define CHECK_STROP(s1, s2, sense) \
48 do { \
49 if ((strcmp(s1, s2) == 0) != sense) { \
50 LOG(FATAL) << "Check failed: " \
51 << "\"" << s1 << "\"" \
52 << (sense ? " == " : " != ") \
53 << "\"" << s2 << "\""; \
54 } \
55 } while (false)
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
Carl Shapiro6c21dc12011-06-20 15:20:52 -070060#ifndef NDEBUG
61
62#define DCHECK(x) CHECK(x)
63#define DCHECK_EQ(x, y) CHECK_EQ(x, y)
64#define DCHECK_NE(x, y) CHECK_NE(x, y)
65#define DCHECK_LE(x, y) CHECK_LE(x, y)
66#define DCHECK_LT(x, y) CHECK_LT(x, y)
67#define DCHECK_GE(x, y) CHECK_GE(x, y)
68#define DCHECK_GT(x, y) CHECK_GT(x, y)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070069#define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2)
70#define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070071
72#else // NDEBUG
73
74#define DCHECK(condition) \
75 while (false) \
76 CHECK(condition)
77
78#define DCHECK_EQ(val1, val2) \
79 while (false) \
80 CHECK_EQ(val1, val2)
81
82#define DCHECK_NE(val1, val2) \
83 while (false) \
84 CHECK_NE(val1, val2)
85
86#define DCHECK_LE(val1, val2) \
87 while (false) \
88 CHECK_LE(val1, val2)
89
90#define DCHECK_LT(val1, val2) \
91 while (false) \
92 CHECK_LT(val1, val2)
93
94#define DCHECK_GE(val1, val2) \
95 while (false) \
96 CHECK_GE(val1, val2)
97
98#define DCHECK_GT(val1, val2) \
99 while (false) \
100 CHECK_GT(val1, val2)
101
102#define DCHECK_STREQ(str1, str2) \
103 while (false) \
104 CHECK_STREQ(str1, str2)
105
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700106#define DCHECK_STRNE(str1, str2) \
107 while (false) \
108 CHECK_STRNE(str1, str2)
109
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700110#endif
111
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700112#define LOG(severity) LogMessage(__FILE__, __LINE__, severity, -1).stream()
113#define PLOG(severity) LogMessage(__FILE__, __LINE__, severity, errno).stream()
114
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700115#define LG LOG(INFO)
116
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700117#define UNIMPLEMENTED(level) LOG(level) << __FUNCTION__ << " unimplemented "
Elliott Hughes53b61312011-08-12 18:28:20 -0700118
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700119class LogMessage {
120 public:
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700121 LogMessage(const char* file, int line, LogSeverity severity, int error);
122 ~LogMessage();
123 std::ostream& stream();
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700124
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700125 private:
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700126 std::stringstream buffer_;
Elliott Hughesffe67362011-07-17 12:09:27 -0700127 const char* file_;
128 int line_;
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700129 LogSeverity severity_;
130 int errno_;
131
132 DISALLOW_COPY_AND_ASSIGN(LogMessage);
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700133};
134
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700135#endif // ART_SRC_LOGGING_H_