[llvm-exegesis][NFC] Improve parsing of the YAML files
[llvm-core.git] / utils / benchmark / src / log.h
blobd06e1031db141ad5a37991f9d54cc8c778e2fd1d
1 #ifndef BENCHMARK_LOG_H_
2 #define BENCHMARK_LOG_H_
4 #include <iostream>
5 #include <ostream>
7 #include "benchmark/benchmark.h"
9 namespace benchmark {
10 namespace internal {
12 typedef std::basic_ostream<char>&(EndLType)(std::basic_ostream<char>&);
14 class LogType {
15 friend LogType& GetNullLogInstance();
16 friend LogType& GetErrorLogInstance();
18 // FIXME: Add locking to output.
19 template <class Tp>
20 friend LogType& operator<<(LogType&, Tp const&);
21 friend LogType& operator<<(LogType&, EndLType*);
23 private:
24 LogType(std::ostream* out) : out_(out) {}
25 std::ostream* out_;
26 BENCHMARK_DISALLOW_COPY_AND_ASSIGN(LogType);
29 template <class Tp>
30 LogType& operator<<(LogType& log, Tp const& value) {
31 if (log.out_) {
32 *log.out_ << value;
34 return log;
37 inline LogType& operator<<(LogType& log, EndLType* m) {
38 if (log.out_) {
39 *log.out_ << m;
41 return log;
44 inline int& LogLevel() {
45 static int log_level = 0;
46 return log_level;
49 inline LogType& GetNullLogInstance() {
50 static LogType log(nullptr);
51 return log;
54 inline LogType& GetErrorLogInstance() {
55 static LogType log(&std::clog);
56 return log;
59 inline LogType& GetLogInstanceForLevel(int level) {
60 if (level <= LogLevel()) {
61 return GetErrorLogInstance();
63 return GetNullLogInstance();
66 } // end namespace internal
67 } // end namespace benchmark
69 #define VLOG(x) \
70 (::benchmark::internal::GetLogInstanceForLevel(x) << "-- LOG(" << x << "):" \
71 " ")
73 #endif