1*58b9f456SAndroid Build Coastguard Worker #ifndef BENCHMARK_LOG_H_ 2*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_LOG_H_ 3*58b9f456SAndroid Build Coastguard Worker 4*58b9f456SAndroid Build Coastguard Worker #include <iostream> 5*58b9f456SAndroid Build Coastguard Worker #include <ostream> 6*58b9f456SAndroid Build Coastguard Worker 7*58b9f456SAndroid Build Coastguard Worker #include "benchmark/benchmark.h" 8*58b9f456SAndroid Build Coastguard Worker 9*58b9f456SAndroid Build Coastguard Worker namespace benchmark { 10*58b9f456SAndroid Build Coastguard Worker namespace internal { 11*58b9f456SAndroid Build Coastguard Worker 12*58b9f456SAndroid Build Coastguard Worker typedef std::basic_ostream<char>&(EndLType)(std::basic_ostream<char>&); 13*58b9f456SAndroid Build Coastguard Worker 14*58b9f456SAndroid Build Coastguard Worker class LogType { 15*58b9f456SAndroid Build Coastguard Worker friend LogType& GetNullLogInstance(); 16*58b9f456SAndroid Build Coastguard Worker friend LogType& GetErrorLogInstance(); 17*58b9f456SAndroid Build Coastguard Worker 18*58b9f456SAndroid Build Coastguard Worker // FIXME: Add locking to output. 19*58b9f456SAndroid Build Coastguard Worker template <class Tp> 20*58b9f456SAndroid Build Coastguard Worker friend LogType& operator<<(LogType&, Tp const&); 21*58b9f456SAndroid Build Coastguard Worker friend LogType& operator<<(LogType&, EndLType*); 22*58b9f456SAndroid Build Coastguard Worker 23*58b9f456SAndroid Build Coastguard Worker private: LogType(std::ostream * out)24*58b9f456SAndroid Build Coastguard Worker LogType(std::ostream* out) : out_(out) {} 25*58b9f456SAndroid Build Coastguard Worker std::ostream* out_; 26*58b9f456SAndroid Build Coastguard Worker BENCHMARK_DISALLOW_COPY_AND_ASSIGN(LogType); 27*58b9f456SAndroid Build Coastguard Worker }; 28*58b9f456SAndroid Build Coastguard Worker 29*58b9f456SAndroid Build Coastguard Worker template <class Tp> 30*58b9f456SAndroid Build Coastguard Worker LogType& operator<<(LogType& log, Tp const& value) { 31*58b9f456SAndroid Build Coastguard Worker if (log.out_) { 32*58b9f456SAndroid Build Coastguard Worker *log.out_ << value; 33*58b9f456SAndroid Build Coastguard Worker } 34*58b9f456SAndroid Build Coastguard Worker return log; 35*58b9f456SAndroid Build Coastguard Worker } 36*58b9f456SAndroid Build Coastguard Worker 37*58b9f456SAndroid Build Coastguard Worker inline LogType& operator<<(LogType& log, EndLType* m) { 38*58b9f456SAndroid Build Coastguard Worker if (log.out_) { 39*58b9f456SAndroid Build Coastguard Worker *log.out_ << m; 40*58b9f456SAndroid Build Coastguard Worker } 41*58b9f456SAndroid Build Coastguard Worker return log; 42*58b9f456SAndroid Build Coastguard Worker } 43*58b9f456SAndroid Build Coastguard Worker LogLevel()44*58b9f456SAndroid Build Coastguard Workerinline int& LogLevel() { 45*58b9f456SAndroid Build Coastguard Worker static int log_level = 0; 46*58b9f456SAndroid Build Coastguard Worker return log_level; 47*58b9f456SAndroid Build Coastguard Worker } 48*58b9f456SAndroid Build Coastguard Worker GetNullLogInstance()49*58b9f456SAndroid Build Coastguard Workerinline LogType& GetNullLogInstance() { 50*58b9f456SAndroid Build Coastguard Worker static LogType log(nullptr); 51*58b9f456SAndroid Build Coastguard Worker return log; 52*58b9f456SAndroid Build Coastguard Worker } 53*58b9f456SAndroid Build Coastguard Worker GetErrorLogInstance()54*58b9f456SAndroid Build Coastguard Workerinline LogType& GetErrorLogInstance() { 55*58b9f456SAndroid Build Coastguard Worker static LogType log(&std::clog); 56*58b9f456SAndroid Build Coastguard Worker return log; 57*58b9f456SAndroid Build Coastguard Worker } 58*58b9f456SAndroid Build Coastguard Worker GetLogInstanceForLevel(int level)59*58b9f456SAndroid Build Coastguard Workerinline LogType& GetLogInstanceForLevel(int level) { 60*58b9f456SAndroid Build Coastguard Worker if (level <= LogLevel()) { 61*58b9f456SAndroid Build Coastguard Worker return GetErrorLogInstance(); 62*58b9f456SAndroid Build Coastguard Worker } 63*58b9f456SAndroid Build Coastguard Worker return GetNullLogInstance(); 64*58b9f456SAndroid Build Coastguard Worker } 65*58b9f456SAndroid Build Coastguard Worker 66*58b9f456SAndroid Build Coastguard Worker } // end namespace internal 67*58b9f456SAndroid Build Coastguard Worker } // end namespace benchmark 68*58b9f456SAndroid Build Coastguard Worker 69*58b9f456SAndroid Build Coastguard Worker // clang-format off 70*58b9f456SAndroid Build Coastguard Worker #define VLOG(x) \ 71*58b9f456SAndroid Build Coastguard Worker (::benchmark::internal::GetLogInstanceForLevel(x) << "-- LOG(" << x << "):" \ 72*58b9f456SAndroid Build Coastguard Worker " ") 73*58b9f456SAndroid Build Coastguard Worker // clang-format on 74*58b9f456SAndroid Build Coastguard Worker #endif 75