log.cpp
Go to the documentation of this file.
1 #include "types.h"
2 
3 #include <fstream>
4 #include <iostream>
5 #include <algorithm>
6 #include <ctime>
7 
8 namespace rsimpl {
9  class logger_type {
10  private:
15 
16  std::mutex log_mutex;
17  std::ofstream log_file;
19 
20  public:
21  logger_type() : callback(nullptr, [](rs_log_callback * /*c*/) {}) {}
22 
24 
25  void log_to_console(rs_log_severity min_severity)
26  {
27  minimum_console_severity = min_severity;
28  minimum_log_severity = std::min(minimum_console_severity, minimum_log_severity);
29  }
30 
31  void log_to_file(rs_log_severity min_severity, const char * file_path)
32  {
33  minimum_file_severity = min_severity;
34  log_file.open(file_path, std::ostream::out | std::ostream::app);
35  minimum_log_severity = std::min(minimum_file_severity, minimum_log_severity);
36  }
37 
38  void log_to_callback(rs_log_severity min_severity, log_callback_ptr callback)
39  {
40  minimum_callback_severity = min_severity;
41  this->callback = std::move(callback);
42  minimum_log_severity = std::min(minimum_callback_severity, minimum_log_severity);
43  }
44 
46  {
47  std::lock_guard<std::mutex> lock(log_mutex);
48 
49  if (static_cast<int>(severity) < minimum_log_severity) return;
50 
51  std::time_t t = std::time(nullptr); char buffer[20] = {}; const tm* time = std::localtime(&t);
52  if (nullptr != time)
53  std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", time);
54 
55  if (severity >= minimum_file_severity)
56  {
57  switch (severity)
58  {
59  case RS_LOG_SEVERITY_DEBUG: log_file << buffer << " DEBUG: " << message << "\n"; break;
60  case RS_LOG_SEVERITY_INFO: log_file << buffer << " INFO: " << message << "\n"; break;
61  case RS_LOG_SEVERITY_WARN: log_file << buffer << " WARN: " << message << "\n"; break;
62  case RS_LOG_SEVERITY_ERROR: log_file << buffer << " ERROR: " << message << "\n"; break;
63  case RS_LOG_SEVERITY_FATAL: log_file << buffer << " FATAL: " << message << "\n"; break;
64  default: throw std::logic_error("not a valid severity for log message");
65  }
66  }
67 
68  if (severity >= minimum_console_severity)
69  {
70  switch (severity)
71  {
72  case RS_LOG_SEVERITY_DEBUG: std::cout << "rs.debug: " << message << "\n"; break;
73  case RS_LOG_SEVERITY_INFO: std::cout << "rs.info: " << message << "\n"; break;
74  case RS_LOG_SEVERITY_WARN: std::cout << "rs.warn: " << message << "\n"; break;
75  case RS_LOG_SEVERITY_ERROR: std::cout << "rs.error: " << message << "\n"; break;
76  case RS_LOG_SEVERITY_FATAL: std::cout << "rs.fatal: " << message << "\n"; break;
77  default: throw std::logic_error("not a valid severity for log message");
78  }
79  }
80 
81  if (callback && severity >= minimum_callback_severity)
82  {
83  callback->on_event(severity, message.c_str());
84  }
85  }
86  };
87 
89 }
90 
92 {
94 }
95 
97 {
98  logger.log(severity, message);
99 }
100 
102 {
103  logger.log_to_console(min_severity);
104 }
105 
106 void rsimpl::log_to_file(rs_log_severity min_severity, const char * file_path)
107 {
108  logger.log_to_file(min_severity, file_path);
109 }
110 
112 {
113  logger.log_to_callback(min_severity, log_callback_ptr(callback, [](rs_log_callback* c) { c->release(); }));
114 }
115 
117  void(*on_log)(rs_log_severity min_severity, const char * message, void * user), void * user)
118 {
119  logger.log_to_callback(min_severity, log_callback_ptr(new log_callback(on_log, user), [](rs_log_callback* c) { delete c; }));
120 }
static logger_type logger
Definition: log.cpp:88
void log_to_console(rs_log_severity min_severity)
Definition: log.cpp:25
void log(rs_log_severity severity, const std::string &message)
Definition: log.cpp:45
rs_log_severity get_minimum_severity()
Definition: log.cpp:91
GLuint GLsizei const GLchar * message
Definition: glext.h:2482
GLsizei const GLchar *const * string
Definition: glext.h:683
rs_log_severity minimum_file_severity
Definition: log.cpp:13
Definition: archive.h:12
void log_to_file(rs_log_severity min_severity, const char *file_path)
Definition: log.cpp:106
rs_log_severity minimum_log_severity
Definition: log.cpp:11
GLuint buffer
Definition: glext.h:528
virtual void release()=0
void log_to_console(rs_log_severity min_severity)
Definition: log.cpp:101
const GLubyte * c
Definition: glext.h:11542
void log_to_callback(rs_log_severity min_severity, rs_log_callback *callback)
Definition: log.cpp:111
log_callback_ptr callback
Definition: log.cpp:18
rs_log_severity get_minimum_severity()
Definition: log.cpp:23
std::unique_ptr< rs_log_callback, void(*)(rs_log_callback *)> log_callback_ptr
Definition: types.h:426
rs_log_severity minimum_console_severity
Definition: log.cpp:12
GLdouble GLdouble t
Definition: glext.h:239
std::ofstream log_file
Definition: log.cpp:17
std::mutex log_mutex
Definition: log.cpp:16
rs_log_severity minimum_callback_severity
Definition: log.cpp:14
void log(rs_log_severity severity, const std::string &message)
Definition: log.cpp:96
rs_log_severity
Severity of the librealsense logger.
Definition: rs.h:265
void log_to_file(rs_log_severity min_severity, const char *file_path)
Definition: log.cpp:31
GLenum GLenum severity
Definition: glext.h:2478
void log_to_callback(rs_log_severity min_severity, log_callback_ptr callback)
Definition: log.cpp:38


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Fri Mar 13 2020 03:16:17