blob: ccab249a3e897b9ad8fceca79308518d2b2f501f [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
17#include <cstdlib>
18#include <iostream> // NOLINT
19#include "src/macros.h"
20
21#define CHECK(x) \
22 if (!(x)) LogMessageFatal(__FILE__, __LINE__).stream() << "Check failed: " #x
23#define CHECK_EQ(x, y) CHECK((x) == (y))
24#define CHECK_NE(x, y) CHECK((x) != (y))
25#define CHECK_LE(x, y) CHECK((x) <= (y))
26#define CHECK_LT(x, y) CHECK((x) < (y))
27#define CHECK_GE(x, y) CHECK((x) >= (y))
28#define CHECK_GT(x, y) CHECK((x) > (y))
29
30#ifndef NDEBUG
31
32#define DCHECK(x) CHECK(x)
33#define DCHECK_EQ(x, y) CHECK_EQ(x, y)
34#define DCHECK_NE(x, y) CHECK_NE(x, y)
35#define DCHECK_LE(x, y) CHECK_LE(x, y)
36#define DCHECK_LT(x, y) CHECK_LT(x, y)
37#define DCHECK_GE(x, y) CHECK_GE(x, y)
38#define DCHECK_GT(x, y) CHECK_GT(x, y)
39
40#else // NDEBUG
41
42#define DCHECK(condition) \
43 while (false) \
44 CHECK(condition)
45
46#define DCHECK_EQ(val1, val2) \
47 while (false) \
48 CHECK_EQ(val1, val2)
49
50#define DCHECK_NE(val1, val2) \
51 while (false) \
52 CHECK_NE(val1, val2)
53
54#define DCHECK_LE(val1, val2) \
55 while (false) \
56 CHECK_LE(val1, val2)
57
58#define DCHECK_LT(val1, val2) \
59 while (false) \
60 CHECK_LT(val1, val2)
61
62#define DCHECK_GE(val1, val2) \
63 while (false) \
64 CHECK_GE(val1, val2)
65
66#define DCHECK_GT(val1, val2) \
67 while (false) \
68 CHECK_GT(val1, val2)
69
70#define DCHECK_STREQ(str1, str2) \
71 while (false) \
72 CHECK_STREQ(str1, str2)
73
74#endif
75
76#define LOG_INFO LogMessage(__FILE__, __LINE__)
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070077#define LOG_FATAL LogMessageFatal(__FILE__, __LINE__)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070078#define LOG(severity) LOG_ ## severity.stream()
79#define LG LOG(INFO)
80
81class LogMessage {
82 public:
83 LogMessage(const char* file, int line) {}
84 ~LogMessage() { std::cerr << "\n"; }
85 std::ostream& stream() { return std::cerr; }
86 private:
87 DISALLOW_COPY_AND_ASSIGN(LogMessage);
88};
89
90class LogMessageFatal : public LogMessage {
91 public:
92 LogMessageFatal(const char* file, int line)
93 : LogMessage(file, line) {}
94 ~LogMessageFatal() {
95 std::cerr << "\n";
96 std::abort();
97 }
98 private:
99 DISALLOW_COPY_AND_ASSIGN(LogMessageFatal);
100};
101
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700102#endif // ART_SRC_LOGGING_H_