forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cpp
61 lines (52 loc) · 2.43 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
#include "types.h"
#include <fstream>
#include <iostream>
#include <algorithm>
#include <ctime>
rs_log_severity rsimpl::minimum_log_severity = RS_LOG_SEVERITY_NONE;
static rs_log_severity minimum_console_severity = RS_LOG_SEVERITY_NONE;
static rs_log_severity minimum_file_severity = RS_LOG_SEVERITY_NONE;
static std::ofstream log_file;
static std::mutex log_mutex;
void rsimpl::log(rs_log_severity severity, const std::string & message)
{
std::lock_guard<std::mutex> lock(log_mutex);
if(static_cast<int>(severity) < minimum_log_severity) return;
std::time_t t = std::time(nullptr); char buffer[20];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", std::localtime(&t));
if(severity >= minimum_file_severity)
{
switch(severity)
{
case RS_LOG_SEVERITY_DEBUG: log_file << buffer << " DEBUG: " << message << "\n"; break;
case RS_LOG_SEVERITY_INFO: log_file << buffer << " INFO: " << message << "\n"; break;
case RS_LOG_SEVERITY_WARN: log_file << buffer << " WARN: " << message << "\n"; break;
case RS_LOG_SEVERITY_ERROR: log_file << buffer << " ERROR: " << message << "\n"; break;
case RS_LOG_SEVERITY_FATAL: log_file << buffer << " FATAL: " << message << "\n"; break;
default: throw std::logic_error("not a valid severity for log message");
}
}
if(severity >= minimum_console_severity)
{
switch(severity)
{
case RS_LOG_SEVERITY_DEBUG: std::cout << "rs.debug: " << message << "\n"; break;
case RS_LOG_SEVERITY_INFO: std::cout << "rs.info: " << message << "\n"; break;
case RS_LOG_SEVERITY_WARN: std::cout << "rs.warn: " << message << "\n"; break;
case RS_LOG_SEVERITY_ERROR: std::cout << "rs.error: " << message << "\n"; break;
case RS_LOG_SEVERITY_FATAL: std::cout << "rs.fatal: " << message << "\n"; break;
default: throw std::logic_error("not a valid severity for log message");
}
}
}
void rsimpl::log_to_console(rs_log_severity min_severity)
{
minimum_console_severity = min_severity;
rsimpl::minimum_log_severity = std::min(minimum_console_severity, minimum_file_severity);
}
void rsimpl::log_to_file(rs_log_severity min_severity, const char * file_path)
{
minimum_file_severity = min_severity;
log_file.open(file_path, std::ostream::out | std::ostream::app);
rsimpl::minimum_log_severity = std::min(minimum_console_severity, minimum_file_severity);
}