Go to the documentation of this file.00001 #include "types.h"
00002
00003 #include <fstream>
00004 #include <iostream>
00005 #include <algorithm>
00006 #include <ctime>
00007
00008 namespace rsimpl {
00009 class logger_type {
00010 private:
00011 rs_log_severity minimum_log_severity = RS_LOG_SEVERITY_NONE;
00012 rs_log_severity minimum_console_severity = RS_LOG_SEVERITY_NONE;
00013 rs_log_severity minimum_file_severity = RS_LOG_SEVERITY_NONE;
00014 rs_log_severity minimum_callback_severity = RS_LOG_SEVERITY_NONE;
00015
00016 std::mutex log_mutex;
00017 std::ofstream log_file;
00018 log_callback_ptr callback;
00019
00020 public:
00021 logger_type() : callback(nullptr, [](rs_log_callback * ) {}) {}
00022
00023 rs_log_severity get_minimum_severity() { return minimum_log_severity; }
00024
00025 void log_to_console(rs_log_severity min_severity)
00026 {
00027 minimum_console_severity = min_severity;
00028 minimum_log_severity = std::min(minimum_console_severity, minimum_log_severity);
00029 }
00030
00031 void log_to_file(rs_log_severity min_severity, const char * file_path)
00032 {
00033 minimum_file_severity = min_severity;
00034 log_file.open(file_path, std::ostream::out | std::ostream::app);
00035 minimum_log_severity = std::min(minimum_file_severity, minimum_log_severity);
00036 }
00037
00038 void log_to_callback(rs_log_severity min_severity, log_callback_ptr callback)
00039 {
00040 minimum_callback_severity = min_severity;
00041 this->callback = std::move(callback);
00042 minimum_log_severity = std::min(minimum_callback_severity, minimum_log_severity);
00043 }
00044
00045 void log(rs_log_severity severity, const std::string & message)
00046 {
00047 std::lock_guard<std::mutex> lock(log_mutex);
00048
00049 if (static_cast<int>(severity) < minimum_log_severity) return;
00050
00051 std::time_t t = std::time(nullptr); char buffer[20] = {}; const tm* time = std::localtime(&t);
00052 if (nullptr != time)
00053 std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", time);
00054
00055 if (severity >= minimum_file_severity)
00056 {
00057 switch (severity)
00058 {
00059 case RS_LOG_SEVERITY_DEBUG: log_file << buffer << " DEBUG: " << message << "\n"; break;
00060 case RS_LOG_SEVERITY_INFO: log_file << buffer << " INFO: " << message << "\n"; break;
00061 case RS_LOG_SEVERITY_WARN: log_file << buffer << " WARN: " << message << "\n"; break;
00062 case RS_LOG_SEVERITY_ERROR: log_file << buffer << " ERROR: " << message << "\n"; break;
00063 case RS_LOG_SEVERITY_FATAL: log_file << buffer << " FATAL: " << message << "\n"; break;
00064 default: throw std::logic_error("not a valid severity for log message");
00065 }
00066 }
00067
00068 if (severity >= minimum_console_severity)
00069 {
00070 switch (severity)
00071 {
00072 case RS_LOG_SEVERITY_DEBUG: std::cout << "rs.debug: " << message << "\n"; break;
00073 case RS_LOG_SEVERITY_INFO: std::cout << "rs.info: " << message << "\n"; break;
00074 case RS_LOG_SEVERITY_WARN: std::cout << "rs.warn: " << message << "\n"; break;
00075 case RS_LOG_SEVERITY_ERROR: std::cout << "rs.error: " << message << "\n"; break;
00076 case RS_LOG_SEVERITY_FATAL: std::cout << "rs.fatal: " << message << "\n"; break;
00077 default: throw std::logic_error("not a valid severity for log message");
00078 }
00079 }
00080
00081 if (callback && severity >= minimum_callback_severity)
00082 {
00083 callback->on_event(severity, message.c_str());
00084 }
00085 }
00086 };
00087
00088 static logger_type logger;
00089 }
00090
00091 rs_log_severity rsimpl::get_minimum_severity(void)
00092 {
00093 return logger.get_minimum_severity();
00094 }
00095
00096 void rsimpl::log(rs_log_severity severity, const std::string & message)
00097 {
00098 logger.log(severity, message);
00099 }
00100
00101 void rsimpl::log_to_console(rs_log_severity min_severity)
00102 {
00103 logger.log_to_console(min_severity);
00104 }
00105
00106 void rsimpl::log_to_file(rs_log_severity min_severity, const char * file_path)
00107 {
00108 logger.log_to_file(min_severity, file_path);
00109 }
00110
00111 void rsimpl::log_to_callback(rs_log_severity min_severity, rs_log_callback * callback)
00112 {
00113 logger.log_to_callback(min_severity, log_callback_ptr(callback, [](rs_log_callback* c) { c->release(); }));
00114 }
00115
00116 void rsimpl::log_to_callback(rs_log_severity min_severity,
00117 void(*on_log)(rs_log_severity min_severity, const char * message, void * user), void * user)
00118 {
00119 logger.log_to_callback(min_severity, log_callback_ptr(new log_callback(on_log, user), [](rs_log_callback* c) { delete c; }));
00120 }