Sunshine master
Self-hosted game stream host for Moonlight.
logging.h
Go to the documentation of this file.
1
5#pragma once
6
7// lib includes
8#include <boost/log/common.hpp>
9#include <boost/log/sinks.hpp>
10
14using text_sink = boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>;
15
16extern boost::log::sources::severity_logger<int> verbose;
17extern boost::log::sources::severity_logger<int> debug;
18extern boost::log::sources::severity_logger<int> info;
19extern boost::log::sources::severity_logger<int> warning;
20extern boost::log::sources::severity_logger<int> error;
21extern boost::log::sources::severity_logger<int> fatal;
22#ifdef SUNSHINE_TESTS
23extern boost::log::sources::severity_logger<int> tests;
24#endif
25
26#include "config.h"
27#include "stat_trackers.h"
28
32namespace logging {
36 class deinit_t {
37 public:
41 ~deinit_t();
42 };
43
50 void deinit();
51
58 void formatter(const boost::log::record_view &view, boost::log::formatting_ostream &os);
59
69 [[nodiscard]] std::unique_ptr<deinit_t> init(int min_log_level, const std::string &log_file);
70
75 void setup_av_logging(int min_log_level);
76
81 void setup_libdisplaydevice_logging(int min_log_level);
82
89 void log_flush();
90
98 void print_help(const char *name);
99
113 template<typename T>
115 public:
124 min_max_avg_periodic_logger(boost::log::sources::severity_logger<int> &severity, std::string_view message, std::string_view units, std::chrono::seconds interval_in_seconds = std::chrono::seconds(20)):
125 severity(severity),
126 message(message),
127 units(units),
128 interval(interval_in_seconds),
129 enabled(config::sunshine.min_log_level <= severity.default_severity()) {
130 }
131
137 void collect_and_log(const T &value) {
138 if (enabled) {
139 auto print_info = [&](const T &min_value, const T &max_value, double avg_value) {
140 auto f = stat_trackers::two_digits_after_decimal();
141 if constexpr (std::is_floating_point_v<T>) {
142 BOOST_LOG(severity.get()) << message << " (min/max/avg): " << f % min_value << units << "/" << f % max_value << units << "/" << f % avg_value << units;
143 } else {
144 BOOST_LOG(severity.get()) << message << " (min/max/avg): " << min_value << units << "/" << max_value << units << "/" << f % avg_value << units;
145 }
146 };
147 tracker.collect_and_callback_on_interval(value, print_info, interval);
148 }
149 }
150
156 void collect_and_log(std::function<T()> func) {
157 if (enabled) {
158 collect_and_log(func());
159 }
160 }
161
165 void reset() {
166 if (enabled) {
167 tracker.reset();
168 }
169 }
170
176 bool is_enabled() const {
177 return enabled;
178 }
179
180 private:
181 std::reference_wrapper<boost::log::sources::severity_logger<int>> severity;
182 std::string message;
183 std::string units;
184 std::chrono::seconds interval;
185 bool enabled;
187 };
188
205 public:
213 time_delta_periodic_logger(boost::log::sources::severity_logger<int> &severity, std::string_view message, std::chrono::seconds interval_in_seconds = std::chrono::seconds(20)):
214 logger(severity, message, "ms", interval_in_seconds) {
215 }
216
222 void first_point(const std::chrono::steady_clock::time_point &point) {
223 if (logger.is_enabled()) {
224 point1 = point;
225 }
226 }
227
232 if (logger.is_enabled()) {
233 first_point(std::chrono::steady_clock::now());
234 }
235 }
236
242 void second_point_and_log(const std::chrono::steady_clock::time_point &point) {
243 if (logger.is_enabled()) {
244 logger.collect_and_log(std::chrono::duration<double, std::milli>(point - point1).count());
245 }
246 }
247
252 if (logger.is_enabled()) {
253 second_point_and_log(std::chrono::steady_clock::now());
254 }
255 }
256
260 void reset() {
261 if (logger.is_enabled()) {
262 logger.reset();
263 }
264 }
265
271 bool is_enabled() const {
272 return logger.is_enabled();
273 }
274
275 private:
276 std::chrono::steady_clock::time_point point1 = std::chrono::steady_clock::now();
278 };
279
285 std::string bracket(const std::string &input);
286
292 std::wstring bracket(const std::wstring &input);
293
294} // namespace logging
RAII helper that runs shutdown cleanup when destroyed.
Definition logging.h:36
~deinit_t()
Restores logging state when the logging subsystem shuts down.
Definition logging.cpp:53
A helper class for tracking and logging numerical values across a period of time.
Definition logging.h:114
void collect_and_log(std::function< T()> func)
Collect a metric sample and write it to the periodic log when due.
Definition logging.h:156
void collect_and_log(const T &value)
Collect a metric sample and write it to the periodic log when due.
Definition logging.h:137
bool is_enabled() const
Check whether this periodic logger should emit at the configured severity.
Definition logging.h:176
void reset()
Reset the object to its initial empty state.
Definition logging.h:165
min_max_avg_periodic_logger(boost::log::sources::severity_logger< int > &severity, std::string_view message, std::string_view units, std::chrono::seconds interval_in_seconds=std::chrono::seconds(20))
Construct a periodic logger that reports min, max, and average samples.
Definition logging.h:124
A helper class for tracking and logging short time intervals across a period of time.
Definition logging.h:204
void reset()
Reset the object to its initial empty state.
Definition logging.h:260
bool is_enabled() const
Check whether this periodic logger should emit at the configured severity.
Definition logging.h:271
void second_point_now_and_log()
Store the current time as the second timestamp and log the elapsed interval.
Definition logging.h:251
void first_point_now()
Store the current time as the first timestamp.
Definition logging.h:231
void second_point_and_log(const std::chrono::steady_clock::time_point &point)
Store the second timestamp and log the elapsed interval.
Definition logging.h:242
void first_point(const std::chrono::steady_clock::time_point &point)
Store the first timestamp for a measured interval.
Definition logging.h:222
time_delta_periodic_logger(boost::log::sources::severity_logger< int > &severity, std::string_view message, std::chrono::seconds interval_in_seconds=std::chrono::seconds(20))
Construct a periodic logger for elapsed-time samples.
Definition logging.h:213
Accumulates minimum, maximum, and average values between periodic callbacks.
Definition stat_trackers.h:35
Declarations for the configuration of Sunshine.
boost::log::sources::severity_logger< int > verbose
Verbose.
Definition logging.cpp:40
boost::log::sources::severity_logger< int > fatal
Fatal.
Definition logging.cpp:45
boost::log::sinks::asynchronous_sink< boost::log::sinks::text_ostream_backend > text_sink
Boost.Log asynchronous text sink used by Sunshine logging.
Definition logging.h:14
boost::log::sources::severity_logger< int > error
Error.
Definition logging.cpp:44
boost::log::sources::severity_logger< int > debug
Debug.
Definition logging.cpp:41
boost::log::sources::severity_logger< int > warning
Warning.
Definition logging.cpp:43
boost::log::sources::severity_logger< int > info
Info.
Definition logging.cpp:42
Handles the initialization and deinitialization of the logging system.
Definition logging.cpp:52
void deinit()
Deinitialize the logging system.
Definition logging.cpp:57
void setup_av_logging(int min_log_level)
Setup AV logging.
Definition logging.cpp:194
std::string bracket(const std::string &input)
Enclose string in square brackets.
Definition logging.cpp:279
void print_help(const char *name)
Print help to stdout.
Definition logging.cpp:259
std::unique_ptr< deinit_t > init(int min_log_level, const std::string &log_file)
Initialize the logging system.
Definition logging.cpp:153
void formatter(const boost::log::record_view &view, boost::log::formatting_ostream &os)
Format a Boost.Log record for Sunshine log output.
Definition logging.cpp:66
void setup_libdisplaydevice_logging(int min_log_level)
Setup logging for libdisplaydevice.
Definition logging.cpp:222
void log_flush()
Flush the log.
Definition logging.cpp:253
Declarations for streaming statistic tracking.