Hermes
0.7.0
Linux game streaming host
Toggle main menu visibility
Loading...
Searching...
No Matches
logging.cpp
Go to the documentation of this file.
1
5
// standard includes
6
#include <fstream>
7
#include <filesystem>
8
#include <iomanip>
9
#include <iostream>
10
11
// lib includes
12
#include <boost/core/null_deleter.hpp>
13
#include <boost/format.hpp>
14
#include <boost/log/attributes/clock.hpp>
15
#include <boost/log/common.hpp>
16
#include <boost/log/expressions.hpp>
17
#include <boost/log/sinks.hpp>
18
#include <boost/log/sources/severity_logger.hpp>
19
20
// local includes
21
#include "
logging.h
"
22
23
// conditional includes
24
#include <display_device/logging.h>
25
26
extern
"C"
{
27
#include <libavutil/log.h>
28
}
29
30
using namespace
std::literals;
31
32
namespace
bl = boost::log;
33
34
boost::shared_ptr<boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>> sink;
35
36
bl::sources::severity_logger<int> verbose(0);
// Dominating output
37
bl::sources::severity_logger<int> debug(1);
// Follow what is happening
38
bl::sources::severity_logger<int> info(2);
// Should be informed about
39
bl::sources::severity_logger<int> warning(3);
// Strange events
40
bl::sources::severity_logger<int> error(4);
// Recoverable errors
41
bl::sources::severity_logger<int> fatal(5);
// Unrecoverable errors
42
#ifdef HERMES_TESTS
43
bl::sources::severity_logger<int> tests(10);
// Automatic tests output
44
#endif
45
46
BOOST_LOG_ATTRIBUTE_KEYWORD(severity,
"Severity"
,
int
)
47
48
namespace
logging
{
49
deinit_t::~deinit_t
() {
50
deinit
();
51
}
52
53
void
deinit
() {
54
log_flush
();
55
bl::core::get()->remove_sink(sink);
56
sink.reset();
57
}
58
59
void
formatter(
const
boost::log::record_view &view, boost::log::formatting_ostream &os) {
60
constexpr
const
char
*message =
"Message"
;
61
constexpr
const
char
*severity =
"Severity"
;
62
63
auto
log_level = view.attribute_values()[severity].extract<
int
>().get();
64
65
std::string_view log_type;
66
switch
(log_level) {
67
case
0:
68
log_type =
"Verbose: "
sv;
69
break
;
70
case
1:
71
log_type =
"Debug: "
sv;
72
break
;
73
case
2:
74
log_type =
"Info: "
sv;
75
break
;
76
case
3:
77
log_type =
"Warning: "
sv;
78
break
;
79
case
4:
80
log_type =
"Error: "
sv;
81
break
;
82
case
5:
83
log_type =
"Fatal: "
sv;
84
break
;
85
#ifdef HERMES_TESTS
86
case
10:
87
log_type =
"Tests: "
sv;
88
break
;
89
#endif
90
};
91
92
auto
now = std::chrono::system_clock::now();
93
auto
ms = std::chrono::duration_cast<std::chrono::milliseconds>(
94
now - std::chrono::time_point_cast<std::chrono::seconds>(now)
95
);
96
97
auto
t = std::chrono::system_clock::to_time_t(now);
98
auto
lt = *std::localtime(&t);
99
100
os <<
"["
sv << std::put_time(<,
"%Y-%m-%d %H:%M:%S."
) << boost::format(
"%03u"
) % ms.count() <<
"]: "
sv
101
<< log_type << view.attribute_values()[message].extract<std::string>();
102
}
103
104
[[nodiscard]] std::unique_ptr<deinit_t>
init
(
int
min_log_level,
const
std::string &log_file) {
105
if
(sink) {
106
// Deinitialize the logging system before reinitializing it. This can probably only ever be hit in tests.
107
deinit
();
108
}
109
110
// Check if the log file exists and handle backup
111
std::string backup_log_file = log_file +
".backup"
;
112
if
(std::filesystem::exists(log_file)) {
113
try
{
114
// If the backup file exists, remove it
115
if
(std::filesystem::exists(backup_log_file)) {
116
std::filesystem::remove(backup_log_file);
117
}
118
// Rename the current log file to the backup name
119
std::filesystem::rename(log_file, backup_log_file);
120
}
catch
(std::exception& e) {
121
std::cout <<
"Failed to rotate log file: "
<< e.what() << std::endl;
122
}
123
}
124
125
setup_av_logging
(min_log_level);
126
setup_libdisplaydevice_logging
(min_log_level);
127
128
sink = boost::make_shared<text_sink>();
129
130
#ifndef HERMES_TESTS
131
boost::shared_ptr<std::ostream> stream {&std::cout, boost::null_deleter()};
132
sink->locked_backend()->add_stream(stream);
133
#endif
134
135
sink->locked_backend()->add_stream(boost::make_shared<std::ofstream>(log_file));
136
sink->set_filter(severity >= min_log_level);
137
sink->set_formatter(&formatter);
138
139
// Flush after each log record to ensure log file contents on disk isn't stale.
140
sink->locked_backend()->auto_flush(
true
);
141
142
bl::core::get()->add_sink(sink);
143
144
return
std::make_unique<deinit_t>();
145
}
146
147
void
setup_av_logging
(
int
min_log_level) {
148
if
(min_log_level >= 1) {
149
av_log_set_level(AV_LOG_QUIET);
150
}
else
{
151
av_log_set_level(AV_LOG_DEBUG);
152
}
153
av_log_set_callback([](
void
*ptr,
int
level,
const
char
*fmt, va_list vl) {
154
static
int
print_prefix = 1;
155
char
buffer[1024];
156
157
av_log_format_line(ptr, level, fmt, vl, buffer,
sizeof
(buffer), &print_prefix);
158
if
(level <= AV_LOG_ERROR) {
159
// We print AV_LOG_FATAL at the error level. FFmpeg prints things as fatal that
160
// are expected in some cases, such as lack of codec support or similar things.
161
BOOST_LOG(error) << buffer;
162
}
else
if
(level <= AV_LOG_WARNING) {
163
BOOST_LOG(warning) << buffer;
164
}
else
if
(level <= AV_LOG_INFO) {
165
BOOST_LOG(info) << buffer;
166
}
else
if
(level <= AV_LOG_VERBOSE) {
167
// AV_LOG_VERBOSE is less verbose than AV_LOG_DEBUG
168
BOOST_LOG(debug) << buffer;
169
}
else
{
170
BOOST_LOG(verbose) << buffer;
171
}
172
});
173
}
174
175
void
setup_libdisplaydevice_logging
(
int
min_log_level) {
176
constexpr
int
min_level {
static_cast<
int
>
(display_device::Logger::LogLevel::verbose)};
177
constexpr
int
max_level {
static_cast<
int
>
(display_device::Logger::LogLevel::fatal)};
178
const
auto
log_level {
static_cast<
display_device::Logger::LogLevel
>
(std::min(std::max(min_level, min_log_level), max_level))};
179
180
display_device::Logger::get().setLogLevel(log_level);
181
display_device::Logger::get().setCustomCallback([](
const
display_device::Logger::LogLevel level,
const
std::string &message) {
182
switch
(level) {
183
case
display_device::Logger::LogLevel::verbose:
184
BOOST_LOG(verbose) << message;
185
break
;
186
case
display_device::Logger::LogLevel::debug:
187
BOOST_LOG(debug) << message;
188
break
;
189
case
display_device::Logger::LogLevel::info:
190
BOOST_LOG(info) << message;
191
break
;
192
case
display_device::Logger::LogLevel::warning:
193
BOOST_LOG(warning) << message;
194
break
;
195
case
display_device::Logger::LogLevel::error:
196
BOOST_LOG(error) << message;
197
break
;
198
case
display_device::Logger::LogLevel::fatal:
199
BOOST_LOG(fatal) << message;
200
break
;
201
}
202
});
203
}
204
205
void
log_flush
() {
206
if
(sink) {
207
sink->flush();
208
}
209
}
210
211
void
print_help
(
const
char
*name) {
212
std::cout
213
<<
"Usage: "
sv << name <<
" [options] [/path/to/configuration_file] [--cmd]"
sv << std::endl
214
<<
" Any configurable option can be overwritten with: \"name=value\""
sv << std::endl
215
<< std::endl
216
<<
" Note: The configuration will be created if it doesn't exist."
sv << std::endl
217
<< std::endl
218
<<
" --help | print help"
sv << std::endl
219
<<
" --creds username password | set user credentials for the Web manager"
sv << std::endl
220
<<
" --version | print the version of Hermes"
sv << std::endl
221
<<
" --prelogin status|enable|disable"
sv << std::endl
222
<<
" | check or set up being reachable before anyone logs in"
sv << std::endl
223
<< std::endl
224
<<
" flags"
sv << std::endl
225
<<
" -0 | Read PIN from stdin"
sv << std::endl
226
<<
" -1 | Do not load previously saved state and do retain any state after shutdown"
sv << std::endl
227
<<
" | Effectively starting as if for the first time without overwriting any pairings with your devices"
sv << std::endl
228
<<
" -2 | Force replacement of headers in video stream"
sv << std::endl
229
<<
" -p | Enable/Disable UPnP"
sv << std::endl
230
<< std::endl;
231
}
232
233
std::string
bracket
(
const
std::string &input) {
234
return
"["
s + input +
"]"
s;
235
}
236
237
std::wstring
bracket
(
const
std::wstring &input) {
238
return
L
"["
s + input + L
"]"
s;
239
}
240
241
}
// namespace logging
logging::deinit_t::~deinit_t
~deinit_t()
A destructor that restores the initial state.
Definition
logging.cpp:49
logging.h
Declarations for logging related functions.
logging
Handles the initialization and deinitialization of the logging system.
Definition
logging.cpp:48
logging::deinit
void deinit()
Deinitialize the logging system.
Definition
logging.cpp:53
logging::setup_av_logging
void setup_av_logging(int min_log_level)
Setup AV logging.
Definition
logging.cpp:147
logging::bracket
std::string bracket(const std::string &input)
Enclose string in square brackets.
Definition
logging.cpp:233
logging::print_help
void print_help(const char *name)
Print help to stdout.
Definition
logging.cpp:211
logging::init
std::unique_ptr< deinit_t > init(int min_log_level, const std::string &log_file)
Initialize the logging system.
Definition
logging.cpp:104
logging::setup_libdisplaydevice_logging
void setup_libdisplaydevice_logging(int min_log_level)
Setup logging for libdisplaydevice.
Definition
logging.cpp:175
logging::log_flush
void log_flush()
Flush the log.
Definition
logging.cpp:205
src
logging.cpp
Generated by
1.18.0