logging.h revision 79397c21138f54fcff6ec067b44b847f1f7e0e98
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Ceres Solver - A fast non-linear least squares minimizer
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2013 Google Inc. All rights reserved.
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// http://code.google.com/p/ceres-solver/
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// modification, are permitted provided that the following conditions are met:
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Redistributions of source code must retain the above copyright notice,
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   this list of conditions and the following disclaimer.
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Redistributions in binary form must reproduce the above copyright notice,
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   this list of conditions and the following disclaimer in the documentation
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   and/or other materials provided with the distribution.
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Neither the name of Google Inc. nor the names of its contributors may be
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   used to endorse or promote products derived from this software without
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   specific prior written permission.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// POSSIBILITY OF SUCH DAMAGE.
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Author: settinger@google.com (Scott Ettinger)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//         mierle@gmail.com (Keir Mierle)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Simplified Glog style logging with Android support. Supported macros in
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// decreasing severity level per line:
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   VLOG(2), VLOG(N)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   VLOG(1),
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LOG(INFO), VLOG(0), LG
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LOG(WARNING),
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LOG(ERROR),
40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//   LOG(FATAL),
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// With VLOG(n), the output is directed to one of the 5 Android log levels:
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//   2 - Verbose
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//   1 - Debug
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   0 - Info
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  -1 - Warning
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  -2 - Error
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//  -3 - Fatal
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Any logging of level 2 and above is directed to the Verbose level. All
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Android log output is tagged with the string "native".
53eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//
54eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// If the symbol ANDROID is not defined, all output goes to std::cerr.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This allows code to be built on a different system for debug.
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Portions of this code are taken from the GLOG package.  This code is only a
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// small subset of the GLOG functionality. Notable differences from GLOG
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// behavior include lack of support for displaying unprintable characters and
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// lack of stack trace information upon failure of the CHECK macros.  On
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// non-Android systems, log output goes to std::cerr and is not written to a
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// file.
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CHECK macros are defined to test for conditions within code.  Any CHECK that
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// fails will log the failure and terminate the application.
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// e.g. CHECK_GE(3, 2) will pass while CHECK_GE(3, 4) will fail after logging
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//      "Check failed 3 >= 4".
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The following CHECK macros are defined:
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   CHECK(condition)        - fails if condition is false and logs condition.
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   CHECK_NOTNULL(variable) - fails if the variable is NULL.
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The following binary check macros are also defined :
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   Macro                     Operator equivalent
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   --------------------      -------------------
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   CHECK_EQ(val1, val2)      val1 == val2
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   CHECK_NE(val1, val2)      val1 != val2
80eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//   CHECK_GT(val1, val2)      val1 > val2
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   CHECK_GE(val1, val2)      val1 >= val2
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   CHECK_LT(val1, val2)      val1 < val2
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   CHECK_LE(val1, val2)      val1 <= val2
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Debug only versions of all of the check macros are also defined.  These
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// macros generate no code in a release build, but avoid unused variable
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// warnings / errors.
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// To use the debug only versions, prepend a D to the normal check macros, e.g.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DCHECK_EQ(a, b).
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CERCES_INTERNAL_MINIGLOG_GLOG_LOGGING_H_
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CERCES_INTERNAL_MINIGLOG_GLOG_LOGGING_H_
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef ANDROID
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#  include <android/log.h>
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // ANDROID
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <ctime>
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <fstream>
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <iostream>
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sstream>
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <vector>
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// For appropriate definition of CERES_EXPORT macro.
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ceres/internal/port.h"
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ceres/internal/disable_warnings.h"
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Log severity level constants.
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int FATAL   = -3;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int ERROR   = -2;
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int WARNING = -1;
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int INFO    =  0;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ------------------------- Glog compatibility ------------------------------
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace google {
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)typedef int LogSeverity;
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int INFO    = ::INFO;
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int WARNING = ::WARNING;
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int ERROR   = ::ERROR;
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int FATAL   = ::FATAL;
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Sink class used for integration with mock and test functions. If sinks are
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// added, all log output is also sent to each sink through the send function.
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// In this implementation, WaitTillSent() is called immediately after the send.
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This implementation is not thread safe.
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class CERES_EXPORT LogSink {
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~LogSink() {}
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void send(LogSeverity severity,
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    const char* full_filename,
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    const char* base_filename,
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    int line,
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    const struct tm* tm_time,
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    const char* message,
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    size_t message_len) = 0;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void WaitTillSent() = 0;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Global set of log sinks. The actual object is defined in logging.cc.
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern CERES_EXPORT std::set<LogSink *> log_sinks_global;
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline void InitGoogleLogging(char *argv) {
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Do nothing; this is ignored.
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note: the Log sink functions are not thread safe.
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline void AddLogSink(LogSink *sink) {
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(settinger): Add locks for thread safety.
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  log_sinks_global.insert(sink);
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline void RemoveLogSink(LogSink *sink) {
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  log_sinks_global.erase(sink);
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace google
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ---------------------------- Logger Class --------------------------------
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Class created for each use of the logging macros.
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The logger acts as a stream and routes the final stream contents to the
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Android logcat output at the proper filter level.  If ANDROID is not
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// defined, output is directed to std::cerr.  This class should not
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// be directly instantiated in code, rather it should be invoked through the
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// use of the log macros LG, LOG, or VLOG.
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CERES_EXPORT MessageLogger {
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MessageLogger(const char *file, int line, const char *tag, int severity)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : file_(file), line_(line), tag_(tag), severity_(severity) {
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Pre-pend the stream with the file and line number.
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    StripBasename(std::string(file), &filename_only_);
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    stream_ << filename_only_ << ":" << line << " ";
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Output the contents of the stream to the proper channel on destruction.
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~MessageLogger() {
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    stream_ << "\n";
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef ANDROID
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static const int android_log_levels[] = {
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ANDROID_LOG_FATAL,    // LOG(FATAL)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ANDROID_LOG_ERROR,    // LOG(ERROR)
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ANDROID_LOG_WARN,     // LOG(WARNING)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ANDROID_LOG_INFO,     // LOG(INFO), LG, VLOG(0)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ANDROID_LOG_DEBUG,    // VLOG(1)
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ANDROID_LOG_VERBOSE,  // VLOG(2) .. VLOG(N)
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    };
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Bound the logging level.
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const int kMaxVerboseLevel = 2;
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int android_level_index = std::min(std::max(FATAL, severity_),
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       kMaxVerboseLevel) - FATAL;
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int android_log_level = android_log_levels[android_level_index];
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Output the log string the Android log at the appropriate level.
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    __android_log_write(android_log_level, tag_.c_str(), stream_.str().c_str());
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Indicate termination if needed.
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (severity_ == FATAL) {
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      __android_log_write(ANDROID_LOG_FATAL,
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          tag_.c_str(),
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          "terminating.\n");
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // If not building on Android, log all output to std::cerr.
211eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    std::cerr << stream_.str();
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // ANDROID
213eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
214eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    LogToSinks(severity_);
215eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    WaitForSinks();
216eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
217eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // Android logging at level FATAL does not terminate execution, so abort()
218eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // is still required to stop the program.
219eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (severity_ == FATAL) {
220eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      abort();
221eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
222eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
223eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
224eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Return the stream associated with the logger object.
2255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::stringstream &stream() { return stream_; }
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void LogToSinks(int severity) {
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    time_t rawtime;
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    time (&rawtime);
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    struct tm* timeinfo;
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // On Windows, use secure localtime_s not localtime.
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    struct tm windows_timeinfo;
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    timeinfo = &windows_timeinfo;
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    localtime_s(timeinfo, &rawtime);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
239    timeinfo = localtime(&rawtime);
240#endif
241
242    std::set<google::LogSink*>::iterator iter;
243    // Send the log message to all sinks.
244    for (iter = google::log_sinks_global.begin();
245         iter != google::log_sinks_global.end(); ++iter) {
246      (*iter)->send(severity, file_.c_str(), filename_only_.c_str(), line_,
247                    timeinfo, stream_.str().c_str(), stream_.str().size());
248    }
249  }
250
251  void WaitForSinks() {
252    // TODO(settinger): Add locks for thread safety.
253    std::set<google::LogSink *>::iterator iter;
254
255    // Call WaitTillSent() for all sinks.
256    for (iter = google::log_sinks_global.begin();
257         iter != google::log_sinks_global.end(); ++iter) {
258      (*iter)->WaitTillSent();
259    }
260  }
261
262  void StripBasename(const std::string &full_path, std::string *filename) {
263    // TODO(settinger): Add support for OSs with different path separators.
264    const char kSeparator = '/';
265    size_t pos = full_path.rfind(kSeparator);
266    if (pos != std::string::npos) {
267      *filename = full_path.substr(pos + 1, std::string::npos);
268    } else {
269      *filename = full_path;
270    }
271  }
272
273  std::string file_;
274  std::string filename_only_;
275  int line_;
276  std::string tag_;
277  std::stringstream stream_;
278  int severity_;
279};
280
281// ---------------------- Logging Macro definitions --------------------------
282
283// This class is used to explicitly ignore values in the conditional
284// logging macros.  This avoids compiler warnings like "value computed
285// is not used" and "statement has no effect".
286class CERES_EXPORT LoggerVoidify {
287 public:
288  LoggerVoidify() { }
289  // This has to be an operator with a precedence lower than << but
290  // higher than ?:
291  void operator&(const std::ostream &s) { }
292};
293
294// Log only if condition is met.  Otherwise evaluates to void.
295#define LOG_IF(severity, condition) \
296    !(condition) ? (void) 0 : LoggerVoidify() & \
297      MessageLogger((char *)__FILE__, __LINE__, "native", severity).stream()
298
299// Log only if condition is NOT met.  Otherwise evaluates to void.
300#define LOG_IF_FALSE(severity, condition) LOG_IF(severity, !(condition))
301
302// LG is a convenient shortcut for LOG(INFO). Its use is in new
303// google3 code is discouraged and the following shortcut exists for
304// backward compatibility with existing code.
305#ifdef MAX_LOG_LEVEL
306#  define LOG(n)  LOG_IF(n, n <= MAX_LOG_LEVEL)
307#  define VLOG(n) LOG_IF(n, n <= MAX_LOG_LEVEL)
308#  define LG      LOG_IF(INFO, INFO <= MAX_LOG_LEVEL)
309#  define VLOG_IF(n, condition) LOG_IF(n, (n <= MAX_LOG_LEVEL) && condition)
310#else
311#  define LOG(n)  MessageLogger((char *)__FILE__, __LINE__, "native", n).stream()    // NOLINT
312#  define VLOG(n) MessageLogger((char *)__FILE__, __LINE__, "native", n).stream()    // NOLINT
313#  define LG      MessageLogger((char *)__FILE__, __LINE__, "native", INFO).stream() // NOLINT
314#  define VLOG_IF(n, condition) LOG_IF(n, condition)
315#endif
316
317// Currently, VLOG is always on for levels below MAX_LOG_LEVEL.
318#ifndef MAX_LOG_LEVEL
319#  define VLOG_IS_ON(x) (1)
320#else
321#  define VLOG_IS_ON(x) (x <= MAX_LOG_LEVEL)
322#endif
323
324#ifndef NDEBUG
325#  define DLOG LOG
326#else
327#  define DLOG(severity) true ? (void) 0 : LoggerVoidify() & \
328      MessageLogger((char *)__FILE__, __LINE__, "native", severity).stream()
329#endif
330
331
332// Log a message and terminate.
333template<class T>
334void LogMessageFatal(const char *file, int line, const T &message) {
335  MessageLogger((char *)__FILE__, __LINE__, "native", FATAL).stream()
336      << message;
337}
338
339// ---------------------------- CHECK macros ---------------------------------
340
341// Check for a given boolean condition.
342#define CHECK(condition) LOG_IF_FALSE(FATAL, condition) \
343        << "Check failed: " #condition " "
344
345#ifndef NDEBUG
346// Debug only version of CHECK
347#  define DCHECK(condition) LOG_IF_FALSE(FATAL, condition) \
348          << "Check failed: " #condition " "
349#else
350// Optimized version - generates no code.
351#  define DCHECK(condition) if (false) LOG_IF_FALSE(FATAL, condition) \
352          << "Check failed: " #condition " "
353#endif  // NDEBUG
354
355// ------------------------- CHECK_OP macros ---------------------------------
356
357// Generic binary operator check macro. This should not be directly invoked,
358// instead use the binary comparison macros defined below.
359#define CHECK_OP(val1, val2, op) LOG_IF_FALSE(FATAL, ((val1) op (val2))) \
360  << "Check failed: " #val1 " " #op " " #val2 " "
361
362// Check_op macro definitions
363#define CHECK_EQ(val1, val2) CHECK_OP(val1, val2, ==)
364#define CHECK_NE(val1, val2) CHECK_OP(val1, val2, !=)
365#define CHECK_LE(val1, val2) CHECK_OP(val1, val2, <=)
366#define CHECK_LT(val1, val2) CHECK_OP(val1, val2, <)
367#define CHECK_GE(val1, val2) CHECK_OP(val1, val2, >=)
368#define CHECK_GT(val1, val2) CHECK_OP(val1, val2, >)
369
370#ifndef NDEBUG
371// Debug only versions of CHECK_OP macros.
372#  define DCHECK_EQ(val1, val2) CHECK_OP(val1, val2, ==)
373#  define DCHECK_NE(val1, val2) CHECK_OP(val1, val2, !=)
374#  define DCHECK_LE(val1, val2) CHECK_OP(val1, val2, <=)
375#  define DCHECK_LT(val1, val2) CHECK_OP(val1, val2, <)
376#  define DCHECK_GE(val1, val2) CHECK_OP(val1, val2, >=)
377#  define DCHECK_GT(val1, val2) CHECK_OP(val1, val2, >)
378#else
379// These versions generate no code in optimized mode.
380#  define DCHECK_EQ(val1, val2) if (false) CHECK_OP(val1, val2, ==)
381#  define DCHECK_NE(val1, val2) if (false) CHECK_OP(val1, val2, !=)
382#  define DCHECK_LE(val1, val2) if (false) CHECK_OP(val1, val2, <=)
383#  define DCHECK_LT(val1, val2) if (false) CHECK_OP(val1, val2, <)
384#  define DCHECK_GE(val1, val2) if (false) CHECK_OP(val1, val2, >=)
385#  define DCHECK_GT(val1, val2) if (false) CHECK_OP(val1, val2, >)
386#endif  // NDEBUG
387
388// ---------------------------CHECK_NOTNULL macros ---------------------------
389
390// Helpers for CHECK_NOTNULL(). Two are necessary to support both raw pointers
391// and smart pointers.
392template <typename T>
393T& CheckNotNullCommon(const char *file, int line, const char *names, T& t) {
394  if (t == NULL) {
395    LogMessageFatal(file, line, std::string(names));
396  }
397  return t;
398}
399
400template <typename T>
401T* CheckNotNull(const char *file, int line, const char *names, T* t) {
402  return CheckNotNullCommon(file, line, names, t);
403}
404
405template <typename T>
406T& CheckNotNull(const char *file, int line, const char *names, T& t) {
407  return CheckNotNullCommon(file, line, names, t);
408}
409
410// Check that a pointer is not null.
411#define CHECK_NOTNULL(val) \
412  CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
413
414#ifndef NDEBUG
415// Debug only version of CHECK_NOTNULL
416#define DCHECK_NOTNULL(val) \
417  CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
418#else
419// Optimized version - generates no code.
420#define DCHECK_NOTNULL(val) if (false)\
421  CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
422#endif  // NDEBUG
423
424#include "ceres/internal/reenable_warnings.h"
425
426#endif  // CERCES_INTERNAL_MINIGLOG_GLOG_LOGGING_H_
427