~ubuntu-branches/ubuntu/jaunty/google-perftools/jaunty

« back to all changes in this revision

Viewing changes to src/base/logging.h

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2008-06-15 23:41:36 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20080615234136-al5gawvdvt5vhdtz
Tags: 0.98-1
* New upstream release. (Closes: #425147)
* Compiled with GCC 4.3. (Closes: #454841)
* debian/watch: can now report upstream's version (Closes: #450294)
* Because of a file conflict between tau and libgoogle-perftools the
  binary pprof is renamed as google-pprof. (Closes: #404001)
  Great thanks to Michael Mende.
* debian/rules: autoconf files are now generated at the build time.
* Bumped up Standards-Version to 3.7.3, no changes are required.
* Split a new package, libtcmallc_minimal0. The upstream supports
  this module for wider platforms. So I leave its architecture to be
  `any'.
* libgoogle-perftools0's architecture is now i386. The upstream
  supports this module for x86 and x86_64. However, x86_64 requires
  libunwind's development head, which Debian does not have yet.
* Removed an unnecessary patch, debian/patches/02_profiler.cc_alpha.diff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
#ifndef _LOGGING_H_
36
36
#define _LOGGING_H_
37
37
 
 
38
#include "config.h"
38
39
#include <stdarg.h>
 
40
#include <stdlib.h>
39
41
#include <stdio.h>
40
 
#include <stdlib.h>
 
42
#ifdef HAVE_UNISTD_H
 
43
#include <unistd.h>    // for write()
 
44
#endif
 
45
#include <string.h>    // for strlen()
 
46
#include <assert.h>
 
47
#include <errno.h>     // for errno
 
48
#include "base/commandlineflags.h"
 
49
 
 
50
// On some systems (like freebsd), we can't call write() at all in a
 
51
// global constructor, perhaps because errno hasn't been set up.
 
52
// Calling the write syscall is safer (it doesn't set errno), so we
 
53
// prefer that.  Note we don't care about errno for logging: we just
 
54
// do logging on a best-effort basis.
 
55
#ifdef HAVE_SYS_SYSCALL_H
 
56
#include <sys/syscall.h>
 
57
#define WRITE_TO_STDERR(buf, len) syscall(SYS_write, STDERR_FILENO, buf, len)
 
58
#else
 
59
#define WRITE_TO_STDERR(buf, len) write(STDERR_FILENO, buf, len)
 
60
#endif
 
61
 
 
62
 
 
63
// We log all messages at this log-level and below.
 
64
// INFO == -1, WARNING == -2, ERROR == -3, FATAL == -4
 
65
DECLARE_int32(verbose);
41
66
 
42
67
// CHECK dies with a fatal error if condition is not true.  It is *not*
43
68
// controlled by NDEBUG, so the check will be executed regardless of
44
69
// compilation mode.  Therefore, it is safe to do things like:
45
70
//    CHECK(fp->Write(x) == 4)
46
 
#define CHECK(condition)  \
47
 
  do { \
48
 
    if (!(condition)) { \
49
 
      fprintf(stderr, "Check failed: %s\n", #condition); \
50
 
      exit(1); \
51
 
    } \
52
 
  } while (0) \
 
71
// Note we use write instead of printf/puts to avoid the risk we'll
 
72
// call malloc().
 
73
#define CHECK(condition)                                                \
 
74
  do {                                                                  \
 
75
    if (!(condition)) {                                                 \
 
76
      WRITE_TO_STDERR("Check failed: " #condition "\n",                 \
 
77
                      sizeof("Check failed: " #condition "\n")-1);      \
 
78
      exit(1);                                                          \
 
79
    }                                                                   \
 
80
  } while (0)
 
81
 
 
82
// This takes a message to print.  The name is historical.
 
83
#define RAW_CHECK(condition, message)                                          \
 
84
  do {                                                                         \
 
85
    if (!(condition)) {                                                        \
 
86
      WRITE_TO_STDERR("Check failed: " #condition ": " message "\n",           \
 
87
                      sizeof("Check failed: " #condition ": " message "\n")-1);\
 
88
      exit(1);                                                                 \
 
89
    }                                                                          \
 
90
  } while (0)
 
91
 
 
92
// This is like RAW_CHECK, but only in debug-mode
 
93
#ifdef NDEBUG
 
94
enum { DEBUG_MODE = 0 };
 
95
#define RAW_DCHECK(condition, message)
 
96
#else
 
97
enum { DEBUG_MODE = 1 };
 
98
#define RAW_DCHECK(condition, message)  RAW_CHECK(condition, message)
 
99
#endif
 
100
 
 
101
// This prints errno as well.  Note we use write instead of printf/puts to
 
102
// avoid the risk we'll call malloc().
 
103
#define PCHECK(condition)                                               \
 
104
  do {                                                                  \
 
105
    if (!(condition)) {                                                 \
 
106
      const int err_no = errno;                                         \
 
107
      WRITE_TO_STDERR("Check failed: " #condition ": ",                 \
 
108
                      sizeof("Check failed: " #condition ": ")-1);      \
 
109
      WRITE_TO_STDERR(strerror(err_no), strlen(strerror(err_no)));      \
 
110
      WRITE_TO_STDERR("\n", sizeof("\n")-1);                            \
 
111
      exit(1);                                                          \
 
112
    }                                                                   \
 
113
  } while (0)
53
114
 
54
115
// Helper macro for binary operators; prints the two values on error
55
116
// Don't use this macro directly in your code, use CHECK_EQ et al below
60
121
 
61
122
// TODO(jandrews): Also print the values in case of failure.  Requires some
62
123
// sort of type-sensitive ToString() function.
63
 
#define CHECK_OP(op, val1, val2)  \
64
 
  do { \
65
 
    if (!((val1) op (val2))) { \
66
 
      fprintf(stderr, "Check failed: %s %s %s\n", #val1, #op, #val2); \
67
 
      exit(1); \
68
 
    } \
 
124
#define CHECK_OP(op, val1, val2)                                        \
 
125
  do {                                                                  \
 
126
    if (!((val1) op (val2))) {                                          \
 
127
      fprintf(stderr, "Check failed: %s %s %s\n", #val1, #op, #val2);   \
 
128
      exit(1);                                                          \
 
129
    }                                                                   \
69
130
  } while (0)
70
131
 
71
132
#define CHECK_EQ(val1, val2) CHECK_OP(==, val1, val2)
75
136
#define CHECK_GE(val1, val2) CHECK_OP(>=, val1, val2)
76
137
#define CHECK_GT(val1, val2) CHECK_OP(> , val1, val2)
77
138
 
78
 
enum {INFO, WARNING, ERROR, FATAL, NUM_SEVERITIES};
79
 
 
80
 
inline void LogPrintf(int severity, const char* pat, ...) {
81
 
  va_list ap;
82
 
  va_start(ap, pat);
83
 
  vfprintf(stderr, pat, ap);
84
 
  va_end(ap);
85
 
  fprintf(stderr, "\n");
 
139
// Used for (libc) functions that return -1 and set errno
 
140
#define CHECK_ERR(invocation)  PCHECK((invocation) != -1)
 
141
 
 
142
// A few more checks that only happen in debug mode
 
143
#ifdef NDEBUG
 
144
#define DCHECK_EQ(val1, val2)
 
145
#define DCHECK_NE(val1, val2)
 
146
#define DCHECK_LE(val1, val2)
 
147
#define DCHECK_LT(val1, val2)
 
148
#define DCHECK_GE(val1, val2)
 
149
#define DCHECK_GT(val1, val2)
 
150
#else
 
151
#define DCHECK_EQ(val1, val2)  CHECK_EQ(val1, val2)
 
152
#define DCHECK_NE(val1, val2)  CHECK_NE(val1, val2)
 
153
#define DCHECK_LE(val1, val2)  CHECK_LE(val1, val2)
 
154
#define DCHECK_LT(val1, val2)  CHECK_LT(val1, val2)
 
155
#define DCHECK_GE(val1, val2)  CHECK_GE(val1, val2)
 
156
#define DCHECK_GT(val1, val2)  CHECK_GT(val1, val2)
 
157
#endif
 
158
 
 
159
 
 
160
#ifdef ERROR
 
161
#undef ERROR      // may conflict with ERROR macro on windows
 
162
#endif
 
163
enum LogSeverity {INFO = -1, WARNING = -2, ERROR = -3, FATAL = -4};
 
164
 
 
165
// NOTE: we add a newline to the end of the output if it's not there already
 
166
inline void LogPrintf(int severity, const char* pat, va_list ap) {
 
167
  // We write directly to the stderr file descriptor and avoid FILE
 
168
  // buffering because that may invoke malloc()
 
169
  char buf[600];
 
170
  vsnprintf(buf, sizeof(buf)-1, pat, ap);
 
171
  if (buf[0] != '\0' && buf[strlen(buf)-1] != '\n') {
 
172
    assert(strlen(buf)+1 < sizeof(buf));
 
173
    strcat(buf, "\n");
 
174
  }
 
175
  WRITE_TO_STDERR(buf, strlen(buf));
86
176
  if ((severity) == FATAL)
87
 
    exit(1);
 
177
    abort(); // LOG(FATAL) indicates a big problem, so don't run atexit() calls
 
178
}
 
179
 
 
180
// Note that since the order of global constructors is unspecified,
 
181
// global code that calls RAW_LOG may execute before FLAGS_verbose is set.
 
182
// Such code will run with verbosity == 0 no matter what.
 
183
#define VLOG_IS_ON(severity) (FLAGS_verbose >= severity)
 
184
 
 
185
// In a better world, we'd use __VA_ARGS__, but VC++ 7 doesn't support it.
 
186
#define LOG_PRINTF(severity, pat) do {          \
 
187
  if (VLOG_IS_ON(severity)) {                   \
 
188
    va_list ap;                                 \
 
189
    va_start(ap, pat);                          \
 
190
    LogPrintf(severity, pat, ap);               \
 
191
    va_end(ap);                                 \
 
192
  }                                             \
 
193
} while (0)
 
194
 
 
195
// RAW_LOG is the main function; some synonyms are used in unittests.
 
196
inline void RAW_LOG(int lvl, const char* pat, ...)  { LOG_PRINTF(lvl, pat); }
 
197
inline void RAW_VLOG(int lvl, const char* pat, ...) { LOG_PRINTF(lvl, pat); }
 
198
inline void LOG(int lvl, const char* pat, ...)      { LOG_PRINTF(lvl, pat); }
 
199
inline void VLOG(int lvl, const char* pat, ...)     { LOG_PRINTF(lvl, pat); }
 
200
inline void LOG_IF(int lvl, bool cond, const char* pat, ...) {
 
201
  if (cond)  LOG_PRINTF(lvl, pat);
88
202
}
89
203
 
90
204
#endif // _LOGGING_H_