refactor(logging): separate logging from main (#2110)

This commit is contained in:
ReenigneArcher 2024-02-07 09:59:24 -05:00 committed by GitHub
commit 0aa4f06c39
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
50 changed files with 161 additions and 80 deletions

View file

@ -45,6 +45,8 @@ set(SUNSHINE_TARGET_FILES
"${CMAKE_SOURCE_DIR}/src/uuid.h"
"${CMAKE_SOURCE_DIR}/src/config.h"
"${CMAKE_SOURCE_DIR}/src/config.cpp"
"${CMAKE_SOURCE_DIR}/src/logging.cpp"
"${CMAKE_SOURCE_DIR}/src/logging.h"
"${CMAKE_SOURCE_DIR}/src/main.cpp"
"${CMAKE_SOURCE_DIR}/src/main.h"
"${CMAKE_SOURCE_DIR}/src/crypto.cpp"

View file

@ -0,0 +1,5 @@
logging
=======
.. doxygenfile:: logging.h
:allow-dot-graphs:

View file

@ -10,6 +10,7 @@
#include "audio.h"
#include "config.h"
#include "logging.h"
#include "main.h"
#include "thread_safe.h"
#include "utility.h"

View file

@ -11,7 +11,7 @@ extern "C" {
}
#include "cbs.h"
#include "main.h"
#include "logging.h"
#include "utility.h"
using namespace std::literals;

View file

@ -15,6 +15,7 @@
#include <boost/property_tree/ptree.hpp>
#include "config.h"
#include "logging.h"
#include "main.h"
#include "nvhttp.h"
#include "rtsp.h"

View file

@ -30,6 +30,7 @@
#include "confighttp.h"
#include "crypto.h"
#include "httpcommon.h"
#include "logging.h"
#include "main.h"
#include "network.h"
#include "nvhttp.h"

View file

@ -22,6 +22,7 @@
#include "config.h"
#include "crypto.h"
#include "httpcommon.h"
#include "logging.h"
#include "main.h"
#include "network.h"
#include "nvhttp.h"

View file

@ -18,6 +18,7 @@ extern "C" {
#include "config.h"
#include "input.h"
#include "logging.h"
#include "main.h"
#include "platform/common.h"
#include "thread_pool.h"

73
src/logging.cpp Normal file
View file

@ -0,0 +1,73 @@
/**
* @file src/logging.cpp
* @brief Logging implementation file for the Sunshine application.
*/
// standard includes
#include <iostream>
// lib includes
#include <boost/log/attributes/clock.hpp>
#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/sources/severity_logger.hpp>
// local includes
#include "logging.h"
using namespace std::literals;
namespace bl = boost::log;
boost::shared_ptr<boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>> sink;
bl::sources::severity_logger<int> verbose(0); // Dominating output
bl::sources::severity_logger<int> debug(1); // Follow what is happening
bl::sources::severity_logger<int> info(2); // Should be informed about
bl::sources::severity_logger<int> warning(3); // Strange events
bl::sources::severity_logger<int> error(4); // Recoverable errors
bl::sources::severity_logger<int> fatal(5); // Unrecoverable errors
/**
* @brief Flush the log.
*
* EXAMPLES:
* ```cpp
* log_flush();
* ```
*/
void
log_flush() {
sink->flush();
}
/**
* @brief Print help to stdout.
* @param name The name of the program.
*
* EXAMPLES:
* ```cpp
* print_help("sunshine");
* ```
*/
void
print_help(const char *name) {
std::cout
<< "Usage: "sv << name << " [options] [/path/to/configuration_file] [--cmd]"sv << std::endl
<< " Any configurable option can be overwritten with: \"name=value\""sv << std::endl
<< std::endl
<< " Note: The configuration will be created if it doesn't exist."sv << std::endl
<< std::endl
<< " --help | print help"sv << std::endl
<< " --creds username password | set user credentials for the Web manager"sv << std::endl
<< " --version | print the version of sunshine"sv << std::endl
<< std::endl
<< " flags"sv << std::endl
<< " -0 | Read PIN from stdin"sv << std::endl
<< " -1 | Do not load previously saved state and do retain any state after shutdown"sv << std::endl
<< " | Effectively starting as if for the first time without overwriting any pairings with your devices"sv << std::endl
<< " -2 | Force replacement of headers in video stream"sv << std::endl
<< " -p | Enable/Disable UPnP"sv << std::endl
<< std::endl;
}

27
src/logging.h Normal file
View file

@ -0,0 +1,27 @@
/**
* @file src/logging.h
* @brief Logging header file for the Sunshine application.
*/
// macros
#pragma once
// lib includes
#include <boost/log/common.hpp>
#include <boost/log/sinks.hpp>
extern boost::shared_ptr<boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>> sink;
using text_sink = boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>;
extern boost::log::sources::severity_logger<int> verbose;
extern boost::log::sources::severity_logger<int> debug;
extern boost::log::sources::severity_logger<int> info;
extern boost::log::sources::severity_logger<int> warning;
extern boost::log::sources::severity_logger<int> error;
extern boost::log::sources::severity_logger<int> fatal;
// functions
void
log_flush();
void
print_help(const char *name);

View file

@ -21,6 +21,7 @@
#include "config.h"
#include "confighttp.h"
#include "httpcommon.h"
#include "logging.h"
#include "main.h"
#include "nvhttp.h"
#include "platform/common.h"
@ -52,18 +53,9 @@ nvprefs::nvprefs_interface nvprefs_instance;
#endif
thread_pool_util::ThreadPool task_pool;
bl::sources::severity_logger<int> verbose(0); // Dominating output
bl::sources::severity_logger<int> debug(1); // Follow what is happening
bl::sources::severity_logger<int> info(2); // Should be informed about
bl::sources::severity_logger<int> warning(3); // Strange events
bl::sources::severity_logger<int> error(4); // Recoverable errors
bl::sources::severity_logger<int> fatal(5); // Unrecoverable errors
bool display_cursor = true;
using text_sink = bl::sinks::asynchronous_sink<bl::sinks::text_ostream_backend>;
boost::shared_ptr<text_sink> sink;
struct NoDelete {
void
operator()(void *) {}
@ -71,36 +63,6 @@ struct NoDelete {
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", int)
/**
* @brief Print help to stdout.
* @param name The name of the program.
*
* EXAMPLES:
* ```cpp
* print_help("sunshine");
* ```
*/
void
print_help(const char *name) {
std::cout
<< "Usage: "sv << name << " [options] [/path/to/configuration_file] [--cmd]"sv << std::endl
<< " Any configurable option can be overwritten with: \"name=value\""sv << std::endl
<< std::endl
<< " Note: The configuration will be created if it doesn't exist."sv << std::endl
<< std::endl
<< " --help | print help"sv << std::endl
<< " --creds username password | set user credentials for the Web manager"sv << std::endl
<< " --version | print the version of sunshine"sv << std::endl
<< std::endl
<< " flags"sv << std::endl
<< " -0 | Read PIN from stdin"sv << std::endl
<< " -1 | Do not load previously saved state and do retain any state after shutdown"sv << std::endl
<< " | Effectively starting as if for the first time without overwriting any pairings with your devices"sv << std::endl
<< " -2 | Force replacement of headers in video stream"sv << std::endl
<< " -p | Enable/Disable UPnP"sv << std::endl
<< std::endl;
}
namespace help {
int
entry(const char *name, int argc, char *argv[]) {
@ -404,19 +366,6 @@ launch_ui_with_path(std::string path) {
platf::open_url(url);
}
/**
* @brief Flush the log.
*
* EXAMPLES:
* ```cpp
* log_flush();
* ```
*/
void
log_flush() {
sink->flush();
}
std::map<int, std::function<void()>> signal_handlers;
void
on_signal_forwarder(int sig) {
@ -488,6 +437,9 @@ SessionMonitorWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
*/
int
main(int argc, char *argv[]) {
// the version should be printed to the log before anything else
BOOST_LOG(info) << PROJECT_NAME << " version: " << PROJECT_VER;
lifetime::argv = argv;
task_pool_util::TaskPool::task_id_t force_shutdown = nullptr;
@ -689,7 +641,6 @@ main(int argc, char *argv[]) {
#endif
BOOST_LOG(info) << PROJECT_NAME << " version: " << PROJECT_VER << std::endl;
task_pool.start(1);
#if defined SUNSHINE_TRAY && SUNSHINE_TRAY >= 1

View file

@ -10,9 +10,6 @@
#include <filesystem>
#include <string_view>
// lib includes
#include <boost/log/common.hpp>
// local includes
#include "thread_pool.h"
#include "thread_safe.h"
@ -26,20 +23,9 @@ extern nvprefs::nvprefs_interface nvprefs_instance;
extern thread_pool_util::ThreadPool task_pool;
extern bool display_cursor;
extern boost::log::sources::severity_logger<int> verbose;
extern boost::log::sources::severity_logger<int> debug;
extern boost::log::sources::severity_logger<int> info;
extern boost::log::sources::severity_logger<int> warning;
extern boost::log::sources::severity_logger<int> error;
extern boost::log::sources::severity_logger<int> fatal;
// functions
int
main(int argc, char *argv[]);
void
log_flush();
void
print_help(const char *name);
std::string
read_file(const char *path);
int

View file

@ -1,6 +1,7 @@
#include "nvenc_base.h"
#include "src/config.h"
#include "src/logging.h"
#include "src/utility.h"
namespace {

View file

@ -1,3 +1,5 @@
#include "src/logging.h"
#ifdef _WIN32
#include "nvenc_d3d11.h"

View file

@ -1,3 +1,5 @@
#include <cassert>
#include "nvenc_utils.h"
namespace nvenc {

View file

@ -23,6 +23,7 @@
#include "config.h"
#include "crypto.h"
#include "httpcommon.h"
#include "logging.h"
#include "main.h"
#include "network.h"
#include "nvhttp.h"

View file

@ -10,7 +10,7 @@
#include <mutex>
#include <string>
#include "src/main.h"
#include "src/logging.h"
#include "src/thread_safe.h"
#include "src/utility.h"
#include "src/video_colorspace.h"

View file

@ -14,6 +14,7 @@
#include "src/platform/common.h"
#include "src/config.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/thread_safe.h"

View file

@ -19,6 +19,7 @@ extern "C" {
#include "cuda.h"
#include "graphics.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/utility.h"
#include "src/video.h"

View file

@ -3,6 +3,8 @@
* @brief todo
*/
#include "graphics.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/video.h"
#include <fcntl.h>

View file

@ -11,7 +11,7 @@
#include <glad/gl.h>
#include "misc.h"
#include "src/main.h"
#include "src/logging.h"
#include "src/platform/common.h"
#include "src/utility.h"
#include "src/video_colorspace.h"

View file

@ -23,6 +23,7 @@
#include "src/config.h"
#include "src/input.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/platform/common.h"
#include "src/utility.h"

View file

@ -14,6 +14,7 @@
#include <filesystem>
#include "src/logging.h"
#include "src/main.h"
#include "src/platform/common.h"
#include "src/round_robin.h"

View file

@ -26,6 +26,7 @@
#include "graphics.h"
#include "misc.h"
#include "src/config.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/platform/common.h"
#include "vaapi.h"

View file

@ -7,6 +7,7 @@
#include <thread>
#include "misc.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/nvhttp.h"
#include "src/platform/common.h"

View file

@ -26,7 +26,7 @@ vaSyncBuffer(
#include "graphics.h"
#include "misc.h"
#include "src/config.h"
#include "src/main.h"
#include "src/logging.h"
#include "src/platform/common.h"
#include "src/utility.h"
#include "src/video.h"

View file

@ -9,7 +9,7 @@
#include <cstdlib>
#include "graphics.h"
#include "src/main.h"
#include "src/logging.h"
#include "src/platform/common.h"
#include "src/round_robin.h"
#include "src/utility.h"

View file

@ -4,6 +4,7 @@
*/
#include "src/platform/common.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/video.h"

View file

@ -17,6 +17,7 @@
#include <xcb/xfixes.h>
#include "src/config.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/task_pool.h"
#include "src/video.h"

View file

@ -8,6 +8,7 @@
#include "src/platform/macos/nv12_zero_device.h"
#include "src/config.h"
#include "src/logging.h"
// Avoid conflict between AVFoundation and libavutil both defining AVMediaType
#define AVMediaType AVMediaType_FFmpeg

View file

@ -6,7 +6,7 @@
#include <chrono>
#include <mach/mach.h>
#include "src/main.h"
#include "src/logging.h"
#include "src/platform/common.h"
#include "src/utility.h"

View file

@ -6,6 +6,7 @@
#include "src/platform/macos/av_audio.h"
#include "src/config.h"
#include "src/logging.h"
namespace platf {
using namespace std::literals;

View file

@ -18,6 +18,7 @@
#include <pwd.h>
#include "misc.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/platform/common.h"

View file

@ -7,6 +7,7 @@
#include <thread>
#include "misc.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/nvhttp.h"
#include "src/platform/common.h"

View file

@ -16,7 +16,7 @@
#include <avrt.h>
#include "src/config.h"
#include "src/main.h"
#include "src/logging.h"
#include "src/platform/common.h"
// Must be the last included file

View file

@ -15,6 +15,7 @@ typedef long NTSTATUS;
#include "display.h"
#include "misc.h"
#include "src/config.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/platform/common.h"
#include "src/stat_trackers.h"

View file

@ -5,7 +5,7 @@
#include "display.h"
#include "misc.h"
#include "src/main.h"
#include "src/logging.h"
namespace platf {
using namespace std::literals;

View file

@ -17,7 +17,7 @@ extern "C" {
#include "display.h"
#include "misc.h"
#include "src/config.h"
#include "src/main.h"
#include "src/logging.h"
#include "src/nvenc/nvenc_config.h"
#include "src/nvenc/nvenc_d3d11.h"
#include "src/nvenc/nvenc_utils.h"

View file

@ -12,6 +12,7 @@
#include "keylayout.h"
#include "misc.h"
#include "src/config.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/platform/common.h"

View file

@ -29,6 +29,7 @@
#include <sddl.h>
// clang-format on
#include "src/logging.h"
#include "src/main.h"
#include "src/platform/common.h"
#include "src/utility.h"

View file

@ -1,6 +1,6 @@
// local includes
#include "nvprefs_common.h"
#include "src/main.h" // sunshine boost::log severity levels
#include "src/logging.h"
// read user override preferences from global sunshine config
#include "src/config.h"

View file

@ -1,7 +1,9 @@
// standard includes
#include <cassert>
// local includes
#include "nvprefs_interface.h"
#include "driver_settings.h"
#include "src/main.h" // main include for assert
#include "nvprefs_interface.h"
#include "undo_file.h"
namespace {

View file

@ -13,6 +13,7 @@
#include "misc.h"
#include "src/config.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/network.h"
#include "src/nvhttp.h"

View file

@ -22,6 +22,7 @@
#include "config.h"
#include "crypto.h"
#include "logging.h"
#include "main.h"
#include "platform/common.h"
#include "system_tray.h"

View file

@ -17,6 +17,7 @@ extern "C" {
#include "config.h"
#include "input.h"
#include "logging.h"
#include "main.h"
#include "network.h"
#include "rtsp.h"

View file

@ -19,6 +19,7 @@ extern "C" {
#include "config.h"
#include "input.h"
#include "logging.h"
#include "main.h"
#include "network.h"
#include "stat_trackers.h"

View file

@ -37,6 +37,7 @@
// local includes
#include "confighttp.h"
#include "logging.h"
#include "main.h"
#include "platform/common.h"
#include "process.h"

View file

@ -7,6 +7,7 @@
#include "config.h"
#include "confighttp.h"
#include "logging.h"
#include "main.h"
#include "network.h"
#include "nvhttp.h"

View file

@ -20,6 +20,7 @@ extern "C" {
#include "cbs.h"
#include "config.h"
#include "input.h"
#include "logging.h"
#include "main.h"
#include "nvenc/nvenc_base.h"
#include "platform/common.h"

View file

@ -1,6 +1,6 @@
#include "video_colorspace.h"
#include "main.h"
#include "logging.h"
#include "video.h"
extern "C" {