| 1 |
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 |
// Copyright (c) 2005, Google Inc. |
| 3 |
// All rights reserved. |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without |
| 6 |
// modification, are permitted provided that the following conditions are |
| 7 |
// met: |
| 8 |
// |
| 9 |
// * Redistributions of source code must retain the above copyright |
| 10 |
// notice, this list of conditions and the following disclaimer. |
| 11 |
// * Redistributions in binary form must reproduce the above |
| 12 |
// copyright notice, this list of conditions and the following disclaimer |
| 13 |
// in the documentation and/or other materials provided with the |
| 14 |
// distribution. |
| 15 |
// * Neither the name of Google Inc. nor the names of its |
| 16 |
// contributors may be used to endorse or promote products derived from |
| 17 |
// this software without specific prior written permission. |
| 18 |
// |
| 19 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
|
| 31 |
// --- |
| 32 |
// This file contains #include information about logging-related stuff. |
| 33 |
// Pretty much everybody needs to #include this file so that they can |
| 34 |
// log various happenings. |
| 35 |
// |
| 36 |
#ifndef _LOGGING_H_ |
| 37 |
#define _LOGGING_H_ |
| 38 |
|
| 39 |
#include <config.h> |
| 40 |
#include <stdarg.h> |
| 41 |
#include <stdlib.h> |
| 42 |
#include <stdio.h> |
| 43 |
#ifdef HAVE_UNISTD_H |
| 44 |
#include <unistd.h> // for write() |
| 45 |
#endif |
| 46 |
#include <string.h> // for strlen(), strcmp() |
| 47 |
#include <assert.h> |
| 48 |
#include <errno.h> // for errno |
| 49 |
#include "base/commandlineflags.h" |
| 50 |
|
| 51 |
// On some systems (like freebsd), we can't call write() at all in a |
| 52 |
// global constructor, perhaps because errno hasn't been set up. |
| 53 |
// (In windows, we can't call it because it might call malloc.) |
| 54 |
// Calling the write syscall is safer (it doesn't set errno), so we |
| 55 |
// prefer that. Note we don't care about errno for logging: we just |
| 56 |
// do logging on a best-effort basis. |
| 57 |
#if defined(_MSC_VER) |
| 58 |
#define WRITE_TO_STDERR(buf, len) WriteToStderr(buf, len); // in port.cc |
| 59 |
#elif HAVE_SYS_SYSCALL_H && !defined(__APPLE__) |
| 60 |
#include <sys/syscall.h> |
| 61 |
#define WRITE_TO_STDERR(buf, len) syscall(SYS_write, STDERR_FILENO, buf, len) |
| 62 |
#else |
| 63 |
#define WRITE_TO_STDERR(buf, len) write(STDERR_FILENO, buf, len) |
| 64 |
#endif |
| 65 |
|
| 66 |
// MSVC and mingw define their own, safe version of vnsprintf (the |
| 67 |
// windows one in broken) in port.cc. Everyone else can use the |
| 68 |
// version here. We had to give it a unique name for windows. |
| 69 |
#ifndef _WIN32 |
| 70 |
# define perftools_vsnprintf vsnprintf |
| 71 |
#endif |
| 72 |
|
| 73 |
|
| 74 |
// We log all messages at this log-level and below. |
| 75 |
// INFO == -1, WARNING == -2, ERROR == -3, FATAL == -4 |
| 76 |
DECLARE_int32(verbose); |
| 77 |
|
| 78 |
// CHECK dies with a fatal error if condition is not true. It is *not* |
| 79 |
// controlled by NDEBUG, so the check will be executed regardless of |
| 80 |
// compilation mode. Therefore, it is safe to do things like: |
| 81 |
// CHECK(fp->Write(x) == 4) |
| 82 |
// Note we use write instead of printf/puts to avoid the risk we'll |
| 83 |
// call malloc(). |
| 84 |
#define CHECK(condition) \ |
| 85 |
do { \ |
| 86 |
if (!(condition)) { \ |
| 87 |
WRITE_TO_STDERR("Check failed: " #condition "\n", \ |
| 88 |
sizeof("Check failed: " #condition "\n")-1); \ |
| 89 |
abort(); \ |
| 90 |
} \ |
| 91 |
} while (0) |
| 92 |
|
| 93 |
// This takes a message to print. The name is historical. |
| 94 |
#define RAW_CHECK(condition, message) \ |
| 95 |
do { \ |
| 96 |
if (!(condition)) { \ |
| 97 |
WRITE_TO_STDERR("Check failed: " #condition ": " message "\n", \ |
| 98 |
sizeof("Check failed: " #condition ": " message "\n")-1);\ |
| 99 |
abort(); \ |
| 100 |
} \ |
| 101 |
} while (0) |
| 102 |
|
| 103 |
// This is like RAW_CHECK, but only in debug-mode |
| 104 |
#ifdef NDEBUG |
| 105 |
enum { DEBUG_MODE = 0 }; |
| 106 |
#define RAW_DCHECK(condition, message) |
| 107 |
#else |
| 108 |
enum { DEBUG_MODE = 1 }; |
| 109 |
#define RAW_DCHECK(condition, message) RAW_CHECK(condition, message) |
| 110 |
#endif |
| 111 |
|
| 112 |
// This prints errno as well. Note we use write instead of printf/puts to |
| 113 |
// avoid the risk we'll call malloc(). |
| 114 |
#define PCHECK(condition) \ |
| 115 |
do { \ |
| 116 |
if (!(condition)) { \ |
| 117 |
const int err_no = errno; \ |
| 118 |
WRITE_TO_STDERR("Check failed: " #condition ": ", \ |
| 119 |
sizeof("Check failed: " #condition ": ")-1); \ |
| 120 |
WRITE_TO_STDERR(strerror(err_no), strlen(strerror(err_no))); \ |
| 121 |
WRITE_TO_STDERR("\n", sizeof("\n")-1); \ |
| 122 |
abort(); \ |
| 123 |
} \ |
| 124 |
} while (0) |
| 125 |
|
| 126 |
// Helper macro for binary operators; prints the two values on error |
| 127 |
// Don't use this macro directly in your code, use CHECK_EQ et al below |
| 128 |
|
| 129 |
// WARNING: These don't compile correctly if one of the arguments is a pointer |
| 130 |
// and the other is NULL. To work around this, simply static_cast NULL to the |
| 131 |
// type of the desired pointer. |
| 132 |
|
| 133 |
// TODO(jandrews): Also print the values in case of failure. Requires some |
| 134 |
// sort of type-sensitive ToString() function. |
| 135 |
#define CHECK_OP(op, val1, val2) \ |
| 136 |
do { \ |
| 137 |
if (!((val1) op (val2))) { \ |
| 138 |
fprintf(stderr, "%s:%d Check failed: %s %s %s\n", __FILE__, __LINE__, #val1, #op, #val2); \ |
| 139 |
abort(); \ |
| 140 |
} \ |
| 141 |
} while (0) |
| 142 |
|
| 143 |
#define CHECK_EQ(val1, val2) CHECK_OP(==, val1, val2) |
| 144 |
#define CHECK_NE(val1, val2) CHECK_OP(!=, val1, val2) |
| 145 |
#define CHECK_LE(val1, val2) CHECK_OP(<=, val1, val2) |
| 146 |
#define CHECK_LT(val1, val2) CHECK_OP(< , val1, val2) |
| 147 |
#define CHECK_GE(val1, val2) CHECK_OP(>=, val1, val2) |
| 148 |
#define CHECK_GT(val1, val2) CHECK_OP(> , val1, val2) |
| 149 |
|
| 150 |
// Synonyms for CHECK_* that are used in some unittests. |
| 151 |
#define EXPECT_EQ(val1, val2) CHECK_EQ(val1, val2) |
| 152 |
#define EXPECT_NE(val1, val2) CHECK_NE(val1, val2) |
| 153 |
#define EXPECT_LE(val1, val2) CHECK_LE(val1, val2) |
| 154 |
#define EXPECT_LT(val1, val2) CHECK_LT(val1, val2) |
| 155 |
#define EXPECT_GE(val1, val2) CHECK_GE(val1, val2) |
| 156 |
#define EXPECT_GT(val1, val2) CHECK_GT(val1, val2) |
| 157 |
#define ASSERT_EQ(val1, val2) EXPECT_EQ(val1, val2) |
| 158 |
#define ASSERT_NE(val1, val2) EXPECT_NE(val1, val2) |
| 159 |
#define ASSERT_LE(val1, val2) EXPECT_LE(val1, val2) |
| 160 |
#define ASSERT_LT(val1, val2) EXPECT_LT(val1, val2) |
| 161 |
#define ASSERT_GE(val1, val2) EXPECT_GE(val1, val2) |
| 162 |
#define ASSERT_GT(val1, val2) EXPECT_GT(val1, val2) |
| 163 |
// As are these variants. |
| 164 |
#define EXPECT_TRUE(cond) CHECK(cond) |
| 165 |
#define EXPECT_FALSE(cond) CHECK(!(cond)) |
| 166 |
#define EXPECT_STREQ(a, b) CHECK(strcmp(a, b) == 0) |
| 167 |
#define ASSERT_TRUE(cond) EXPECT_TRUE(cond) |
| 168 |
#define ASSERT_FALSE(cond) EXPECT_FALSE(cond) |
| 169 |
#define ASSERT_STREQ(a, b) EXPECT_STREQ(a, b) |
| 170 |
|
| 171 |
// Used for (libc) functions that return -1 and set errno |
| 172 |
#define CHECK_ERR(invocation) PCHECK((invocation) != -1) |
| 173 |
|
| 174 |
// A few more checks that only happen in debug mode |
| 175 |
#ifdef NDEBUG |
| 176 |
#define DCHECK_EQ(val1, val2) |
| 177 |
#define DCHECK_NE(val1, val2) |
| 178 |
#define DCHECK_LE(val1, val2) |
| 179 |
#define DCHECK_LT(val1, val2) |
| 180 |
#define DCHECK_GE(val1, val2) |
| 181 |
#define DCHECK_GT(val1, val2) |
| 182 |
#else |
| 183 |
#define DCHECK_EQ(val1, val2) CHECK_EQ(val1, val2) |
| 184 |
#define DCHECK_NE(val1, val2) CHECK_NE(val1, val2) |
| 185 |
#define DCHECK_LE(val1, val2) CHECK_LE(val1, val2) |
| 186 |
#define DCHECK_LT(val1, val2) CHECK_LT(val1, val2) |
| 187 |
#define DCHECK_GE(val1, val2) CHECK_GE(val1, val2) |
| 188 |
#define DCHECK_GT(val1, val2) CHECK_GT(val1, val2) |
| 189 |
#endif |
| 190 |
|
| 191 |
|
| 192 |
#ifdef ERROR |
| 193 |
#undef ERROR // may conflict with ERROR macro on windows |
| 194 |
#endif |
| 195 |
enum LogSeverity {INFO = -1, WARNING = -2, ERROR = -3, FATAL = -4}; |
| 196 |
|
| 197 |
// NOTE: we add a newline to the end of the output if it's not there already |
| 198 |
inline void LogPrintf(int severity, const char* pat, va_list ap) { |
| 199 |
// We write directly to the stderr file descriptor and avoid FILE |
| 200 |
// buffering because that may invoke malloc() |
| 201 |
char buf[600]; |
| 202 |
perftools_vsnprintf(buf, sizeof(buf)-1, pat, ap); |
| 203 |
if (buf[0] != '\0' && buf[strlen(buf)-1] != '\n') { |
| 204 |
assert(strlen(buf)+1 < sizeof(buf)); |
| 205 |
strcat(buf, "\n"); |
| 206 |
} |
| 207 |
WRITE_TO_STDERR(buf, strlen(buf)); |
| 208 |
if ((severity) == FATAL) |
| 209 |
abort(); // LOG(FATAL) indicates a big problem, so don't run atexit() calls |
| 210 |
} |
| 211 |
|
| 212 |
// Note that since the order of global constructors is unspecified, |
| 213 |
// global code that calls RAW_LOG may execute before FLAGS_verbose is set. |
| 214 |
// Such code will run with verbosity == 0 no matter what. |
| 215 |
#define VLOG_IS_ON(severity) (FLAGS_verbose >= severity) |
| 216 |
|
| 217 |
// In a better world, we'd use __VA_ARGS__, but VC++ 7 doesn't support it. |
| 218 |
#define LOG_PRINTF(severity, pat) do { \ |
| 219 |
if (VLOG_IS_ON(severity)) { \ |
| 220 |
va_list ap; \ |
| 221 |
va_start(ap, pat); \ |
| 222 |
LogPrintf(severity, pat, ap); \ |
| 223 |
va_end(ap); \ |
| 224 |
} \ |
| 225 |
} while (0) |
| 226 |
|
| 227 |
// RAW_LOG is the main function; some synonyms are used in unittests. |
| 228 |
inline void RAW_LOG(int lvl, const char* pat, ...) { LOG_PRINTF(lvl, pat); } |
| 229 |
inline void RAW_VLOG(int lvl, const char* pat, ...) { LOG_PRINTF(lvl, pat); } |
| 230 |
inline void LOG(int lvl, const char* pat, ...) { LOG_PRINTF(lvl, pat); } |
| 231 |
inline void VLOG(int lvl, const char* pat, ...) { LOG_PRINTF(lvl, pat); } |
| 232 |
inline void LOG_IF(int lvl, bool cond, const char* pat, ...) { |
| 233 |
if (cond) LOG_PRINTF(lvl, pat); |
| 234 |
} |
| 235 |
|
| 236 |
// This isn't technically logging, but it's also IO and also is an |
| 237 |
// attempt to be "raw" -- that is, to not use any higher-level libc |
| 238 |
// routines that might allocate memory or (ideally) try to allocate |
| 239 |
// locks. We use an opaque file handle (not necessarily an int) |
| 240 |
// to allow even more low-level stuff in the future. |
| 241 |
// Like other "raw" routines, these functions are best effort, and |
| 242 |
// thus don't return error codes (except RawOpenForWriting()). |
| 243 |
#if defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) |
| 244 |
#ifndef NOMINMAX |
| 245 |
#define NOMINMAX // @#!$& windows |
| 246 |
#endif |
| 247 |
#include <windows.h> |
| 248 |
typedef HANDLE RawFD; |
| 249 |
const RawFD kIllegalRawFD = INVALID_HANDLE_VALUE; |
| 250 |
#else |
| 251 |
typedef int RawFD; |
| 252 |
const RawFD kIllegalRawFD = -1; // what open returns if it fails |
| 253 |
#endif // defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) |
| 254 |
|
| 255 |
RawFD RawOpenForWriting(const char* filename); // uses default permissions |
| 256 |
void RawWrite(RawFD fd, const char* buf, size_t len); |
| 257 |
void RawClose(RawFD fd); |
| 258 |
|
| 259 |
#endif // _LOGGING_H_ |