Sunshine latest
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
11using text_sink = boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>;
12
13extern boost::log::sources::severity_logger<int> verbose;
14extern boost::log::sources::severity_logger<int> debug;
15extern boost::log::sources::severity_logger<int> info;
16extern boost::log::sources::severity_logger<int> warning;
17extern boost::log::sources::severity_logger<int> error;
18extern boost::log::sources::severity_logger<int> fatal;
19#ifdef SUNSHINE_TESTS
20extern boost::log::sources::severity_logger<int> tests;
21#endif
22
23#include "config.h"
24#include "stat_trackers.h"
25
29namespace logging {
30 class deinit_t {
31 public:
35 ~deinit_t();
36 };
37
44 void deinit();
45
46 void formatter(const boost::log::record_view &view, boost::log::formatting_ostream &os);
47
57 [[nodiscard]] std::unique_ptr<deinit_t> init(int min_log_level, const std::string &log_file);
58
63 void setup_av_logging(int min_log_level);
64
69 void setup_libdisplaydevice_logging(int min_log_level);
70
77 void log_flush();
78
86 void print_help(const char *name);
87
101 template<typename T>
103 public:
104 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)):
105 severity(severity),
106 message(message),
107 units(units),
108 interval(interval_in_seconds),
109 enabled(config::sunshine.min_log_level <= severity.default_severity()) {
110 }
111
112 void collect_and_log(const T &value) {
113 if (enabled) {
114 auto print_info = [&](const T &min_value, const T &max_value, double avg_value) {
115 auto f = stat_trackers::two_digits_after_decimal();
116 if constexpr (std::is_floating_point_v<T>) {
117 BOOST_LOG(severity.get()) << message << " (min/max/avg): " << f % min_value << units << "/" << f % max_value << units << "/" << f % avg_value << units;
118 } else {
119 BOOST_LOG(severity.get()) << message << " (min/max/avg): " << min_value << units << "/" << max_value << units << "/" << f % avg_value << units;
120 }
121 };
122 tracker.collect_and_callback_on_interval(value, print_info, interval);
123 }
124 }
125
126 void collect_and_log(std::function<T()> func) {
127 if (enabled) {
128 collect_and_log(func());
129 }
130 }
131
132 void reset() {
133 if (enabled) {
134 tracker.reset();
135 }
136 }
137
138 bool is_enabled() const {
139 return enabled;
140 }
141
142 private:
143 std::reference_wrapper<boost::log::sources::severity_logger<int>> severity;
144 std::string message;
145 std::string units;
146 std::chrono::seconds interval;
147 bool enabled;
149 };
150
167 public:
168 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)):
169 logger(severity, message, "ms", interval_in_seconds) {
170 }
171
172 void first_point(const std::chrono::steady_clock::time_point &point) {
173 if (logger.is_enabled()) {
174 point1 = point;
175 }
176 }
177
178 void first_point_now() {
179 if (logger.is_enabled()) {
180 first_point(std::chrono::steady_clock::now());
181 }
182 }
183
184 void second_point_and_log(const std::chrono::steady_clock::time_point &point) {
185 if (logger.is_enabled()) {
186 logger.collect_and_log(std::chrono::duration<double, std::milli>(point - point1).count());
187 }
188 }
189
190 void second_point_now_and_log() {
191 if (logger.is_enabled()) {
192 second_point_and_log(std::chrono::steady_clock::now());
193 }
194 }
195
196 void reset() {
197 if (logger.is_enabled()) {
198 logger.reset();
199 }
200 }
201
202 bool is_enabled() const {
203 return logger.is_enabled();
204 }
205
206 private:
207 std::chrono::steady_clock::time_point point1 = std::chrono::steady_clock::now();
209 };
210
216 std::string bracket(const std::string &input);
217
223 std::wstring bracket(const std::wstring &input);
224
225} // namespace logging
Definition logging.h:30
~deinit_t()
A destructor that restores the initial state.
Definition logging.cpp:46
A helper class for tracking and logging numerical values across a period of time.
Definition logging.h:102
A helper class for tracking and logging short time intervals across a period of time.
Definition logging.h:166
Definition stat_trackers.h:22
Declarations for the configuration of Sunshine.
Handles the initialization and deinitialization of the logging system.
Definition logging.cpp:45
void deinit()
Deinitialize the logging system.
Definition logging.cpp:50
void setup_av_logging(int min_log_level)
Setup AV logging.
Definition logging.cpp:128
std::string bracket(const std::string &input)
Enclose string in square brackets.
Definition logging.cpp:212
void print_help(const char *name)
Print help to stdout.
Definition logging.cpp:192
std::unique_ptr< deinit_t > init(int min_log_level, const std::string &log_file)
Initialize the logging system.
Definition logging.cpp:101
void setup_libdisplaydevice_logging(int min_log_level)
Setup logging for libdisplaydevice.
Definition logging.cpp:156
void log_flush()
Flush the log.
Definition logging.cpp:186
Declarations for streaming statistic tracking.