blob: 43596b52c41ee18217a82574040976f62c40ff7a [file] [log] [blame]
Elliott Hughes42ee1422011-09-06 12:33:32 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro6c21dc12011-06-20 15:20:52 -070016
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070017#ifndef ART_SRC_LOGGING_H_
18#define ART_SRC_LOGGING_H_
Carl Shapiro6c21dc12011-06-20 15:20:52 -070019
Elliott Hugheseb4f6142011-07-15 17:43:51 -070020#include <cerrno>
21#include <cstring>
Carl Shapiro6c21dc12011-06-20 15:20:52 -070022#include <iostream> // NOLINT
Elliott Hugheseb4f6142011-07-15 17:43:51 -070023#include <sstream>
24#include "log_severity.h"
25#include "macros.h"
Carl Shapiro6c21dc12011-06-20 15:20:52 -070026
27#define CHECK(x) \
Elliott Hugheseb4f6142011-07-15 17:43:51 -070028 if (!(x)) \
Elliott Hughes710a0cb2011-08-16 14:32:37 -070029 LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
30 << "Check failed: " #x << " "
Elliott Hugheseb4f6142011-07-15 17:43:51 -070031
Elliott Hughes1f359b02011-07-17 14:27:17 -070032#define CHECK_OP(LHS, RHS, OP) \
33 do { \
34 typeof (LHS) _lhs = (LHS); \
35 typeof (RHS) _rhs = (RHS); \
36 if (!(_lhs OP _rhs)) { \
37 LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
38 << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
39 << " (" #LHS "=" << _lhs << ", " #RHS "=" << _rhs << ")"; \
40 } \
41 } while (false)
42
43#define CHECK_EQ(x, y) CHECK_OP(x, y, ==)
44#define CHECK_NE(x, y) CHECK_OP(x, y, !=)
45#define CHECK_LE(x, y) CHECK_OP(x, y, <=)
46#define CHECK_LT(x, y) CHECK_OP(x, y, <)
47#define CHECK_GE(x, y) CHECK_OP(x, y, >=)
48#define CHECK_GT(x, y) CHECK_OP(x, y, >)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070049
50#define CHECK_STROP(s1, s2, sense) \
51 do { \
52 if ((strcmp(s1, s2) == 0) != sense) { \
53 LOG(FATAL) << "Check failed: " \
54 << "\"" << s1 << "\"" \
55 << (sense ? " == " : " != ") \
56 << "\"" << s2 << "\""; \
57 } \
58 } while (false)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070059
Elliott Hughes1f359b02011-07-17 14:27:17 -070060#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
61#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
62
Carl Shapiro6c21dc12011-06-20 15:20:52 -070063#ifndef NDEBUG
64
65#define DCHECK(x) CHECK(x)
66#define DCHECK_EQ(x, y) CHECK_EQ(x, y)
67#define DCHECK_NE(x, y) CHECK_NE(x, y)
68#define DCHECK_LE(x, y) CHECK_LE(x, y)
69#define DCHECK_LT(x, y) CHECK_LT(x, y)
70#define DCHECK_GE(x, y) CHECK_GE(x, y)
71#define DCHECK_GT(x, y) CHECK_GT(x, y)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070072#define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2)
73#define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070074
75#else // NDEBUG
76
77#define DCHECK(condition) \
78 while (false) \
79 CHECK(condition)
80
81#define DCHECK_EQ(val1, val2) \
82 while (false) \
83 CHECK_EQ(val1, val2)
84
85#define DCHECK_NE(val1, val2) \
86 while (false) \
87 CHECK_NE(val1, val2)
88
89#define DCHECK_LE(val1, val2) \
90 while (false) \
91 CHECK_LE(val1, val2)
92
93#define DCHECK_LT(val1, val2) \
94 while (false) \
95 CHECK_LT(val1, val2)
96
97#define DCHECK_GE(val1, val2) \
98 while (false) \
99 CHECK_GE(val1, val2)
100
101#define DCHECK_GT(val1, val2) \
102 while (false) \
103 CHECK_GT(val1, val2)
104
105#define DCHECK_STREQ(str1, str2) \
106 while (false) \
107 CHECK_STREQ(str1, str2)
108
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700109#define DCHECK_STRNE(str1, str2) \
110 while (false) \
111 CHECK_STRNE(str1, str2)
112
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700113#endif
114
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700115#define LOG(severity) LogMessage(__FILE__, __LINE__, severity, -1).stream()
116#define PLOG(severity) LogMessage(__FILE__, __LINE__, severity, errno).stream()
117
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700118#define LG LOG(INFO)
119
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700120#define UNIMPLEMENTED(level) LOG(level) << __FUNCTION__ << " unimplemented "
Elliott Hughes53b61312011-08-12 18:28:20 -0700121
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700122class LogMessage {
123 public:
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700124 LogMessage(const char* file, int line, LogSeverity severity, int error);
125 ~LogMessage();
126 std::ostream& stream();
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700127
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700128 private:
Elliott Hughes42ee1422011-09-06 12:33:32 -0700129 void LogLine(const char*);
130
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700131 std::stringstream buffer_;
Elliott Hughesffe67362011-07-17 12:09:27 -0700132 const char* file_;
Elliott Hughes42ee1422011-09-06 12:33:32 -0700133 int line_number_;
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700134 LogSeverity severity_;
135 int errno_;
136
137 DISALLOW_COPY_AND_ASSIGN(LogMessage);
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700138};
139
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700140#endif // ART_SRC_LOGGING_H_