From 92c45cce7de9206c770129938677737c8cbc6516 Mon Sep 17 00:00:00 2001 From: Giuseppe Penone Date: Sun, 2 Jun 2024 11:27:18 +0100 Subject: [PATCH] minor rework in g_log_set_default_handler (#2518) --- src/ct/ct_main.cc | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/ct/ct_main.cc b/src/ct/ct_main.cc index 018fed361..aa3bf38e9 100644 --- a/src/ct/ct_main.cc +++ b/src/ct/ct_main.cc @@ -1,7 +1,7 @@ /* * ct_main.cc * - * Copyright 2009-2021 + * Copyright 2009-2024 * Giuseppe Penone * Evgenii Gurianov * @@ -28,31 +28,23 @@ #include #include -void glib_log_handler(const gchar*, GLogLevelFlags log_level, const gchar* msg, gpointer) +void glib_log_handler(const gchar*/*log_domain*/, GLogLevelFlags log_level, const gchar* message, gpointer user_data) { - auto gtk_logger = spdlog::get("gtk"); + if (not message or not user_data) { + return; + } + auto pGtkLogger = static_cast(user_data); switch (log_level) { - case G_LOG_LEVEL_ERROR: - gtk_logger->error(msg); - break; - case G_LOG_LEVEL_CRITICAL: - gtk_logger->critical(msg); - break; - case G_LOG_LEVEL_WARNING: - gtk_logger->warn(msg); - break; - case G_LOG_LEVEL_MESSAGE: - gtk_logger->info(msg); - break; - case G_LOG_LEVEL_INFO: - gtk_logger->info(msg); - break; + case G_LOG_LEVEL_ERROR: pGtkLogger->error(message); break; + case G_LOG_LEVEL_CRITICAL: pGtkLogger->critical(message); break; + case G_LOG_LEVEL_WARNING: pGtkLogger->warn(message); break; + case G_LOG_LEVEL_MESSAGE: pGtkLogger->info(message); break; + case G_LOG_LEVEL_INFO: pGtkLogger->info(message); break; case G_LOG_LEVEL_DEBUG: // disable due to excessive output - //gtk_logger->debug(msg); + //pGtkLogger->debug(msg); break; - default: - gtk_logger->info(msg); + default: pGtkLogger->info(message); } } @@ -104,8 +96,8 @@ int main(int argc, char *argv[]) } spdlog::drop(""); // remove the default logger (if you want, you can use its name) - // these two loggers are the same, they just add "[ ]" and "[gtk]" in their output - auto cherrytree_logger = std::make_shared(" ", begin(sinks), end(sinks)); + // these two loggers are the same, they just add "[che]" and "[gtk]" in their output + auto cherrytree_logger = std::make_shared("che", begin(sinks), end(sinks)); auto gtk_logger = std::make_shared("gtk", begin(sinks), end(sinks)); spdlog::set_default_logger(cherrytree_logger); // make our logger as a default logger @@ -113,7 +105,7 @@ int main(int argc, char *argv[]) spdlog::flush_on(spdlog::level::debug); // flush when "info" or higher message is logged on all loggers spdlog::set_level(spdlog::level::debug); // Setup spdlog, use debug level by default for now - g_log_set_default_handler(glib_log_handler, nullptr); // Redirect Gtk log messages to spdlog + g_log_set_default_handler(glib_log_handler, gtk_logger.get()); // Redirect Gtk log messages to spdlog bool is_secondary_session{false}; for (int i = 1; i < argc; ++i) {