fix spdlog

This commit is contained in:
Scott E. Graves 2025-02-19 14:26:36 -06:00
parent 30bcb28575
commit 7ebba5bafb
2 changed files with 75 additions and 20 deletions

View File

@ -8,8 +8,8 @@
#if defined(PROJECT_REQUIRE_ALPINE) && !defined(PROJECT_IS_MINGW)
#include <filesystem>
#include <stdlib.h>
#include <pthread.h>
#include <stdlib.h>
#endif // defined(PROJECT_REQUIRE_ALPINE) && !defined(PROJECT_IS_MINGW)
#if defined(PROJECT_ENABLE_LIBSODIUM)
@ -20,6 +20,12 @@
#include "sqlite3.h"
#endif // defined(PROJECT_ENABLE_SQLITE)
#if defined(PROJECT_ENABLE_SPDLOG)
#include <chrono>
#include "spdlog/spdlog.h"
#endif // defined(PROJECT_ENABLE_SPDLOG)
#include "initialize.hpp"
#if defined(PROJECT_REQUIRE_ALPINE) && !defined(PROJECT_IS_MINGW)
@ -42,6 +48,12 @@ auto project_initialize() -> bool {
}
#endif // defined(PROJECT_REQUIRE_ALPINE) && !defined(PROJECT_IS_MINGW)
#if defined(PROJECT_ENABLE_SPDLOG)
spdlog::drop_all();
spdlog::flush_every(std::chrono::seconds(10));
spdlog::set_pattern("%Y-%m-%d|%T.%e|%^%l%$|%v");
#endif // defined(PROJECT_ENABLE_SPDLOG)
#if defined(PROJECT_ENABLE_LIBSODIUM)
{
if (sodium_init() == -1) {
@ -51,7 +63,9 @@ auto project_initialize() -> bool {
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
#if defined(PROJECT_ENABLE_OPENSSL)
{ SSL_library_init(); }
{
SSL_library_init();
}
#endif // defined(PROJECT_ENABLE_OPENSSL)
#if defined(PROJECT_ENABLE_CURL)
@ -86,5 +100,9 @@ void project_cleanup() {
#if defined(PROJECT_ENABLE_SQLITE)
sqlite3_shutdown();
#endif // defined(PROJECT_ENABLE_SQLITE)
#if defined(PROJECT_ENABLE_SPDLOG)
spdlog::shutdown();
#endif // defined(PROJECT_ENABLE_SPDLOG)
}
} // namespace monitarr

View File

@ -30,18 +30,34 @@ 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(
auto file = spdlog::get("file");
if (file) {
file->debug(utils::error::create_error_message(function_name, {msg}));
}
spdlog::get("console")->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(
auto file = spdlog::get("file");
if (file) {
file->error(utils::error::create_error_message(function_name, {msg}));
}
spdlog::get("console")->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(
auto file = spdlog::get("file");
if (file) {
file->error(utils::error::create_error_message(function_name,
{
"exception",
"unknown exception",
}));
}
spdlog::get("console")->error(utils::error::create_error_message(
function_name, {
"exception",
"unknown exception",
@ -50,7 +66,15 @@ struct monitarr_exception_handler final
void handle_exception(std::string_view function_name,
const std::exception &ex) const override {
spdlog::get("file")->error(utils::error::create_error_message(
auto file = spdlog::get("file");
if (file) {
file->error(utils::error::create_error_message(
function_name, {
"exception",
(ex.what() == nullptr ? "unknown" : ex.what()),
}));
}
spdlog::get("console")->error(utils::error::create_error_message(
function_name, {
"exception",
(ex.what() == nullptr ? "unknown" : ex.what()),
@ -59,19 +83,31 @@ struct monitarr_exception_handler final
void handle_info(std::string_view function_name,
std::string_view msg) const override {
spdlog::get("file")->info(
auto file = spdlog::get("file");
if (file) {
file->info(utils::error::create_error_message(function_name, {msg}));
}
spdlog::get("console")->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(
auto file = spdlog::get("file");
if (file) {
file->trace(utils::error::create_error_message(function_name, {msg}));
}
spdlog::get("console")->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(
auto file = spdlog::get("file");
if (file) {
file->warn(utils::error::create_error_message(function_name, {msg}));
}
spdlog::get("console")->warn(
utils::error::create_error_message(function_name, {msg}));
}
};
@ -88,9 +124,17 @@ 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;
}
if (not monitarr::project_initialize()) {
return -1;
}
spdlog::drop("console");
auto console = spdlog::stdout_color_mt("console");
utils::error::set_exception_handler(&handler);
auto log_dir = utils::get_environment_variable("MONITARR_LOG_DIR");
if (log_dir.empty()) {
log_dir = utils::path::combine(".", {"logs"});
@ -103,17 +147,6 @@ auto main(int argc, char **argv) -> int {
return -1;
}
spdlog::drop("file");
spdlog::create_async<spdlog::sinks::rotating_file_sink_mt>(
"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;
}
auto ret{0};
try {
@ -132,6 +165,10 @@ auto main(int argc, char **argv) -> int {
} else if (has_arg("-l", argc, argv)) {
ret = list_cmd(argc, argv, cfg);
} else if (has_arg("-r", argc, argv)) {
spdlog::drop("file");
spdlog::create_async<spdlog::sinks::rotating_file_sink_mt>(
"file", utils::path::combine(log_dir, {"monitarr.log"}),
MAX_LOG_FILE_SIZE, MAX_LOG_FILES);
ret = run_cmd(cfg);
} else if (has_arg("-s", argc, argv)) {
ret = show_cmd(argc, argv, cfg);