This repository was archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLogger.cpp
216 lines (185 loc) · 6.62 KB
/
Logger.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* -*- Mode: c++ -*-
*
* Copyright (C) John Poet 2018
*
* This file is part of HauppaugeUSB.
*
* HauppaugeUSB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HauppaugeUSB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HauppaugeUSB. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Logger.h"
#include <boost/log/core/core.hpp>
#include <boost/log/expressions/formatters/date_time.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/sinks/unlocked_frontend.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/trivial.hpp>
#include <boost/core/null_deleter.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include "boost/format.hpp"
#include <fstream>
#include <ostream>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
static const char* SeverityStr[] =
{
"DEBG",
"INFO",
"NOTC",
"WARN",
"ERRR",
"CRIT"
};
bool DisableConsole = false;
void disableConsoleLog(void)
{
DisableConsole = true;
}
std::string LogFilePath = "hauppauge2.log";
void setLogFilePath(const std::string & path)
{
LogFilePath = path;
}
BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime)
BOOST_LOG_ATTRIBUTE_KEYWORD(Severity, "Severity", SeverityLvl)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_thread_name, "ThreadName", std::string)
// Allow a thread to declare its name
void setThreadName(const char *name)
{
logging::core::get()->add_thread_attribute("ThreadName",
attrs::constant<std::string>(name));
}
void setLogLevelFilter(SeverityLvl level)
{
CRITLOG << "Changing loglevel to " << SeverityStr[level];
logging::core::get()->set_filter(Severity >= level);
}
void setLogLevelFilter(const std::string& lvl)
{
std::string level(lvl);
boost::to_upper(level);
if (level == "CRITICAL")
setLogLevelFilter(SeverityLvl::CRITICAL);
else if (level == "ERROR")
setLogLevelFilter(SeverityLvl::ERROR);
else if (level == "WARNING")
setLogLevelFilter(SeverityLvl::WARNING);
else if (level == "NOTICE")
setLogLevelFilter(SeverityLvl::NOTICE);
else if (level == "INFO")
setLogLevelFilter(SeverityLvl::INFO);
else
setLogLevelFilter(SeverityLvl::DEBUG);
}
template< typename CharT, typename TraitsT>
inline std::basic_istream< CharT, TraitsT >& operator>> (
std::basic_istream< CharT, TraitsT >& strm, SeverityLvl &lvl)
{
std::string tmp;
strm >> tmp;
// can make it case insensitive to allow 'warning' instead of only 'WARNING'
// std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::toupper);
// if you have a lot of levels you may want use a map instead
if (tmp.compare("INFO") == 0)
lvl = SeverityLvl::INFO;
else if (tmp.compare("NOTICE") == 0)
lvl = SeverityLvl::NOTICE;
else if (tmp.compare("WARNING") == 0)
lvl = SeverityLvl::WARNING;
else if (tmp.compare("ERROR") == 0)
lvl = SeverityLvl::ERROR;
else if (tmp.compare("CRITICAL") == 0)
lvl = SeverityLvl::CRITICAL;
// provide a default value for invalid strings
else
lvl = SeverityLvl::DEBUG;
return strm;
}
// The operator is used for regular stream formatting
std::ostream& operator<< (std::ostream& strm, SeverityLvl level)
{
if (static_cast< std::size_t >(level) <
sizeof(SeverityStr) / sizeof(*SeverityStr))
strm << SeverityStr[level];
else
strm << static_cast< int >(level);
return strm;
}
// Attribute value tag type
struct severity_tag;
// The operator is used when putting the severity level to log
logging::formatting_ostream& operator<<
(
logging::formatting_ostream& strm,
logging::to_log_manip< SeverityLvl, severity_tag > const& manip
)
{
SeverityLvl level = manip.get();
if (static_cast< std::size_t >(level) <
sizeof(SeverityStr) / sizeof(*SeverityStr))
strm << SeverityStr[level];
else
strm << static_cast< int >(level);
return strm;
}
BOOST_LOG_GLOBAL_LOGGER_INIT(logger, src::severity_logger_mt)
{
src::severity_logger_mt<SeverityLvl> logger;
logging::add_common_attributes();
// add attributes
// lines are sequentially numbered
logger.add_attribute("LineID", attrs::counter<unsigned int>(1));
// each log line gets a timestamp
logger.add_attribute("TimeStamp", attrs::local_clock());
// specify the format of the log message
logging::formatter formatter =
(expr::stream
<< expr::format_date_time<boost::posix_time::ptime>
("TimeStamp", "%Y-%m-%dT%H:%M:%S.%f") << " "
<< expr::attr< SeverityLvl, severity_tag >("Severity")
// << boost::log::expressions::attr <boost::log::attributes::current_thread_id::value_type> ("ThreadID") << ", "
<< " <" << a_thread_name << "> "
<< expr::attr<std::string>("a_FileName")
<< ':' << expr::attr<int>("a_LineNum") << " ("
<< expr::attr<std::string>("a_Function")
<< ") "
<< expr::smessage);
boost::shared_ptr< logging::core > core = logging::core::get();
using backend_t = sinks::text_ostream_backend;
using sink_t = sinks::synchronous_sink< backend_t >;
boost::shared_ptr< backend_t > backend(new backend_t());
if (!DisableConsole)
{
backend->add_stream(boost::shared_ptr< std::ostream >
(&std::clog, boost::null_deleter()));
}
backend->add_stream(boost::shared_ptr< std::ostream >
(new std::ofstream(LogFilePath)));
boost::shared_ptr< sink_t > file_sink(new sink_t(backend));
file_sink->set_formatter(formatter);
core->add_sink(file_sink);
// Enable auto-flushing after each log record written
backend->auto_flush(true);
core->set_filter(Severity >= SeverityLvl::INFO);
return logger;
}