aboutsummaryrefslogtreecommitdiff
path: root/internal/ceres/miniglog/glog/logging.h
blob: bab31919d863d60439c1c8f200bd54fc025c8cf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright 2011 Google Inc. All Rights Reserved.
// Author: settinger@google.com (Scott Ettinger)

// Simplified Google3 style logging with Android support.
// Supported macros are : LOG(INFO), LOG(WARNING), LOG(ERROR), LOG(FATAL),
//                        and VLOG(n).
//
// Portions of this code are taken from the GLOG package.  This code
// is only a small subset of the GLOG functionality. And like GLOG,
// higher levels are more verbose.
//
// Notable differences from GLOG :
//
// 1. lack of support for displaying unprintable characters and lack
// of stack trace information upon failure of the CHECK macros.
// 2. All output is tagged with the string "native".
// 3. While there is no runtime flag filtering logs (-v, -vmodule), the
//    compile time define MAX_LOG_LEVEL can be used to silence any
//    logging above the given level.
//
// -------------------------------- Usage ------------------------------------
// Basic usage :
// LOG(<severity level>) acts as a c++ stream to the Android logcat output.
// e.g. LOG(INFO) << "Value of counter = " << counter;
//
// Valid severity levels include INFO, WARNING, ERROR, FATAL.
// The various severity levels are routed to the corresponding Android logcat
// output.
// LOG(FATAL) outputs to the log and then terminates.
//
// VLOG(<severity level>) can also be used.
// VLOG(n) output is directed to the Android logcat levels as follows :
//  >=2 - Verbose
//    1 - Debug
//    0 - Info
//   -1 - Warning
//   -2 - Error
// <=-3 - Fatal
// Note that VLOG(FATAL) will terminate the program.
//
// CHECK macros are defined to test for conditions within code.  Any CHECK
// that fails will log the failure and terminate the application.
// e.g. CHECK_GE(3, 2) will pass while CHECK_GE(3, 4) will fail after logging
//      "Check failed 3 >= 4".
// The following CHECK macros are defined :
//
// CHECK(condition) - fails if condition is false and logs condition.
// CHECK_NOTNULL(variable) - fails if the variable is NULL.
//
// The following binary check macros are also defined :
//    Macro                 operator applied
// ------------------------------------------
// CHECK_EQ(val1, val2)      val1 == val2
// CHECK_NE(val1, val2)      val1 != val2
// CHECK_GT(val1, val2)      val1 > val2
// CHECK_GE(val1, val2)      val1 >= val2
// CHECK_LT(val1, val2)      val1 < val2
// CHECK_LE(val1, val2)      val1 <= val2
//
// Debug only versions of all of the check macros are also defined.  These
// macros generate no code in a release build, but avoid unused variable
// warnings / errors.
// To use the debug only versions, Prepend a D to the normal check macros.
// e.g. DCHECK_EQ(a, b);

#ifndef MOBILE_BASE_LOGGING_H_
#define MOBILE_BASE_LOGGING_H_

// Definitions for building on an Android system.
#include <android/log.h>
#include <time.h>

#include <algorithm>
#include <iostream>
#include <string>
#include <fstream>
#include <set>
#include <sstream>
#include <vector>

// Log severity level constants.
const int FATAL   = -3;
const int ERROR   = -2;
const int WARNING = -1;
const int INFO    =  0;

// ------------------------- Glog compatibility ------------------------------

namespace google {

typedef int LogSeverity;
const int INFO    = ::INFO;
const int WARNING = ::WARNING;
const int ERROR   = ::ERROR;
const int FATAL   = ::FATAL;

#ifdef ENABLE_LOG_SINKS

// Sink class used for integration with mock and test functions.
// If sinks are added, all log output is also sent to each sink through
// the send function.  In this implementation, WaitTillSent() is called
// immediately after the send.
// This implementation is not thread safe.
class LogSink {
 public:
  virtual ~LogSink() {}
  virtual void send(LogSeverity severity, const char* full_filename,
                    const char* base_filename, int line,
                    const struct tm* tm_time,
                    const char* message, size_t message_len) = 0;
  virtual void WaitTillSent() = 0;
};

// Global set of log sinks.
// TODO(settinger): Move this into a .cc file.
static std::set<LogSink *> log_sinks_global;

// Note: the Log sink functions are not thread safe.
inline void AddLogSink(LogSink *sink) {
  // TODO(settinger): Add locks for thread safety.
  log_sinks_global.insert(sink);
}
inline void RemoveLogSink(LogSink *sink) {
  log_sinks_global.erase(sink);
}

#endif  // #ifdef ENABLE_LOG_SINKS

inline void InitGoogleLogging(char *argv) {}

}  // namespace google

// ---------------------------- Logger Class --------------------------------

// Class created for each use of the logging macros.
// The logger acts as a stream and routes the final stream contents to the
// Android logcat output at the proper filter level.  This class should not
// be directly instantiated in code, rather it should be invoked through the
// use of the log macros LOG, or VLOG.
class MessageLogger {
 public:
  MessageLogger(const char *file, int line, const char *tag, int severity)
    : file_(file), line_(line), tag_(tag), severity_(severity) {
    // Pre-pend the stream with the file and line number.
    StripBasename(std::string(file), &filename_only_);
    stream_ << filename_only_ << ":" << line << " ";
  }

  // Output the contents of the stream to the proper channel on destruction.
  ~MessageLogger() {
#ifdef MAX_LOG_LEVEL
    if (severity_ > MAX_LOG_LEVEL && severity_ > FATAL) {
      return;
    }
#endif
    stream_ << "\n";
    static const int android_log_levels[] = {
        ANDROID_LOG_FATAL,    // LOG(FATAL)
        ANDROID_LOG_ERROR,    // LOG(ERROR)
        ANDROID_LOG_WARN,     // LOG(WARNING)
        ANDROID_LOG_INFO,     // LOG(INFO), VLOG(0)
        ANDROID_LOG_DEBUG,    // VLOG(1)
        ANDROID_LOG_VERBOSE,  // VLOG(2) .. VLOG(N)
    };

    // Bound the logging level.
    const int kMaxVerboseLevel = 2;
    int android_level_index = std::min(std::max(FATAL, severity_),
                                       kMaxVerboseLevel) - FATAL;
    int android_log_level = android_log_levels[android_level_index];

    // Output the log string the Android log at the appropriate level.
    __android_log_write(android_log_level, tag_.c_str(), stream_.str().c_str());

    // Indicate termination if needed.
    if (severity_ == FATAL) {
      __android_log_write(ANDROID_LOG_FATAL,
                          tag_.c_str(),
                          "terminating.\n");
    }

#ifdef ENABLE_LOG_SINKS

    LogToSinks(severity_);
    WaitForSinks();

#endif  // #ifdef ENABLE_LOG_SINKS

    // Android logging at level FATAL does not terminate execution, so abort()
    // is still required to stop the program.
    if (severity_ == FATAL) {
      abort();
    }
  }

  // Return the stream associated with the logger object.
  std::stringstream &stream() { return stream_; }

 private:
#ifdef ENABLE_LOG_SINKS

  void LogToSinks(int severity) {
    time_t rawtime;
    struct tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    std::set<google::LogSink *>::iterator iter;
    // Send the log message to all sinks.
    for (iter = google::log_sinks_global.begin();
         iter != google::log_sinks_global.end(); ++iter)
      (*iter)->send(severity, file_.c_str(), filename_only_.c_str(), line_,
                    timeinfo, stream_.str().c_str(), stream_.str().size());
  }

  void WaitForSinks() {
    // TODO(settinger): add locks for thread safety.
    std::set<google::LogSink *>::iterator iter;
    // Call WaitTillSent() for all sinks.
    for (iter = google::log_sinks_global.begin();
         iter != google::log_sinks_global.end(); ++iter)
      (*iter)->WaitTillSent();
  }

#endif // #ifdef ENABLE_LOG_SINKS

  void StripBasename(const std::string &full_path, std::string *filename) {
    // TODO(settinger): add support for OS with different path separators.
    const char kSeparator = '/';
    size_t pos = full_path.rfind(kSeparator);
    if (pos != std::string::npos)
      *filename = full_path.substr(pos + 1, std::string::npos);
    else
      *filename = full_path;
  }

  std::string file_;
  std::string filename_only_;
  int line_;
  std::string tag_;
  std::stringstream stream_;
  int severity_;
};

// ---------------------- Logging Macro definitions --------------------------

// This class is used to explicitly ignore values in the conditional
// logging macros.  This avoids compiler warnings like "value computed
// is not used" and "statement has no effect".
class LoggerVoidify {
 public:
  LoggerVoidify() { }
  // This has to be an operator with a precedence lower than << but
  // higher than ?:
  void operator&(const std::ostream &s) { }
};

// Log only if condition is met.  Otherwise evaluates to void.
#define LOG_IF(severity, condition) \
  !(condition) ? (void) 0 : LoggerVoidify() & \
    MessageLogger((char *)__FILE__, __LINE__, "native", severity).stream()

// Log only if condition is NOT met.  Otherwise evaluates to void.
#define LOG_IF_FALSE(severity, condition) LOG_IF(severity, !(condition))

// LG is a convenient shortcut for LOG(INFO). Its use is in new
// google3 code is discouraged and the following shortcut exists for
// backward compatibility with existing code.
#ifdef MAX_LOG_LEVEL
#define LOG(n) LOG_IF(n, n <= MAX_LOG_LEVEL)
#define VLOG(n) LOG_IF(n, n <= MAX_LOG_LEVEL)
#define LG LOG_IF(INFO, INFO <= MAX_LOG_LEVEL)
#else
#define LOG(n) MessageLogger((char *)__FILE__, __LINE__, "native", n).stream()
#define VLOG(n) MessageLogger((char *)__FILE__, __LINE__, "native", n).stream()
#define LG MessageLogger((char *)__FILE__, __LINE__, "native", INFO).stream()
#endif

// Currently, VLOG is always on for levels below MAX_LOG_LEVEL.
#ifndef MAX_LOG_LEVEL
#define VLOG_IS_ON(x) (1)
#else
#define VLOG_IS_ON(x) (x <= MAX_LOG_LEVEL)
#endif

#ifndef NDEBUG
#define DLOG LOG
#else
#define DLOG(severity) true ? (void) 0 : LoggerVoidify() & \
    MessageLogger((char *)__FILE__, __LINE__, "native", severity).stream()
#endif

// ---------------------------- CHECK helpers --------------------------------

// Log a message and terminate.
template<class T>
void LogMessageFatal(const char *file, int line, const T &message) {
  MessageLogger((char *)__FILE__, __LINE__, "native", FATAL).stream()
      << message;
}

// ---------------------------- CHECK macros ---------------------------------

// Check for a given boolean condition.
#define CHECK(condition) LOG_IF_FALSE(FATAL, condition) \
        << "Check failed: " #condition " "

#ifndef NDEBUG
// Debug only version of CHECK
#define DCHECK(condition) LOG_IF_FALSE(FATAL, condition) \
        << "Check failed: " #condition " "
#else
// Optimized version - generates no code.
#define DCHECK(condition) if (false) LOG_IF_FALSE(FATAL, condition) \
        << "Check failed: " #condition " "
#endif  // NDEBUG

// ------------------------- CHECK_OP macros ---------------------------------

// Generic binary operator check macro. This should not be directly invoked,
// instead use the binary comparison macros defined below.
#define CHECK_OP(val1, val2, op) LOG_IF_FALSE(FATAL, (val1 op val2)) \
  << "Check failed: " #val1 " " #op " " #val2 " "

// Check_op macro definitions
#define CHECK_EQ(val1, val2) CHECK_OP(val1, val2, ==)
#define CHECK_NE(val1, val2) CHECK_OP(val1, val2, !=)
#define CHECK_LE(val1, val2) CHECK_OP(val1, val2, <=)
#define CHECK_LT(val1, val2) CHECK_OP(val1, val2, <)
#define CHECK_GE(val1, val2) CHECK_OP(val1, val2, >=)
#define CHECK_GT(val1, val2) CHECK_OP(val1, val2, >)

#ifndef NDEBUG
// Debug only versions of CHECK_OP macros.
#define DCHECK_EQ(val1, val2) CHECK_OP(val1, val2, ==)
#define DCHECK_NE(val1, val2) CHECK_OP(val1, val2, !=)
#define DCHECK_LE(val1, val2) CHECK_OP(val1, val2, <=)
#define DCHECK_LT(val1, val2) CHECK_OP(val1, val2, <)
#define DCHECK_GE(val1, val2) CHECK_OP(val1, val2, >=)
#define DCHECK_GT(val1, val2) CHECK_OP(val1, val2, >)
#else
// These versions generate no code in optimized mode.
#define DCHECK_EQ(val1, val2) if (false) CHECK_OP(val1, val2, ==)
#define DCHECK_NE(val1, val2) if (false) CHECK_OP(val1, val2, !=)
#define DCHECK_LE(val1, val2) if (false) CHECK_OP(val1, val2, <=)
#define DCHECK_LT(val1, val2) if (false) CHECK_OP(val1, val2, <)
#define DCHECK_GE(val1, val2) if (false) CHECK_OP(val1, val2, >=)
#define DCHECK_GT(val1, val2) if (false) CHECK_OP(val1, val2, >)
#endif  // NDEBUG

// ---------------------------CHECK_NOTNULL macros ---------------------------

// Helpers for CHECK_NOTNULL(). Two are necessary to support both raw pointers
// and smart pointers.
template <typename T>
T& CheckNotNullCommon(const char *file, int line, const char *names, T& t) {
  if (t == NULL) {
    LogMessageFatal(file, line, std::string(names));
  }
  return t;
}

template <typename T>
T* CheckNotNull(const char *file, int line, const char *names, T* t) {
  return CheckNotNullCommon(file, line, names, t);
}

template <typename T>
T& CheckNotNull(const char *file, int line, const char *names, T& t) {
  return CheckNotNullCommon(file, line, names, t);
}

// Check that a pointer is not null.
#define CHECK_NOTNULL(val) \
  CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))

#ifndef NDEBUG
// Debug only version of CHECK_NOTNULL
#define DCHECK_NOTNULL(val) \
  CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
#else
// Optimized version - generates no code.
#define DCHECK_NOTNULL(val) if (false)\
  CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
#endif  // NDEBUG

inline void PrintAndroid(const char *msg) {
  __android_log_write(ANDROID_LOG_VERBOSE, "native", msg);
}

#endif  // MOBILE_BASE_LOGGING_H_