From 30bcb28575852e5a41e90e10221131035ddd637f Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Wed, 19 Feb 2025 13:51:14 -0600 Subject: [PATCH] use spdlog --- monitarr/monitarr/main.cpp | 84 +++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/monitarr/monitarr/main.cpp b/monitarr/monitarr/main.cpp index fe7fa66..1d145d5 100644 --- a/monitarr/monitarr/main.cpp +++ b/monitarr/monitarr/main.cpp @@ -13,10 +13,71 @@ #include "settings.hpp" #include "show_cmd.hpp" #include "usage_cmd.hpp" +#include "utils/common.hpp" #include "utils/file.hpp" +#include "utils/path.hpp" +#include "utils/unix.hpp" +#include "utils/windows.hpp" using namespace monitarr; +static constexpr const std::uint8_t MAX_LOG_FILES{5U}; +static constexpr const std::uint64_t MAX_LOG_FILE_SIZE{ + 1024ULL * 1024ULL * 5ULL, +}; + +struct monitarr_exception_handler final + : public utils::error::i_exception_handler { + void handle_debug(std::string_view function_name, + std::string_view msg) const override { + spdlog::get("file")->debug( + utils::error::create_error_message(function_name, {msg})); + } + + void handle_error(std::string_view function_name, + std::string_view msg) const override { + spdlog::get("file")->error( + utils::error::create_error_message(function_name, {msg})); + } + + void handle_exception(std::string_view function_name) const override { + spdlog::get("file")->error(utils::error::create_error_message( + function_name, { + "exception", + "unknown exception", + })); + } + + void handle_exception(std::string_view function_name, + const std::exception &ex) const override { + spdlog::get("file")->error(utils::error::create_error_message( + function_name, { + "exception", + (ex.what() == nullptr ? "unknown" : ex.what()), + })); + } + + void handle_info(std::string_view function_name, + std::string_view msg) const override { + spdlog::get("file")->info( + utils::error::create_error_message(function_name, {msg})); + } + + void handle_trace(std::string_view function_name, + std::string_view msg) const override { + spdlog::get("file")->trace( + utils::error::create_error_message(function_name, {msg})); + } + + void handle_warn(std::string_view function_name, + std::string_view msg) const override { + spdlog::get("file")->warn( + utils::error::create_error_message(function_name, {msg})); + } +}; + +static const monitarr_exception_handler handler{}; + auto main(int argc, char **argv) -> int { MONITARR_USES_FUNCTION_NAME(); @@ -27,9 +88,28 @@ auto main(int argc, char **argv) -> int { if (not utils::file::change_to_process_directory()) { utils::error::handle_error(function_name, "failed to change to process directory"); - return 1; + return -1; } + auto log_dir = utils::get_environment_variable("MONITARR_LOG_DIR"); + if (log_dir.empty()) { + log_dir = utils::path::combine(".", {"logs"}); + } + + if (not utils::file::directory{log_dir}.create_directory()) { + utils::error::handle_error( + function_name, fmt::format("failed to create log dir|{}|{}", log_dir, + utils::get_last_error_code())); + return -1; + } + + spdlog::drop("file"); + spdlog::create_async( + "file", utils::path::combine(log_dir, {"monitarr.log"}), + MAX_LOG_FILE_SIZE, MAX_LOG_FILES); + + utils::error::set_exception_handler(&handler); + if (not monitarr::project_initialize()) { return -1; } @@ -71,5 +151,7 @@ auto main(int argc, char **argv) -> int { } monitarr::project_cleanup(); + + utils::error::set_exception_handler(nullptr); return ret; }