blob: 4abafdd3e7eb67abf1dd111f794e209e77a40871 [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
Elliott Hughes8d768a92011-09-14 16:35:25 -070063#define CHECK_PTHREAD_CALL(call, args, what) \
64 do { \
65 int rc = call args; \
66 if (rc != 0) { \
67 errno = rc; \
68 PLOG(FATAL) << # call << " failed for " << what; \
69 } \
70 } while (false)
71
Carl Shapiro6c21dc12011-06-20 15:20:52 -070072#ifndef NDEBUG
73
74#define DCHECK(x) CHECK(x)
75#define DCHECK_EQ(x, y) CHECK_EQ(x, y)
76#define DCHECK_NE(x, y) CHECK_NE(x, y)
77#define DCHECK_LE(x, y) CHECK_LE(x, y)
78#define DCHECK_LT(x, y) CHECK_LT(x, y)
79#define DCHECK_GE(x, y) CHECK_GE(x, y)
80#define DCHECK_GT(x, y) CHECK_GT(x, y)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070081#define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2)
82#define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070083
84#else // NDEBUG
85
86#define DCHECK(condition) \
87 while (false) \
88 CHECK(condition)
89
90#define DCHECK_EQ(val1, val2) \
91 while (false) \
92 CHECK_EQ(val1, val2)
93
94#define DCHECK_NE(val1, val2) \
95 while (false) \
96 CHECK_NE(val1, val2)
97
98#define DCHECK_LE(val1, val2) \
99 while (false) \
100 CHECK_LE(val1, val2)
101
102#define DCHECK_LT(val1, val2) \
103 while (false) \
104 CHECK_LT(val1, val2)
105
106#define DCHECK_GE(val1, val2) \
107 while (false) \
108 CHECK_GE(val1, val2)
109
110#define DCHECK_GT(val1, val2) \
111 while (false) \
112 CHECK_GT(val1, val2)
113
114#define DCHECK_STREQ(str1, str2) \
115 while (false) \
116 CHECK_STREQ(str1, str2)
117
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700118#define DCHECK_STRNE(str1, str2) \
119 while (false) \
120 CHECK_STRNE(str1, str2)
121
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700122#endif
123
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700124#define LOG(severity) LogMessage(__FILE__, __LINE__, severity, -1).stream()
125#define PLOG(severity) LogMessage(__FILE__, __LINE__, severity, errno).stream()
126
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700127#define LG LOG(INFO)
128
Elliott Hughes8d768a92011-09-14 16:35:25 -0700129#define UNIMPLEMENTED(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
Elliott Hughes53b61312011-08-12 18:28:20 -0700130
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700131class LogMessage {
132 public:
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700133 LogMessage(const char* file, int line, LogSeverity severity, int error);
134 ~LogMessage();
135 std::ostream& stream();
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700136
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700137 private:
Elliott Hughes42ee1422011-09-06 12:33:32 -0700138 void LogLine(const char*);
139
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700140 std::stringstream buffer_;
Elliott Hughesffe67362011-07-17 12:09:27 -0700141 const char* file_;
Elliott Hughes42ee1422011-09-06 12:33:32 -0700142 int line_number_;
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700143 LogSeverity severity_;
144 int errno_;
145
146 DISALLOW_COPY_AND_ASSIGN(LogMessage);
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700147};
148
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700149#endif // ART_SRC_LOGGING_H_