blob: bca0cf60b559d9f0108365496ef7b6c721095180 [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)) \
26 LogMessage(__FILE__, __LINE__, FATAL, -1).stream() << "Check failed: " #x
27
Elliott Hughes1f359b02011-07-17 14:27:17 -070028#define CHECK_OP(LHS, RHS, OP) \
29 do { \
30 typeof (LHS) _lhs = (LHS); \
31 typeof (RHS) _rhs = (RHS); \
32 if (!(_lhs OP _rhs)) { \
33 LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
34 << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
35 << " (" #LHS "=" << _lhs << ", " #RHS "=" << _rhs << ")"; \
36 } \
37 } while (false)
38
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) \
47 do { \
48 if ((strcmp(s1, s2) == 0) != sense) { \
49 LOG(FATAL) << "Check failed: " \
50 << "\"" << s1 << "\"" \
51 << (sense ? " == " : " != ") \
52 << "\"" << s2 << "\""; \
53 } \
54 } while (false)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070055
Elliott Hughes1f359b02011-07-17 14:27:17 -070056#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
57#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
58
Carl Shapiro6c21dc12011-06-20 15:20:52 -070059#ifndef NDEBUG
60
61#define DCHECK(x) CHECK(x)
62#define DCHECK_EQ(x, y) CHECK_EQ(x, y)
63#define DCHECK_NE(x, y) CHECK_NE(x, y)
64#define DCHECK_LE(x, y) CHECK_LE(x, y)
65#define DCHECK_LT(x, y) CHECK_LT(x, y)
66#define DCHECK_GE(x, y) CHECK_GE(x, y)
67#define DCHECK_GT(x, y) CHECK_GT(x, y)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070068#define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2)
69#define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070070
71#else // NDEBUG
72
73#define DCHECK(condition) \
74 while (false) \
75 CHECK(condition)
76
77#define DCHECK_EQ(val1, val2) \
78 while (false) \
79 CHECK_EQ(val1, val2)
80
81#define DCHECK_NE(val1, val2) \
82 while (false) \
83 CHECK_NE(val1, val2)
84
85#define DCHECK_LE(val1, val2) \
86 while (false) \
87 CHECK_LE(val1, val2)
88
89#define DCHECK_LT(val1, val2) \
90 while (false) \
91 CHECK_LT(val1, val2)
92
93#define DCHECK_GE(val1, val2) \
94 while (false) \
95 CHECK_GE(val1, val2)
96
97#define DCHECK_GT(val1, val2) \
98 while (false) \
99 CHECK_GT(val1, val2)
100
101#define DCHECK_STREQ(str1, str2) \
102 while (false) \
103 CHECK_STREQ(str1, str2)
104
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700105#define DCHECK_STRNE(str1, str2) \
106 while (false) \
107 CHECK_STRNE(str1, str2)
108
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700109#endif
110
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700111#define LOG(severity) LogMessage(__FILE__, __LINE__, severity, -1).stream()
112#define PLOG(severity) LogMessage(__FILE__, __LINE__, severity, errno).stream()
113
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700114#define LG LOG(INFO)
115
Elliott Hughes53b61312011-08-12 18:28:20 -0700116#define UNIMPLEMENTED(level) LOG(level) << __FUNCTION__ << " unimplemented"
117
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700118class LogMessage {
119 public:
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700120 LogMessage(const char* file, int line, LogSeverity severity, int error);
121 ~LogMessage();
122 std::ostream& stream();
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700123
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700124 private:
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700125 std::stringstream buffer_;
Elliott Hughesffe67362011-07-17 12:09:27 -0700126 const char* file_;
127 int line_;
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700128 LogSeverity severity_;
129 int errno_;
130
131 DISALLOW_COPY_AND_ASSIGN(LogMessage);
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700132};
133
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700134#endif // ART_SRC_LOGGING_H_