12ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// Copyright 2009 The RE2 Authors.  All Rights Reserved.
22ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// Use of this source code is governed by a BSD-style
32ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// license that can be found in the LICENSE file.
42ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
52ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// Simplified version of Google's logging.
62ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
72ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#ifndef RE2_UTIL_LOGGING_H__
82ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define RE2_UTIL_LOGGING_H__
92ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
102ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#include <unistd.h>  /* for write */
112ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#include <sstream>
122ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
132ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// Debug-only checking.
142ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define DCHECK(condition) assert(condition)
152ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define DCHECK_EQ(val1, val2) assert((val1) == (val2))
162ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define DCHECK_NE(val1, val2) assert((val1) != (val2))
172ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define DCHECK_LE(val1, val2) assert((val1) <= (val2))
182ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define DCHECK_LT(val1, val2) assert((val1) < (val2))
192ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define DCHECK_GE(val1, val2) assert((val1) >= (val2))
202ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define DCHECK_GT(val1, val2) assert((val1) > (val2))
212ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
222ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// Always-on checking
232ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define CHECK(x)	if(x){}else LogMessageFatal(__FILE__, __LINE__).stream() << "Check failed: " #x
242ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define CHECK_LT(x, y)	CHECK((x) < (y))
252ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define CHECK_GT(x, y)	CHECK((x) > (y))
262ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define CHECK_LE(x, y)	CHECK((x) <= (y))
272ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define CHECK_GE(x, y)	CHECK((x) >= (y))
282ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define CHECK_EQ(x, y)	CHECK((x) == (y))
292ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define CHECK_NE(x, y)	CHECK((x) != (y))
302ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
312ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define LOG_INFO LogMessage(__FILE__, __LINE__)
322ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define LOG_ERROR LOG_INFO
332ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define LOG_WARNING LOG_INFO
342ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define LOG_FATAL LogMessageFatal(__FILE__, __LINE__)
352ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define LOG_QFATAL LOG_FATAL
362ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
372ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define VLOG(x) if((x)>0){}else LOG_INFO.stream()
382ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
392ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#ifdef NDEBUG
402ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define DEBUG_MODE 0
412ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define LOG_DFATAL LOG_ERROR
422ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#else
432ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define DEBUG_MODE 1
442ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define LOG_DFATAL LOG_FATAL
452ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#endif
462ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
472ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define LOG(severity) LOG_ ## severity.stream()
482ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
492ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodsonclass LogMessage {
502ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson public:
510d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  LogMessage(const char* file, int line) : flushed_(false) {
522ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson    stream() << file << ":" << line << ": ";
532ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  }
540d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  void Flush() {
552ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson    stream() << "\n";
562ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson    string s = str_.str();
570d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    int n = (int)s.size(); // shut up msvc
580d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    if(write(2, s.data(), n) < 0) {}  // shut up gcc
590d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    flushed_ = true;
600d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  }
610d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  ~LogMessage() {
620d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    if (!flushed_) {
630d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin      Flush();
640d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    }
652ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  }
662ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  ostream& stream() { return str_; }
672ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
682ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson private:
690d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  bool flushed_;
702ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  std::ostringstream str_;
712ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  DISALLOW_EVIL_CONSTRUCTORS(LogMessage);
722ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson};
732ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
742ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodsonclass LogMessageFatal : public LogMessage {
752ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson public:
762ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  LogMessageFatal(const char* file, int line)
772ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson    : LogMessage(file, line) { }
782ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  ~LogMessageFatal() {
790d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    Flush();
802ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson    abort();
812ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  }
822ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson private:
832ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  DISALLOW_EVIL_CONSTRUCTORS(LogMessageFatal);
842ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson};
852ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
862ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#endif  // RE2_UTIL_LOGGING_H__
87