179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// Ceres Solver - A fast non-linear least squares minimizer
279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// Copyright 2013 Google Inc. All rights reserved.
379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// http://code.google.com/p/ceres-solver/
479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//
579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// Redistribution and use in source and binary forms, with or without
679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// modification, are permitted provided that the following conditions are met:
779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//
879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// * Redistributions of source code must retain the above copyright notice,
979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   this list of conditions and the following disclaimer.
1079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// * Redistributions in binary form must reproduce the above copyright notice,
1179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   this list of conditions and the following disclaimer in the documentation
1279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   and/or other materials provided with the distribution.
1379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// * Neither the name of Google Inc. nor the names of its contributors may be
1479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   used to endorse or promote products derived from this software without
1579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   specific prior written permission.
1679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//
1779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// POSSIBILITY OF SUCH DAMAGE.
2879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//
290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: settinger@google.com (Scott Ettinger)
3079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//         mierle@gmail.com (Keir Mierle)
310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
3279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// Simplified Glog style logging with Android support. Supported macros in
3379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// decreasing severity level per line:
340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
3579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   VLOG(2), VLOG(N)
3679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   VLOG(1),
3779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   LOG(INFO), VLOG(0), LG
3879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   LOG(WARNING),
3979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   LOG(ERROR),
4079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   LOG(FATAL),
410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
4279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// With VLOG(n), the output is directed to one of the 5 Android log levels:
430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
4479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   2 - Verbose
4579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   1 - Debug
4679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   0 - Info
4779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//  -1 - Warning
4879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//  -2 - Error
4979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//  -3 - Fatal
500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
5179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// Any logging of level 2 and above is directed to the Verbose level. All
5279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// Android log output is tagged with the string "native".
530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
5479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// If the symbol ANDROID is not defined, all output goes to std::cerr.
5579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// This allows code to be built on a different system for debug.
560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
5779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// Portions of this code are taken from the GLOG package.  This code is only a
5879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// small subset of the GLOG functionality. Notable differences from GLOG
5979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// behavior include lack of support for displaying unprintable characters and
6079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// lack of stack trace information upon failure of the CHECK macros.  On
6179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// non-Android systems, log output goes to std::cerr and is not written to a
6279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// file.
6379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//
6479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// CHECK macros are defined to test for conditions within code.  Any CHECK that
6579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// fails will log the failure and terminate the application.
660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// e.g. CHECK_GE(3, 2) will pass while CHECK_GE(3, 4) will fail after logging
670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//      "Check failed 3 >= 4".
680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
6979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// The following CHECK macros are defined:
7079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//
7179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   CHECK(condition)        - fails if condition is false and logs condition.
7279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   CHECK_NOTNULL(variable) - fails if the variable is NULL.
730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// The following binary check macros are also defined :
7579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//
7679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   Macro                     Operator equivalent
7779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   --------------------      -------------------
7879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   CHECK_EQ(val1, val2)      val1 == val2
7979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   CHECK_NE(val1, val2)      val1 != val2
8079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   CHECK_GT(val1, val2)      val1 > val2
8179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   CHECK_GE(val1, val2)      val1 >= val2
8279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   CHECK_LT(val1, val2)      val1 < val2
8379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//   CHECK_LE(val1, val2)      val1 <= val2
840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Debug only versions of all of the check macros are also defined.  These
860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// macros generate no code in a release build, but avoid unused variable
870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// warnings / errors.
8879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez//
8979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// To use the debug only versions, prepend a D to the normal check macros, e.g.
9079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// DCHECK_EQ(a, b).
910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
9279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#ifndef CERCES_INTERNAL_MINIGLOG_GLOG_LOGGING_H_
9379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#define CERCES_INTERNAL_MINIGLOG_GLOG_LOGGING_H_
940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
9579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#ifdef ANDROID
9679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  include <android/log.h>
9779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#endif  // ANDROID
980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <algorithm>
10079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include <ctime>
1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <fstream>
10279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include <iostream>
1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <set>
1040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <sstream>
10579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include <string>
1060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <vector>
1070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
10879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// For appropriate definition of CERES_EXPORT macro.
10979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include "ceres/internal/port.h"
11079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include "ceres/internal/disable_warnings.h"
11179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
1120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Log severity level constants.
1130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongconst int FATAL   = -3;
1140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongconst int ERROR   = -2;
1150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongconst int WARNING = -1;
1160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongconst int INFO    =  0;
1170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ------------------------- Glog compatibility ------------------------------
1190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace google {
1210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongtypedef int LogSeverity;
1230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongconst int INFO    = ::INFO;
1240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongconst int WARNING = ::WARNING;
1250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongconst int ERROR   = ::ERROR;
1260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongconst int FATAL   = ::FATAL;
1270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
12879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// Sink class used for integration with mock and test functions. If sinks are
12979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// added, all log output is also sent to each sink through the send function.
13079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// In this implementation, WaitTillSent() is called immediately after the send.
1310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// This implementation is not thread safe.
13279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandezclass CERES_EXPORT LogSink {
1330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong public:
1340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  virtual ~LogSink() {}
13579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  virtual void send(LogSeverity severity,
13679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                    const char* full_filename,
13779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                    const char* base_filename,
13879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                    int line,
1390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                    const struct tm* tm_time,
14079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                    const char* message,
14179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                    size_t message_len) = 0;
1420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  virtual void WaitTillSent() = 0;
1430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
1440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
14579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// Global set of log sinks. The actual object is defined in logging.cc.
14679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandezextern CERES_EXPORT std::set<LogSink *> log_sinks_global;
14779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
14879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandezinline void InitGoogleLogging(char *argv) {
14979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // Do nothing; this is ignored.
15079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez}
1510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Note: the Log sink functions are not thread safe.
1530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Konginline void AddLogSink(LogSink *sink) {
1540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // TODO(settinger): Add locks for thread safety.
1550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  log_sinks_global.insert(sink);
1560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
1570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Konginline void RemoveLogSink(LogSink *sink) {
1580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  log_sinks_global.erase(sink);
1590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
1600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace google
1620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ---------------------------- Logger Class --------------------------------
1640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Class created for each use of the logging macros.
1660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// The logger acts as a stream and routes the final stream contents to the
16779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// Android logcat output at the proper filter level.  If ANDROID is not
16879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// defined, output is directed to std::cerr.  This class should not
1690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// be directly instantiated in code, rather it should be invoked through the
17079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez// use of the log macros LG, LOG, or VLOG.
17179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandezclass CERES_EXPORT MessageLogger {
1720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong public:
1730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  MessageLogger(const char *file, int line, const char *tag, int severity)
1740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    : file_(file), line_(line), tag_(tag), severity_(severity) {
1750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Pre-pend the stream with the file and line number.
1760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    StripBasename(std::string(file), &filename_only_);
1770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    stream_ << filename_only_ << ":" << line << " ";
1780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
1790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Output the contents of the stream to the proper channel on destruction.
1810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  ~MessageLogger() {
1820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    stream_ << "\n";
18379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
18479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#ifdef ANDROID
1850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    static const int android_log_levels[] = {
1860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong        ANDROID_LOG_FATAL,    // LOG(FATAL)
1870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong        ANDROID_LOG_ERROR,    // LOG(ERROR)
1880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong        ANDROID_LOG_WARN,     // LOG(WARNING)
18979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez        ANDROID_LOG_INFO,     // LOG(INFO), LG, VLOG(0)
1900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong        ANDROID_LOG_DEBUG,    // VLOG(1)
1910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong        ANDROID_LOG_VERBOSE,  // VLOG(2) .. VLOG(N)
1920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    };
1930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Bound the logging level.
1950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const int kMaxVerboseLevel = 2;
1960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    int android_level_index = std::min(std::max(FATAL, severity_),
1970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                                       kMaxVerboseLevel) - FATAL;
1980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    int android_log_level = android_log_levels[android_level_index];
1990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
2000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Output the log string the Android log at the appropriate level.
201399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger    __android_log_write(android_log_level, tag_.c_str(), stream_.str().c_str());
2020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
2030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Indicate termination if needed.
2040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    if (severity_ == FATAL) {
205399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger      __android_log_write(ANDROID_LOG_FATAL,
2060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                          tag_.c_str(),
2070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                          "terminating.\n");
2080ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    }
20979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#else
21079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    // If not building on Android, log all output to std::cerr.
21179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    std::cerr << stream_.str();
21279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#endif  // ANDROID
2130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
2140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    LogToSinks(severity_);
2150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    WaitForSinks();
2160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
2170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Android logging at level FATAL does not terminate execution, so abort()
2180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // is still required to stop the program.
2190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    if (severity_ == FATAL) {
2200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      abort();
2210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    }
2220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
2230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
2240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Return the stream associated with the logger object.
2250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  std::stringstream &stream() { return stream_; }
2260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
2270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong private:
2280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  void LogToSinks(int severity) {
2290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    time_t rawtime;
23079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    time (&rawtime);
23179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
23279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    struct tm* timeinfo;
23379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
23479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    // On Windows, use secure localtime_s not localtime.
23579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    struct tm windows_timeinfo;
23679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    timeinfo = &windows_timeinfo;
23779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    localtime_s(timeinfo, &rawtime);
23879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#else
23979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    timeinfo = localtime(&rawtime);
24079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#endif
2410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
24279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    std::set<google::LogSink*>::iterator iter;
2430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Send the log message to all sinks.
2440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    for (iter = google::log_sinks_global.begin();
24579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez         iter != google::log_sinks_global.end(); ++iter) {
2460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      (*iter)->send(severity, file_.c_str(), filename_only_.c_str(), line_,
2470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                    timeinfo, stream_.str().c_str(), stream_.str().size());
24879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    }
2490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
2500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
2510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  void WaitForSinks() {
25279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    // TODO(settinger): Add locks for thread safety.
2530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    std::set<google::LogSink *>::iterator iter;
25479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
2550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Call WaitTillSent() for all sinks.
2560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    for (iter = google::log_sinks_global.begin();
25779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez         iter != google::log_sinks_global.end(); ++iter) {
2580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      (*iter)->WaitTillSent();
25979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    }
2600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
2610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
2620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  void StripBasename(const std::string &full_path, std::string *filename) {
26379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    // TODO(settinger): Add support for OSs with different path separators.
2640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const char kSeparator = '/';
2650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    size_t pos = full_path.rfind(kSeparator);
26679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    if (pos != std::string::npos) {
2670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      *filename = full_path.substr(pos + 1, std::string::npos);
26879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    } else {
2690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      *filename = full_path;
27079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    }
2710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
2720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
2730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  std::string file_;
2740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  std::string filename_only_;
2750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int line_;
2760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  std::string tag_;
2770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  std::stringstream stream_;
2780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int severity_;
2790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
2800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
2810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ---------------------- Logging Macro definitions --------------------------
2820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
2830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// This class is used to explicitly ignore values in the conditional
2840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// logging macros.  This avoids compiler warnings like "value computed
2850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// is not used" and "statement has no effect".
28679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandezclass CERES_EXPORT LoggerVoidify {
2870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong public:
2880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  LoggerVoidify() { }
2890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // This has to be an operator with a precedence lower than << but
2900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // higher than ?:
2910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  void operator&(const std::ostream &s) { }
2920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
2930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
2940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Log only if condition is met.  Otherwise evaluates to void.
2950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define LOG_IF(severity, condition) \
29679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    !(condition) ? (void) 0 : LoggerVoidify() & \
29779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez      MessageLogger((char *)__FILE__, __LINE__, "native", severity).stream()
298399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger
299399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// Log only if condition is NOT met.  Otherwise evaluates to void.
300399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger#define LOG_IF_FALSE(severity, condition) LOG_IF(severity, !(condition))
301399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger
302399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// LG is a convenient shortcut for LOG(INFO). Its use is in new
303399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// google3 code is discouraged and the following shortcut exists for
304399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// backward compatibility with existing code.
305399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger#ifdef MAX_LOG_LEVEL
30679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define LOG(n)  LOG_IF(n, n <= MAX_LOG_LEVEL)
30779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define VLOG(n) LOG_IF(n, n <= MAX_LOG_LEVEL)
30879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define LG      LOG_IF(INFO, INFO <= MAX_LOG_LEVEL)
30979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define VLOG_IF(n, condition) LOG_IF(n, (n <= MAX_LOG_LEVEL) && condition)
310399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger#else
31179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define LOG(n)  MessageLogger((char *)__FILE__, __LINE__, "native", n).stream()    // NOLINT
31279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define VLOG(n) MessageLogger((char *)__FILE__, __LINE__, "native", n).stream()    // NOLINT
31379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define LG      MessageLogger((char *)__FILE__, __LINE__, "native", INFO).stream() // NOLINT
31479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define VLOG_IF(n, condition) LOG_IF(n, condition)
315399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger#endif
316399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger
317399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// Currently, VLOG is always on for levels below MAX_LOG_LEVEL.
318399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger#ifndef MAX_LOG_LEVEL
31979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define VLOG_IS_ON(x) (1)
320399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger#else
32179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define VLOG_IS_ON(x) (x <= MAX_LOG_LEVEL)
322399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger#endif
323399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger
324399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger#ifndef NDEBUG
32579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DLOG LOG
326399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger#else
32779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DLOG(severity) true ? (void) 0 : LoggerVoidify() & \
32879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez      MessageLogger((char *)__FILE__, __LINE__, "native", severity).stream()
329399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger#endif
330399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger
3310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
3320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Log a message and terminate.
3330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongtemplate<class T>
3340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongvoid LogMessageFatal(const char *file, int line, const T &message) {
3350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  MessageLogger((char *)__FILE__, __LINE__, "native", FATAL).stream()
3360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      << message;
3370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
3380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
3390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ---------------------------- CHECK macros ---------------------------------
3400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
3410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Check for a given boolean condition.
342399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger#define CHECK(condition) LOG_IF_FALSE(FATAL, condition) \
3430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong        << "Check failed: " #condition " "
3440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
3450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef NDEBUG
3460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Debug only version of CHECK
34779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK(condition) LOG_IF_FALSE(FATAL, condition) \
34879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez          << "Check failed: " #condition " "
3490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#else
3500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Optimized version - generates no code.
35179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK(condition) if (false) LOG_IF_FALSE(FATAL, condition) \
35279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez          << "Check failed: " #condition " "
3530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif  // NDEBUG
3540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
3550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ------------------------- CHECK_OP macros ---------------------------------
3560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
3570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Generic binary operator check macro. This should not be directly invoked,
3580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// instead use the binary comparison macros defined below.
35979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#define CHECK_OP(val1, val2, op) LOG_IF_FALSE(FATAL, ((val1) op (val2))) \
3600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  << "Check failed: " #val1 " " #op " " #val2 " "
3610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
3620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Check_op macro definitions
3630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CHECK_EQ(val1, val2) CHECK_OP(val1, val2, ==)
3640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CHECK_NE(val1, val2) CHECK_OP(val1, val2, !=)
3650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CHECK_LE(val1, val2) CHECK_OP(val1, val2, <=)
3660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CHECK_LT(val1, val2) CHECK_OP(val1, val2, <)
3670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CHECK_GE(val1, val2) CHECK_OP(val1, val2, >=)
3680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CHECK_GT(val1, val2) CHECK_OP(val1, val2, >)
3690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
3700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef NDEBUG
3710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Debug only versions of CHECK_OP macros.
37279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK_EQ(val1, val2) CHECK_OP(val1, val2, ==)
37379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK_NE(val1, val2) CHECK_OP(val1, val2, !=)
37479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK_LE(val1, val2) CHECK_OP(val1, val2, <=)
37579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK_LT(val1, val2) CHECK_OP(val1, val2, <)
37679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK_GE(val1, val2) CHECK_OP(val1, val2, >=)
37779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK_GT(val1, val2) CHECK_OP(val1, val2, >)
3780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#else
3790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// These versions generate no code in optimized mode.
38079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK_EQ(val1, val2) if (false) CHECK_OP(val1, val2, ==)
38179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK_NE(val1, val2) if (false) CHECK_OP(val1, val2, !=)
38279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK_LE(val1, val2) if (false) CHECK_OP(val1, val2, <=)
38379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK_LT(val1, val2) if (false) CHECK_OP(val1, val2, <)
38479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK_GE(val1, val2) if (false) CHECK_OP(val1, val2, >=)
38579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#  define DCHECK_GT(val1, val2) if (false) CHECK_OP(val1, val2, >)
3860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif  // NDEBUG
3870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
3880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ---------------------------CHECK_NOTNULL macros ---------------------------
3890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
3900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Helpers for CHECK_NOTNULL(). Two are necessary to support both raw pointers
3910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and smart pointers.
3920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongtemplate <typename T>
3930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongT& CheckNotNullCommon(const char *file, int line, const char *names, T& t) {
3940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  if (t == NULL) {
3950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    LogMessageFatal(file, line, std::string(names));
3960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
3970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  return t;
3980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
3990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
4000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongtemplate <typename T>
4010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongT* CheckNotNull(const char *file, int line, const char *names, T* t) {
4020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  return CheckNotNullCommon(file, line, names, t);
4030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
4040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
4050ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongtemplate <typename T>
4060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongT& CheckNotNull(const char *file, int line, const char *names, T& t) {
4070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  return CheckNotNullCommon(file, line, names, t);
4080ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
4090ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
4100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Check that a pointer is not null.
4110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CHECK_NOTNULL(val) \
4120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
4130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
4140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef NDEBUG
4150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Debug only version of CHECK_NOTNULL
4160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define DCHECK_NOTNULL(val) \
4170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
4180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#else
4190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Optimized version - generates no code.
4200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define DCHECK_NOTNULL(val) if (false)\
4210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
4220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif  // NDEBUG
4230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
42479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include "ceres/internal/reenable_warnings.h"
425399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger
42679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#endif  // CERCES_INTERNAL_MINIGLOG_GLOG_LOGGING_H_
427