| 1 |
// Ceres Solver - A fast non-linear least squares minimizer |
| 2 |
// Copyright 2015 Google Inc. All rights reserved. |
| 3 |
// http://ceres-solver.org/ |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without |
| 6 |
// modification, are permitted provided that the following conditions are met: |
| 7 |
// |
| 8 |
// * Redistributions of source code must retain the above copyright notice, |
| 9 |
// this list of conditions and the following disclaimer. |
| 10 |
// * Redistributions in binary form must reproduce the above copyright notice, |
| 11 |
// this list of conditions and the following disclaimer in the documentation |
| 12 |
// and/or other materials provided with the distribution. |
| 13 |
// * Neither the name of Google Inc. nor the names of its contributors may be |
| 14 |
// used to endorse or promote products derived from this software without |
| 15 |
// specific prior written permission. |
| 16 |
// |
| 17 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 |
// POSSIBILITY OF SUCH DAMAGE. |
| 28 |
// |
| 29 |
// Author: [email protected] (Scott Ettinger) |
| 30 |
// [email protected] (Keir Mierle) |
| 31 |
// |
| 32 |
// Simplified Glog style logging with Android support. Supported macros in |
| 33 |
// decreasing severity level per line: |
| 34 |
// |
| 35 |
// VLOG(2), VLOG(N) |
| 36 |
// VLOG(1), |
| 37 |
// LOG(INFO), VLOG(0), LG |
| 38 |
// LOG(WARNING), |
| 39 |
// LOG(ERROR), |
| 40 |
// LOG(FATAL), |
| 41 |
// |
| 42 |
// With VLOG(n), the output is directed to one of the 5 Android log levels: |
| 43 |
// |
| 44 |
// 2 - Verbose |
| 45 |
// 1 - Debug |
| 46 |
// 0 - Info |
| 47 |
// -1 - Warning |
| 48 |
// -2 - Error |
| 49 |
// -3 - Fatal |
| 50 |
// |
| 51 |
// Any logging of level 2 and above is directed to the Verbose level. All |
| 52 |
// Android log output is tagged with the string "native". |
| 53 |
// |
| 54 |
// If the symbol ANDROID is not defined, all output goes to std::cerr. |
| 55 |
// This allows code to be built on a different system for debug. |
| 56 |
// |
| 57 |
// Portions of this code are taken from the GLOG package. This code is only a |
| 58 |
// small subset of the GLOG functionality. Notable differences from GLOG |
| 59 |
// behavior include lack of support for displaying unprintable characters and |
| 60 |
// lack of stack trace information upon failure of the CHECK macros. On |
| 61 |
// non-Android systems, log output goes to std::cerr and is not written to a |
| 62 |
// file. |
| 63 |
// |
| 64 |
// CHECK macros are defined to test for conditions within code. Any CHECK that |
| 65 |
// fails will log the failure and terminate the application. |
| 66 |
// e.g. CHECK_GE(3, 2) will pass while CHECK_GE(3, 4) will fail after logging |
| 67 |
// "Check failed 3 >= 4". |
| 68 |
// |
| 69 |
// The following CHECK macros are defined: |
| 70 |
// |
| 71 |
// CHECK(condition) - fails if condition is false and logs condition. |
| 72 |
// CHECK_NOTNULL(variable) - fails if the variable is nullptr. |
| 73 |
// |
| 74 |
// The following binary check macros are also defined : |
| 75 |
// |
| 76 |
// Macro Operator equivalent |
| 77 |
// -------------------- ------------------- |
| 78 |
// CHECK_EQ(val1, val2) val1 == val2 |
| 79 |
// CHECK_NE(val1, val2) val1 != val2 |
| 80 |
// CHECK_GT(val1, val2) val1 > val2 |
| 81 |
// CHECK_GE(val1, val2) val1 >= val2 |
| 82 |
// CHECK_LT(val1, val2) val1 < val2 |
| 83 |
// CHECK_LE(val1, val2) val1 <= val2 |
| 84 |
// |
| 85 |
// Debug only versions of all of the check macros are also defined. These |
| 86 |
// macros generate no code in a release build, but avoid unused variable |
| 87 |
// warnings / errors. |
| 88 |
// |
| 89 |
// To use the debug only versions, prepend a D to the normal check macros, e.g. |
| 90 |
// DCHECK_EQ(a, b). |
| 91 |
|
| 92 |
#ifndef CERCES_INTERNAL_MINIGLOG_GLOG_LOGGING_H_ |
| 93 |
#define CERCES_INTERNAL_MINIGLOG_GLOG_LOGGING_H_ |
| 94 |
|
| 95 |
#ifdef ANDROID |
| 96 |
#include <android/log.h> |
| 97 |
#endif // ANDROID |
| 98 |
|
| 99 |
#include <algorithm> |
| 100 |
#include <ctime> |
| 101 |
#include <fstream> |
| 102 |
#include <iostream> |
| 103 |
#include <set> |
| 104 |
#include <sstream> |
| 105 |
#include <string> |
| 106 |
#include <vector> |
| 107 |
|
| 108 |
#include "ceres/internal/disable_warnings.h" |
| 109 |
#include "ceres/internal/export.h" |
| 110 |
|
| 111 |
// Log severity level constants. |
| 112 |
// clang-format off |
| 113 |
const int FATAL = -3; |
| 114 |
const int ERROR = -2; |
| 115 |
const int WARNING = -1; |
| 116 |
const int INFO = 0; |
| 117 |
// clang-format on |
| 118 |
|
| 119 |
// ------------------------- Glog compatibility ------------------------------ |
| 120 |
|
| 121 |
namespace google { |
| 122 |
|
| 123 |
using LogSeverity = int; |
| 124 |
// clang-format off |
| 125 |
const int INFO = ::INFO; |
| 126 |
const int WARNING = ::WARNING; |
| 127 |
const int ERROR = ::ERROR; |
| 128 |
const int FATAL = ::FATAL; |
| 129 |
// clang-format on |
| 130 |
|
| 131 |
// Sink class used for integration with mock and test functions. If sinks are |
| 132 |
// added, all log output is also sent to each sink through the send function. |
| 133 |
// In this implementation, WaitTillSent() is called immediately after the send. |
| 134 |
// This implementation is not thread safe. |
| 135 |
class CERES_EXPORT LogSink { |
| 136 |
public: |
| 137 |
virtual ~LogSink() = default; |
| 138 |
virtual void send(LogSeverity severity, |
| 139 |
const char* full_filename, |
| 140 |
const char* base_filename, |
| 141 |
int line, |
| 142 |
const struct tm* tm_time, |
| 143 |
const char* message, |
| 144 |
size_t message_len) = 0; |
| 145 |
virtual void WaitTillSent() = 0; |
| 146 |
}; |
| 147 |
|
| 148 |
// Global set of log sinks. The actual object is defined in logging.cc. |
| 149 |
extern CERES_EXPORT std::set<LogSink*> log_sinks_global; |
| 150 |
|
| 151 |
inline void InitGoogleLogging(const char* /* argv */) { |
| 152 |
// Do nothing; this is ignored. |
| 153 |
} |
| 154 |
|
| 155 |
// Note: the Log sink functions are not thread safe. |
| 156 |
inline void AddLogSink(LogSink* sink) { |
| 157 |
// TODO(settinger): Add locks for thread safety. |
| 158 |
log_sinks_global.insert(sink); |
| 159 |
} |
| 160 |
inline void RemoveLogSink(LogSink* sink) { log_sinks_global.erase(sink); } |
| 161 |
|
| 162 |
} // namespace google |
| 163 |
|
| 164 |
// ---------------------------- Logger Class -------------------------------- |
| 165 |
|
| 166 |
// Class created for each use of the logging macros. |
| 167 |
// The logger acts as a stream and routes the final stream contents to the |
| 168 |
// Android logcat output at the proper filter level. If ANDROID is not |
| 169 |
// defined, output is directed to std::cerr. This class should not |
| 170 |
// be directly instantiated in code, rather it should be invoked through the |
| 171 |
// use of the log macros LG, LOG, or VLOG. |
| 172 |
class CERES_EXPORT MessageLogger { |
| 173 |
public: |
| 174 |
MessageLogger(const char* file, int line, const char* tag, int severity) |
| 175 |
: file_(file), line_(line), tag_(tag), severity_(severity) { |
| 176 |
// Pre-pend the stream with the file and line number. |
| 177 |
StripBasename(std::string(file), &filename_only_); |
| 178 |
stream_ << filename_only_ << ":" << line << " "; |
| 179 |
} |
| 180 |
|
| 181 |
// Output the contents of the stream to the proper channel on destruction. |
| 182 |
~MessageLogger() { |
| 183 |
stream_ << "\n"; |
| 184 |
|
| 185 |
#ifdef ANDROID |
| 186 |
static const int android_log_levels[] = { |
| 187 |
ANDROID_LOG_FATAL, // LOG(FATAL) |
| 188 |
ANDROID_LOG_ERROR, // LOG(ERROR) |
| 189 |
ANDROID_LOG_WARN, // LOG(WARNING) |
| 190 |
ANDROID_LOG_INFO, // LOG(INFO), LG, VLOG(0) |
| 191 |
ANDROID_LOG_DEBUG, // VLOG(1) |
| 192 |
ANDROID_LOG_VERBOSE, // VLOG(2) .. VLOG(N) |
| 193 |
}; |
| 194 |
|
| 195 |
// Bound the logging level. |
| 196 |
const int kMaxVerboseLevel = 2; |
| 197 |
int android_level_index = |
| 198 |
std::min(std::max(FATAL, severity_), kMaxVerboseLevel) - FATAL; |
| 199 |
int android_log_level = android_log_levels[android_level_index]; |
| 200 |
|
| 201 |
// Output the log string the Android log at the appropriate level. |
| 202 |
__android_log_write(android_log_level, tag_.c_str(), stream_.str().c_str()); |
| 203 |
|
| 204 |
// Indicate termination if needed. |
| 205 |
if (severity_ == FATAL) { |
| 206 |
__android_log_write(ANDROID_LOG_FATAL, tag_.c_str(), "terminating.\n"); |
| 207 |
} |
| 208 |
#else |
| 209 |
// If not building on Android, log all output to std::cerr. |
| 210 |
std::cerr << stream_.str(); |
| 211 |
#endif // ANDROID |
| 212 |
|
| 213 |
LogToSinks(severity_); |
| 214 |
WaitForSinks(); |
| 215 |
|
| 216 |
// Android logging at level FATAL does not terminate execution, so abort() |
| 217 |
// is still required to stop the program. |
| 218 |
if (severity_ == FATAL) { |
| 219 |
abort(); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
// Return the stream associated with the logger object. |
| 224 |
std::stringstream& stream() { return stream_; } |
| 225 |
|
| 226 |
private: |
| 227 |
void LogToSinks(int severity) { |
| 228 |
time_t rawtime; |
| 229 |
time(&rawtime); |
| 230 |
|
| 231 |
struct tm timeinfo; |
| 232 |
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) |
| 233 |
// On Windows, use secure localtime_s not localtime. |
| 234 |
localtime_s(&timeinfo, &rawtime); |
| 235 |
#else |
| 236 |
// On non-Windows systems, use threadsafe localtime_r not localtime. |
| 237 |
localtime_r(&rawtime, &timeinfo); |
| 238 |
#endif |
| 239 |
|
| 240 |
std::set<google::LogSink*>::iterator iter; |
| 241 |
// Send the log message to all sinks. |
| 242 |
for (iter = google::log_sinks_global.begin(); |
| 243 |
iter != google::log_sinks_global.end(); |
| 244 |
++iter) { |
| 245 |
(*iter)->send(severity, |
| 246 |
file_.c_str(), |
| 247 |
filename_only_.c_str(), |
| 248 |
line_, |
| 249 |
&timeinfo, |
| 250 |
stream_.str().c_str(), |
| 251 |
stream_.str().size()); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
void WaitForSinks() { |
| 256 |
// TODO(settinger): Add locks for thread safety. |
| 257 |
std::set<google::LogSink*>::iterator iter; |
| 258 |
|
| 259 |
// Call WaitTillSent() for all sinks. |
| 260 |
for (iter = google::log_sinks_global.begin(); |
| 261 |
iter != google::log_sinks_global.end(); |
| 262 |
++iter) { |
| 263 |
(*iter)->WaitTillSent(); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
void StripBasename(const std::string& full_path, std::string* filename) { |
| 268 |
// TODO(settinger): Add support for OSs with different path separators. |
| 269 |
const char kSeparator = '/'; |
| 270 |
size_t pos = full_path.rfind(kSeparator); |
| 271 |
if (pos != std::string::npos) { |
| 272 |
*filename = full_path.substr(pos + 1, std::string::npos); |
| 273 |
} else { |
| 274 |
*filename = full_path; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
std::string file_; |
| 279 |
std::string filename_only_; |
| 280 |
int line_; |
| 281 |
std::string tag_; |
| 282 |
std::stringstream stream_; |
| 283 |
int severity_; |
| 284 |
}; |
| 285 |
|
| 286 |
// ---------------------- Logging Macro definitions -------------------------- |
| 287 |
|
| 288 |
// This class is used to explicitly ignore values in the conditional |
| 289 |
// logging macros. This avoids compiler warnings like "value computed |
| 290 |
// is not used" and "statement has no effect". |
| 291 |
class CERES_EXPORT LoggerVoidify { |
| 292 |
public: |
| 293 |
// This has to be an operator with a precedence lower than << but |
| 294 |
// higher than ?: |
| 295 |
void operator&(const std::ostream& s) {} |
| 296 |
}; |
| 297 |
|
| 298 |
// Log only if condition is met. Otherwise evaluates to void. |
| 299 |
// clang-format off |
| 300 |
#define LOG_IF(severity, condition) \ |
| 301 |
!(condition) ? (void) 0 : LoggerVoidify() & \ |
| 302 |
MessageLogger((char *)__FILE__, __LINE__, "native", severity).stream() |
| 303 |
// clang-format on |
| 304 |
|
| 305 |
// Log only if condition is NOT met. Otherwise evaluates to void. |
| 306 |
#define LOG_IF_FALSE(severity, condition) LOG_IF(severity, !(condition)) |
| 307 |
|
| 308 |
// LG is a convenient shortcut for LOG(INFO). Its use is in new |
| 309 |
// google3 code is discouraged and the following shortcut exists for |
| 310 |
// backward compatibility with existing code. |
| 311 |
// clang-format off |
| 312 |
#ifdef MAX_LOG_LEVEL |
| 313 |
# define LOG(n) LOG_IF(n, n <= MAX_LOG_LEVEL) |
| 314 |
# define VLOG(n) LOG_IF(n, n <= MAX_LOG_LEVEL) |
| 315 |
# define LG LOG_IF(INFO, INFO <= MAX_LOG_LEVEL) |
| 316 |
# define VLOG_IF(n, condition) LOG_IF(n, (n <= MAX_LOG_LEVEL) && condition) |
| 317 |
#else |
| 318 |
# define LOG(n) MessageLogger((char *)__FILE__, __LINE__, "native", n).stream() // NOLINT |
| 319 |
# define VLOG(n) MessageLogger((char *)__FILE__, __LINE__, "native", n).stream() // NOLINT |
| 320 |
# define LG MessageLogger((char *)__FILE__, __LINE__, "native", INFO).stream() // NOLINT |
| 321 |
# define VLOG_IF(n, condition) LOG_IF(n, condition) |
| 322 |
#endif |
| 323 |
|
| 324 |
// Currently, VLOG is always on for levels below MAX_LOG_LEVEL. |
| 325 |
#ifndef MAX_LOG_LEVEL |
| 326 |
# define VLOG_IS_ON(x) (1) |
| 327 |
#else |
| 328 |
# define VLOG_IS_ON(x) (x <= MAX_LOG_LEVEL) |
| 329 |
#endif |
| 330 |
|
| 331 |
#ifndef NDEBUG |
| 332 |
# define DLOG LOG |
| 333 |
#else |
| 334 |
# define DLOG(severity) true ? (void) 0 : LoggerVoidify() & \ |
| 335 |
MessageLogger((char *)__FILE__, __LINE__, "native", severity).stream() |
| 336 |
#endif |
| 337 |
// clang-format on |
| 338 |
|
| 339 |
// Log a message and terminate. |
| 340 |
template <class T> |
| 341 |
void LogMessageFatal(const char* file, int line, const T& message) { |
| 342 |
MessageLogger((char*)__FILE__, __LINE__, "native", FATAL).stream() << message; |
| 343 |
} |
| 344 |
|
| 345 |
// ---------------------------- CHECK macros --------------------------------- |
| 346 |
|
| 347 |
// Check for a given boolean condition. |
| 348 |
#define CHECK(condition) \ |
| 349 |
LOG_IF_FALSE(FATAL, condition) << "Check failed: " #condition " " |
| 350 |
|
| 351 |
#ifndef NDEBUG |
| 352 |
// Debug only version of CHECK |
| 353 |
#define DCHECK(condition) \ |
| 354 |
LOG_IF_FALSE(FATAL, condition) << "Check failed: " #condition " " |
| 355 |
#else |
| 356 |
// Optimized version - generates no code. |
| 357 |
#define DCHECK(condition) \ |
| 358 |
if (false) LOG_IF_FALSE(FATAL, condition) << "Check failed: " #condition " " |
| 359 |
#endif // NDEBUG |
| 360 |
|
| 361 |
// ------------------------- CHECK_OP macros --------------------------------- |
| 362 |
|
| 363 |
// Generic binary operator check macro. This should not be directly invoked, |
| 364 |
// instead use the binary comparison macros defined below. |
| 365 |
#define CHECK_OP(val1, val2, op) \ |
| 366 |
LOG_IF_FALSE(FATAL, ((val1)op(val2))) \ |
| 367 |
<< "Check failed: " #val1 " " #op " " #val2 " " |
| 368 |
|
| 369 |
// clang-format off |
| 370 |
|
| 371 |
// Check_op macro definitions |
| 372 |
#define CHECK_EQ(val1, val2) CHECK_OP(val1, val2, ==) |
| 373 |
#define CHECK_NE(val1, val2) CHECK_OP(val1, val2, !=) |
| 374 |
#define CHECK_LE(val1, val2) CHECK_OP(val1, val2, <=) |
| 375 |
#define CHECK_LT(val1, val2) CHECK_OP(val1, val2, <) |
| 376 |
#define CHECK_GE(val1, val2) CHECK_OP(val1, val2, >=) |
| 377 |
#define CHECK_GT(val1, val2) CHECK_OP(val1, val2, >) |
| 378 |
|
| 379 |
#ifndef NDEBUG |
| 380 |
// Debug only versions of CHECK_OP macros. |
| 381 |
# define DCHECK_EQ(val1, val2) CHECK_OP(val1, val2, ==) |
| 382 |
# define DCHECK_NE(val1, val2) CHECK_OP(val1, val2, !=) |
| 383 |
# define DCHECK_LE(val1, val2) CHECK_OP(val1, val2, <=) |
| 384 |
# define DCHECK_LT(val1, val2) CHECK_OP(val1, val2, <) |
| 385 |
# define DCHECK_GE(val1, val2) CHECK_OP(val1, val2, >=) |
| 386 |
# define DCHECK_GT(val1, val2) CHECK_OP(val1, val2, >) |
| 387 |
#else |
| 388 |
// These versions generate no code in optimized mode. |
| 389 |
# define DCHECK_EQ(val1, val2) if (false) CHECK_OP(val1, val2, ==) |
| 390 |
# define DCHECK_NE(val1, val2) if (false) CHECK_OP(val1, val2, !=) |
| 391 |
# define DCHECK_LE(val1, val2) if (false) CHECK_OP(val1, val2, <=) |
| 392 |
# define DCHECK_LT(val1, val2) if (false) CHECK_OP(val1, val2, <) |
| 393 |
# define DCHECK_GE(val1, val2) if (false) CHECK_OP(val1, val2, >=) |
| 394 |
# define DCHECK_GT(val1, val2) if (false) CHECK_OP(val1, val2, >) |
| 395 |
#endif // NDEBUG |
| 396 |
|
| 397 |
// clang-format on |
| 398 |
|
| 399 |
// ---------------------------CHECK_NOTNULL macros --------------------------- |
| 400 |
|
| 401 |
// Helpers for CHECK_NOTNULL(). Two are necessary to support both raw pointers |
| 402 |
// and smart pointers. |
| 403 |
template <typename T> |
| 404 |
T& CheckNotNullCommon(const char* file, int line, const char* names, T& t) { |
| 405 |
if (t == nullptr) { |
| 406 |
LogMessageFatal(file, line, std::string(names)); |
| 407 |
} |
| 408 |
return t; |
| 409 |
} |
| 410 |
|
| 411 |
template <typename T> |
| 412 |
T* CheckNotNull(const char* file, int line, const char* names, T* t) { |
| 413 |
return CheckNotNullCommon(file, line, names, t); |
| 414 |
} |
| 415 |
|
| 416 |
template <typename T> |
| 417 |
T& CheckNotNull(const char* file, int line, const char* names, T& t) { |
| 418 |
return CheckNotNullCommon(file, line, names, t); |
| 419 |
} |
| 420 |
|
| 421 |
// Check that a pointer is not null. |
| 422 |
#define CHECK_NOTNULL(val) \ |
| 423 |
CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non nullptr", (val)) |
| 424 |
|
| 425 |
#ifndef NDEBUG |
| 426 |
// Debug only version of CHECK_NOTNULL |
| 427 |
#define DCHECK_NOTNULL(val) \ |
| 428 |
CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non nullptr", (val)) |
| 429 |
#else |
| 430 |
// Optimized version - generates no code. |
| 431 |
#define DCHECK_NOTNULL(val) \ |
| 432 |
if (false) \ |
| 433 |
CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non nullptr", (val)) |
| 434 |
#endif // NDEBUG |
| 435 |
|
| 436 |
#include "ceres/internal/reenable_warnings.h" |
| 437 |
|
| 438 |
#endif // CERCES_INTERNAL_MINIGLOG_GLOG_LOGGING_H_ |