-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlog.h
67 lines (49 loc) · 1.6 KB
/
log.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef __ASHUFFLE_LOG_H__
#define __ASHUFFLE_LOG_H__
#include <string_view>
#include <absl/strings/str_format.h>
#include "log_internal.h"
namespace ashuffle {
class Log final {
public:
Log(log::SourceLocation loc = log::SourceLocation()) : loc_(loc) {}
template <typename... Args>
void Info(const absl::FormatSpec<Args...>& fmt, Args... args) {
WriteLog(Level::kInfo, fmt, args...);
}
void InfoStr(std::string_view message) {
WriteLogStr(Level::kInfo, message);
}
template <typename... Args>
void Error(const absl::FormatSpec<Args...>& fmt, Args... args) {
WriteLog(Level::kError, fmt, args...);
}
void ErrorStr(std::string_view message) {
WriteLogStr(Level::kError, message);
}
private:
enum class Level {
kInfo,
kError,
};
friend std::ostream& operator<<(std::ostream&, const Level&);
void WriteLogStr(Level level, std::string_view message) {
log::DefaultLogger().Stream()
<< level << " " << loc_ << ": " << message << std::endl;
}
template <typename... Args>
void WriteLog(Level level, const absl::FormatSpec<Args...>& fmt,
Args... args) {
log::DefaultLogger().Stream()
<< level << " " << loc_ << ": " << absl::StrFormat(fmt, args...)
<< std::endl;
}
log::SourceLocation loc_;
};
namespace log {
// Set the output of the default logger to the given ostream. The ostream must
// have program lifetime.
void SetOutput(std::ostream& output);
} // namespace log
} // namespace ashuffle
#endif // __ASHUFFLE_LOG_H__