blob: 24ffdbcc68e0ccfd8d58041c3fa5d2e057d4af39 [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
Carl Shapiro6c21dc12011-06-20 15:20:52 -070028#define CHECK_EQ(x, y) CHECK((x) == (y))
29#define CHECK_NE(x, y) CHECK((x) != (y))
30#define CHECK_LE(x, y) CHECK((x) <= (y))
31#define CHECK_LT(x, y) CHECK((x) < (y))
32#define CHECK_GE(x, y) CHECK((x) >= (y))
33#define CHECK_GT(x, y) CHECK((x) > (y))
Elliott Hugheseb4f6142011-07-15 17:43:51 -070034#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
35#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
36
37#define CHECK_STROP(s1, s2, sense) \
38 do { \
39 if ((strcmp(s1, s2) == 0) != sense) { \
40 LOG(FATAL) << "Check failed: " \
41 << "\"" << s1 << "\"" \
42 << (sense ? " == " : " != ") \
43 << "\"" << s2 << "\""; \
44 } \
45 } while (false)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070046
47#ifndef NDEBUG
48
49#define DCHECK(x) CHECK(x)
50#define DCHECK_EQ(x, y) CHECK_EQ(x, y)
51#define DCHECK_NE(x, y) CHECK_NE(x, y)
52#define DCHECK_LE(x, y) CHECK_LE(x, y)
53#define DCHECK_LT(x, y) CHECK_LT(x, y)
54#define DCHECK_GE(x, y) CHECK_GE(x, y)
55#define DCHECK_GT(x, y) CHECK_GT(x, y)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070056#define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2)
57#define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070058
59#else // NDEBUG
60
61#define DCHECK(condition) \
62 while (false) \
63 CHECK(condition)
64
65#define DCHECK_EQ(val1, val2) \
66 while (false) \
67 CHECK_EQ(val1, val2)
68
69#define DCHECK_NE(val1, val2) \
70 while (false) \
71 CHECK_NE(val1, val2)
72
73#define DCHECK_LE(val1, val2) \
74 while (false) \
75 CHECK_LE(val1, val2)
76
77#define DCHECK_LT(val1, val2) \
78 while (false) \
79 CHECK_LT(val1, val2)
80
81#define DCHECK_GE(val1, val2) \
82 while (false) \
83 CHECK_GE(val1, val2)
84
85#define DCHECK_GT(val1, val2) \
86 while (false) \
87 CHECK_GT(val1, val2)
88
89#define DCHECK_STREQ(str1, str2) \
90 while (false) \
91 CHECK_STREQ(str1, str2)
92
Elliott Hugheseb4f6142011-07-15 17:43:51 -070093#define DCHECK_STRNE(str1, str2) \
94 while (false) \
95 CHECK_STRNE(str1, str2)
96
Carl Shapiro6c21dc12011-06-20 15:20:52 -070097#endif
98
Elliott Hugheseb4f6142011-07-15 17:43:51 -070099#define LOG(severity) LogMessage(__FILE__, __LINE__, severity, -1).stream()
100#define PLOG(severity) LogMessage(__FILE__, __LINE__, severity, errno).stream()
101
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700102#define LG LOG(INFO)
103
104class LogMessage {
105 public:
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700106 LogMessage(const char* file, int line, LogSeverity severity, int error);
107 ~LogMessage();
108 std::ostream& stream();
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700109
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700110 private:
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700111 std::stringstream buffer_;
112 LogSeverity severity_;
113 int errno_;
114
115 DISALLOW_COPY_AND_ASSIGN(LogMessage);
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700116};
117
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700118#endif // ART_SRC_LOGGING_H_