forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cpp
164 lines (132 loc) · 5.23 KB
/
log.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2019 Intel Corporation. All Rights Reserved.
#include "types.h"
#include <fstream>
#if BUILD_EASYLOGGINGPP
INITIALIZE_EASYLOGGINGPP
namespace librealsense
{
class logger_type
{
rs2_log_severity minimum_log_severity = RS2_LOG_SEVERITY_NONE;
rs2_log_severity minimum_console_severity = RS2_LOG_SEVERITY_NONE;
rs2_log_severity minimum_file_severity = RS2_LOG_SEVERITY_NONE;
rs2_log_severity minimum_callback_severity = RS2_LOG_SEVERITY_NONE;
std::mutex log_mutex;
std::ofstream log_file;
log_callback_ptr callback;
std::string filename;
const std::string log_id = "librealsense";
public:
static el::Level severity_to_level(rs2_log_severity severity)
{
switch (severity)
{
case RS2_LOG_SEVERITY_DEBUG: return el::Level::Debug;
case RS2_LOG_SEVERITY_INFO: return el::Level::Info;
case RS2_LOG_SEVERITY_WARN: return el::Level::Warning;
case RS2_LOG_SEVERITY_ERROR: return el::Level::Error;
case RS2_LOG_SEVERITY_FATAL: return el::Level::Fatal;
default: return el::Level::Unknown;
}
}
void open() const
{
el::Configurations defaultConf;
defaultConf.setToDefault();
// To set GLOBAL configurations you may use
defaultConf.setGlobally(el::ConfigurationType::ToFile, "false");
defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "false");
defaultConf.setGlobally(el::ConfigurationType::MaxLogFileSize, "2097152");
defaultConf.setGlobally(el::ConfigurationType::LogFlushThreshold, "10");
defaultConf.setGlobally(el::ConfigurationType::Format, " %datetime{%d/%M %H:%m:%s,%g} %level [%thread] (%fbase:%line) %msg");
for (int i = minimum_console_severity; i < RS2_LOG_SEVERITY_NONE; i++)
{
defaultConf.set(severity_to_level(static_cast<rs2_log_severity>(i)),
el::ConfigurationType::ToStandardOutput, "true");
}
for (int i = minimum_file_severity; i < RS2_LOG_SEVERITY_NONE; i++)
{
defaultConf.setGlobally(el::ConfigurationType::Filename, filename);
defaultConf.set(severity_to_level(static_cast<rs2_log_severity>(i)),
el::ConfigurationType::ToFile, "true");
}
el::Loggers::reconfigureLogger(log_id, defaultConf);
}
void open_def() const
{
el::Configurations defaultConf;
defaultConf.setToDefault();
// To set GLOBAL configurations you may use
defaultConf.setGlobally(el::ConfigurationType::ToFile, "false");
defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "false");
el::Loggers::reconfigureLogger(log_id, defaultConf);
}
logger_type()
: callback(nullptr, [](rs2_log_callback*) {}),
filename(to_string() << datetime_string() << ".log")
{
rs2_log_severity severity;
if (try_get_log_severity(severity))
{
log_to_file(severity, filename.c_str());
}
else
{
open_def();
}
}
static bool try_get_log_severity(rs2_log_severity& severity)
{
static const char* severity_var_name = "LRS_LOG_LEVEL";
auto content = getenv(severity_var_name);
if (content)
{
std::string content_str(content);
std::transform(content_str.begin(), content_str.end(), content_str.begin(), ::tolower);
for (uint32_t i = 0; i < RS2_LOG_SEVERITY_COUNT; i++)
{
auto current = (rs2_log_severity)i;
std::string name = librealsense::get_string(current);
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
if (content_str == name)
{
severity = current;
return true;
}
}
}
return false;
}
void log_to_console(rs2_log_severity min_severity)
{
minimum_console_severity = min_severity;
open();
}
void log_to_file(rs2_log_severity min_severity, const char * file_path)
{
if (!try_get_log_severity(minimum_file_severity))
minimum_file_severity = min_severity;
if (file_path)
filename = file_path;
open();
}
};
static logger_type logger;
}
void librealsense::log_to_console(rs2_log_severity min_severity)
{
logger.log_to_console(min_severity);
}
void librealsense::log_to_file(rs2_log_severity min_severity, const char * file_path)
{
logger.log_to_file(min_severity, file_path);
}
#else // BUILD_EASYLOGGINGPP
void librealsense::log_to_console(rs2_log_severity min_severity)
{
}
void librealsense::log_to_file(rs2_log_severity min_severity, const char * file_path)
{
}
#endif // BUILD_EASYLOGGINGPP