blob: e1b99d6058b9800a716dd0905174c465c88fa4b4 [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 Shapiro1fb86202011-06-27 17:43:13 -070077#define LOG_WARN LOG_INFO // TODO
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070078#define LOG_FATAL LogMessageFatal(__FILE__, __LINE__)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070079#define LOG(severity) LOG_ ## severity.stream()
80#define LG LOG(INFO)
81
82class LogMessage {
83 public:
84 LogMessage(const char* file, int line) {}
85 ~LogMessage() { std::cerr << "\n"; }
86 std::ostream& stream() { return std::cerr; }
87 private:
88 DISALLOW_COPY_AND_ASSIGN(LogMessage);
89};
90
91class LogMessageFatal : public LogMessage {
92 public:
93 LogMessageFatal(const char* file, int line)
94 : LogMessage(file, line) {}
95 ~LogMessageFatal() {
96 std::cerr << "\n";
97 std::abort();
98 }
99 private:
100 DISALLOW_COPY_AND_ASSIGN(LogMessageFatal);
101};
102
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700103#endif // ART_SRC_LOGGING_H_