-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathlog.cpp
More file actions
78 lines (69 loc) · 2.13 KB
/
log.cpp
File metadata and controls
78 lines (69 loc) · 2.13 KB
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
68
69
70
71
72
73
74
75
76
77
78
#include "databento/log.hpp"
#include <iostream>
#include <memory>
#include <sstream>
#include "databento/system.hpp"
#include "databento/version.hpp"
#include "stream_op_helper.hpp"
databento::ILogReceiver* databento::ILogReceiver::Default() {
static const std::unique_ptr<ILogReceiver> gDefaultLogger{
std::make_unique<ConsoleLogReceiver>()};
return gDefaultLogger.get();
}
using databento::ConsoleLogReceiver;
ConsoleLogReceiver::ConsoleLogReceiver()
: ConsoleLogReceiver{LogLevel::Info, std::clog} {}
ConsoleLogReceiver::ConsoleLogReceiver(LogLevel min_level)
: ConsoleLogReceiver{min_level, std::clog} {}
ConsoleLogReceiver::ConsoleLogReceiver(std::ostream& stream)
: ConsoleLogReceiver{LogLevel::Info, stream} {}
ConsoleLogReceiver::ConsoleLogReceiver(LogLevel min_level, std::ostream& stream)
: stream_{stream}, min_level_{min_level} {}
void ConsoleLogReceiver::Receive(LogLevel level, const std::string& msg) {
if (ShouldLog(level)) {
stream_ << level << ": " << msg;
// Don't add a newline if `msg` ends in one
if (msg.empty() || msg.back() != '\n') {
stream_ << '\n';
}
}
}
namespace databento {
std::ostream& operator<<(std::ostream& out, LogLevel level) {
out << ToString(level);
return out;
}
const char* ToString(LogLevel level) {
switch (level) {
case LogLevel::Debug: {
return "DEBUG";
}
case LogLevel::Info: {
return "INFO";
}
case LogLevel::Warning: {
return "WARN";
}
case LogLevel::Error: {
return "ERROR";
}
default: {
return "UNKNOWN";
};
}
}
void LogPlatformInfo() { LogPlatformInfo(ILogReceiver::Default()); }
void LogPlatformInfo(ILogReceiver* log_receiver) {
std::ostringstream ss;
StreamOpBuilder{ss}
.SetSpacer(" ")
.Build()
.AddField("client_version", DATABENTO_VERSION)
.AddField("compiler", DATABENTO_CXX_COMPILER_ID)
.AddField("compiler_version", DATABENTO_CXX_COMPILER_VERSION)
.AddField("os", DATABENTO_SYSTEM_ID)
.AddField("os_version", DATABENTO_SYSTEM_VERSION)
.Finish();
log_receiver->Receive(LogLevel::Info, ss.str());
}
} // namespace databento