Skip to content

Commit

Permalink
minor rework in g_log_set_default_handler (#2518)
Browse files Browse the repository at this point in the history
  • Loading branch information
giuspen committed Jun 2, 2024
1 parent 3dc4c1e commit 92c45cc
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions src/ct/ct_main.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* ct_main.cc
*
* Copyright 2009-2021
* Copyright 2009-2024
* Giuseppe Penone <giuspen@gmail.com>
* Evgenii Gurianov <https://github.com/txe>
*
Expand All @@ -28,31 +28,23 @@
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/rotating_file_sink.h>

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<spdlog::logger*>(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);
}
}

Expand Down Expand Up @@ -104,16 +96,16 @@ 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<spdlog::logger>(" ", 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<spdlog::logger>("che", begin(sinks), end(sinks));
auto gtk_logger = std::make_shared<spdlog::logger>("gtk", begin(sinks), end(sinks));

spdlog::set_default_logger(cherrytree_logger); // make our logger as a default logger
spdlog::register_logger(gtk_logger); // register it, so we can access it in another place
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) {
Expand Down

0 comments on commit 92c45cc

Please sign in to comment.