12#include <system_error>
15#include <boost/log/common.hpp>
16#include <boost/log/sinks.hpp>
21using text_sink = boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>;
23extern boost::log::sources::severity_logger<int>
verbose;
24extern boost::log::sources::severity_logger<int>
debug;
25extern boost::log::sources::severity_logger<int>
info;
26extern boost::log::sources::severity_logger<int>
warning;
27extern boost::log::sources::severity_logger<int>
error;
28extern boost::log::sources::severity_logger<int>
fatal;
30extern boost::log::sources::severity_logger<int> tests;
54 inline std::error_code
rotate_log_file(
const std::filesystem::path &log_file)
noexcept {
55 const auto rotated_log_path = [&log_file](std::size_t generation) {
56 auto rotated_path = log_file;
57 rotated_path += std::format(
".{}", generation);
65 const auto previous_path = rotated_log_path(generation - 1);
66 if (std::filesystem::exists(previous_path)) {
67 std::filesystem::rename(previous_path, rotated_log_path(generation));
71 if (std::filesystem::exists(log_file)) {
72 std::filesystem::rename(log_file, rotated_log_path(1));
76 }
catch (
const std::filesystem::filesystem_error &filesystem_error) {
77 return filesystem_error.code();
106 void formatter(
const boost::log::record_view &view, boost::log::formatting_ostream &os);
117 [[nodiscard]] std::unique_ptr<deinit_t>
init(
int min_log_level,
const std::string &log_file);
172 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)):
176 interval(interval_in_seconds),
177 enabled(config::sunshine.min_log_level <= severity.default_severity()) {
187 auto print_info = [&](
const T &min_value,
const T &max_value,
double avg_value) {
189 if constexpr (std::is_floating_point_v<T>) {
190 BOOST_LOG(severity.get()) << message <<
" (min/max/avg): " << f % min_value << units <<
"/" << f % max_value << units <<
"/" << f % avg_value << units;
192 BOOST_LOG(severity.get()) << message <<
" (min/max/avg): " << min_value << units <<
"/" << max_value << units <<
"/" << f % avg_value << units;
195 tracker.collect_and_callback_on_interval(value, print_info, interval);
229 std::reference_wrapper<boost::log::sources::severity_logger<int>> severity;
232 std::chrono::seconds interval;
261 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)):
262 logger(severity, message,
"ms", interval_in_seconds) {
270 void first_point(
const std::chrono::steady_clock::time_point &point) {
271 if (logger.is_enabled()) {
280 if (logger.is_enabled()) {
291 if (logger.is_enabled()) {
292 logger.collect_and_log(std::chrono::duration<double, std::milli>(point - point1).count());
300 if (logger.is_enabled()) {
309 if (logger.is_enabled()) {
320 return logger.is_enabled();
324 std::chrono::steady_clock::time_point point1 = std::chrono::steady_clock::now();
333 std::string
bracket(
const std::string &input);
340 std::wstring
bracket(
const std::wstring &input);
RAII helper that runs shutdown cleanup when destroyed.
Definition logging.h:84
A helper class for tracking and logging numerical values across a period of time.
Definition logging.h:162
void collect_and_log(std::function< T()> func)
Collect a metric sample and write it to the periodic log when due.
Definition logging.h:204
void collect_and_log(const T &value)
Collect a metric sample and write it to the periodic log when due.
Definition logging.h:185
bool is_enabled() const
Check whether this periodic logger should emit at the configured severity.
Definition logging.h:224
void reset()
Reset the object to its initial empty state.
Definition logging.h:213
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:172
void reset()
Reset the object to its initial empty state.
Definition logging.h:308
bool is_enabled() const
Check whether this periodic logger should emit at the configured severity.
Definition logging.h:319
void second_point_now_and_log()
Store the current time as the second timestamp and log the elapsed interval.
Definition logging.h:299
void first_point_now()
Store the current time as the first timestamp.
Definition logging.h:279
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:290
void first_point(const std::chrono::steady_clock::time_point &point)
Store the first timestamp for a measured interval.
Definition logging.h:270
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:261
Accumulates minimum, maximum, and average values between periodic callbacks.
Definition stat_trackers.h:37
Declarations for the configuration of Sunshine.
bl::sources::severity_logger< int > debug
Follow what is happening.
Definition logging.cpp:41
bl::sources::severity_logger< int > error
Recoverable errors.
Definition logging.cpp:44
bl::sources::severity_logger< int > fatal
Unrecoverable errors.
Definition logging.cpp:45
bl::sources::severity_logger< int > info
Should be informed about.
Definition logging.cpp:42
bl::sources::severity_logger< int > verbose
Dominating output.
Definition logging.cpp:40
bl::sources::severity_logger< int > warning
Strange events.
Definition logging.cpp:43
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:21
Handles the initialization and deinitialization of the logging system.
Definition logging.cpp:52
void deinit()
Deinitialize the logging system.
Definition logging.cpp:57
constexpr std::size_t retained_log_file_count
The number of previous log files retained during rotation.
Definition logging.h:43
void setup_av_logging(int min_log_level)
Setup AV logging.
Definition logging.cpp:199
std::string bracket(const std::string &input)
Enclose string in square brackets.
Definition logging.cpp:284
void print_help(const char *name)
Print help to stdout.
Definition logging.cpp:264
std::unique_ptr< deinit_t > init(int min_log_level, const std::string &log_file)
Rotate the current log file and 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:227
void log_flush()
Flush the log.
Definition logging.cpp:258
std::error_code rotate_log_file(const std::filesystem::path &log_file) noexcept
Rotate a log file while retaining up to five previous logs.
Definition logging.h:54
boost::format two_digits_after_decimal()
Create a Boost formatter with two fractional digits.
Definition stat_trackers.cpp:20
Declarations for streaming statistic tracking.