-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.h
164 lines (136 loc) · 5.15 KB
/
logging.h
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
164
#pragma once
#ifdef BOOST_LOG_DYN_LINK
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/core/record.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
namespace sinks = boost::log::sinks;
namespace attributes = boost::log::attributes;
#define FILE_T (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define BLT BOOST_LOG_TRIVIAL
#define LOG_DEBUG BLT(debug)<<" ("<<FILE_T<<" +"<<__LINE__<<") "<<__FUNCTION__<<" "
#define LOG_INFO BLT(info)<<" ("<<FILE_T<<" +"<<__LINE__<<") "<<__FUNCTION__<<" "
#define LOG_WARN BLT(warning)<<" ("<<FILE_T<<" +"<<__LINE__<<") "<<__FUNCTION__<<" "
#define LOG_ERROR BLT(error)<<" ("<<FILE_T<<" +"<<__LINE__<<") "<<__FUNCTION__<<" "
#define LOG_FATAL BLT(fatal)<<" ("<<FILE_T<<" +"<<__LINE__<<") "<<__FUNCTION__<<" "
static void logging_init(const std::string& logfile, bool debug) {
logging::add_file_log(
keywords::file_name = logfile + "-%Y_%m_%d.log",
keywords::open_mode = (std::ios::out | std::ios::app),
keywords::time_based_rotation=sinks::file::rotation_at_time_point(0,0,0),
keywords::format =(expr::stream
<< "["<<expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")<<"] "
<< "<" << logging::trivial::severity<< ">"<<expr::smessage
),
keywords::auto_flush = true
);
if(debug) {
//logging::add_console_log(std::clog, keywords::format = "%TimeStamp%: %_%");
logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::trace);
} else {
logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);
}
logging::add_common_attributes();
}
static void logging_destroy() {
}
#elif USING_GLOG
#include <iostream>
#include <glog/logging.h>
static void logging_init(const std::string& _logdir, bool debug) {
std::string logdir{_logdir};
if(logdir.empty() || logdir == "/")
logdir = "./log";
FLAGS_log_dir = logdir;
FLAGS_logtostderr = false;
FLAGS_stderrthreshold = 4; // google::FATAL;
//FLAGS_minloglevel = 1;
if(debug)
FLAGS_logbufsecs = 0; // Set log output speed(s)
else
FLAGS_logbufsecs = 30; // Set log output speed(s)
FLAGS_max_log_size = 1024; // M
FLAGS_stop_logging_if_full_disk = true; // If disk is full
google::InitGoogleLogging("log");
google::SetLogDestination(google::GLOG_INFO, std::string(logdir + "/INFO").data());
google::SetLogDestination(google::GLOG_WARNING, std::string(logdir + "/WARN").data());
google::SetLogDestination(google::GLOG_ERROR, std::string(logdir + "/ERROR").data());
google::SetLogFilenameExtension(".glog.");
}
static void logging_destroy() {
google::ShutdownGoogleLogging();
}
#define LOG_DEBUG DLOG(INFO)
#define LOG_INFO LOG(INFO)
#define LOG_WARN LOG(WARNING)
#define LOG_ERROR LOG(ERROR)
#define LOG_FATAL LOG(FATAL)
#else
#include <iostream>
#include <stdlib.h>
#include <time.h>
enum log_rank_t {
DEBUG,
INFO,
WARNING,
ERROR,
FATAL
};
static const char* LOGNAME[] = {
"DEBUG",
"INFO",
"WARN",
"ERROR",
"FATAL"
};
class Logger {
public:
Logger(log_rank_t log_rank) : m_log_rank(log_rank) {};
~Logger() {}
std::ostream& start(log_rank_t log_rank,
const std::string& file,
const int32_t line,
const std::string& function) {
time_t tm = time(NULL);
char time_str[128] = {0};
//char* pts = ctime_r(&tm, time_str);
struct tm t40;
struct tm* t4 = localtime_r(&tm, &t40);
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", t4);
return stream(log_rank) <<std::endl
<< "["<<time_str<<"] "
<< LOGNAME[log_rank]
<< " " <<file
<< " +" << line
<< " (" <<function << ") "
<< std::flush;
}
private:
static std::ostream& stream(log_rank_t log_rank) {
return std::cerr;
}
log_rank_t m_log_rank;
};
#define LOG(log_rank) \
Logger(log_rank).start(log_rank, __FILE__, __LINE__, __FUNCTION__)
#define LOG_DEBUG LOG(DEBUG)
#define LOG_INFO LOG(INFO)
#define LOG_WARN LOG(WARN)
#define LOG_WARNING LOG(WARNING)
#define LOG_ERROR LOG(ERROR)
#define LOG_FATAL LOG(FATAL)
static void logging_init(const std::string& logdir, bool debug) {
}
static void logging_destroy() {
}
#endif