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