Skip to content

Commit

Permalink
refactor(logging): separate logging from main (#2110)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher authored Feb 7, 2024
1 parent 8373a8b commit 0aa4f06
Show file tree
Hide file tree
Showing 50 changed files with 161 additions and 80 deletions.
2 changes: 2 additions & 0 deletions cmake/compile_definitions/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions docs/source/source_code/src/logging.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
logging
=======

.. doxygenfile:: logging.h
:allow-dot-graphs:
1 change: 1 addition & 0 deletions src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "audio.h"
#include "config.h"
#include "logging.h"
#include "main.h"
#include "thread_safe.h"
#include "utility.h"
Expand Down
2 changes: 1 addition & 1 deletion src/cbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern "C" {
}

#include "cbs.h"
#include "main.h"
#include "logging.h"
#include "utility.h"

using namespace std::literals;
Expand Down
1 change: 1 addition & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/confighttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/httpcommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
73 changes: 73 additions & 0 deletions src/logging.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 27 additions & 0 deletions src/logging.h
Original file line number Diff line number Diff line change
@@ -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);
57 changes: 4 additions & 53 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -52,55 +53,16 @@ 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 *) {}
};

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[]) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/nvenc/nvenc_base.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "nvenc_base.h"

#include "src/config.h"
#include "src/logging.h"
#include "src/utility.h"

namespace {
Expand Down
2 changes: 2 additions & 0 deletions src/nvenc/nvenc_d3d11.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "src/logging.h"

#ifdef _WIN32
#include "nvenc_d3d11.h"

Expand Down
2 changes: 2 additions & 0 deletions src/nvenc/nvenc_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <cassert>

#include "nvenc_utils.h"

namespace nvenc {
Expand Down
1 change: 1 addition & 0 deletions src/nvhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/platform/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/platform/linux/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions src/platform/linux/cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/platform/linux/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* @brief todo
*/
#include "graphics.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/video.h"

#include <fcntl.h>
Expand Down
2 changes: 1 addition & 1 deletion src/platform/linux/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/platform/linux/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/platform/linux/kmsgrab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <filesystem>

#include "src/logging.h"
#include "src/main.h"
#include "src/platform/common.h"
#include "src/round_robin.h"
Expand Down
1 change: 1 addition & 0 deletions src/platform/linux/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading

0 comments on commit 0aa4f06

Please sign in to comment.