From 46f48f2f875bbef1178e76c15e4ddcb47c22ae8c Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Fri, 2 Aug 2024 18:48:09 -0700 Subject: [PATCH] core/log: add fancy logger --- src/core/CMakeLists.txt | 1 + src/core/logging.cpp | 74 +++++++++++++++++++++++++++++++++++++++++ src/core/logging.hpp | 6 ++++ src/core/main.cpp | 8 ++--- 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 src/core/logging.cpp create mode 100644 src/core/logging.hpp diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index c53976ba..eedfca99 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -37,6 +37,7 @@ qt_add_library(quickshell-core STATIC types.cpp qsmenuanchor.cpp clock.cpp + logging.cpp ) set_source_files_properties(main.cpp PROPERTIES COMPILE_DEFINITIONS GIT_REVISION="${GIT_REVISION}") diff --git a/src/core/logging.cpp b/src/core/logging.cpp new file mode 100644 index 00000000..d216a980 --- /dev/null +++ b/src/core/logging.cpp @@ -0,0 +1,74 @@ +#include "logging.hpp" +#include +#include + +#include +#include +#include + +namespace { + +bool COLOR_LOGS = false; // NOLINT + +void formatMessage( + QtMsgType type, + const QMessageLogContext& context, + const QString& msg, + bool color +) { + const auto* typeString = "[log error]"; + + if (color) { + switch (type) { + case QtDebugMsg: typeString = "\033[34m DEBUG"; break; + case QtInfoMsg: typeString = "\033[32m INFO"; break; + case QtWarningMsg: typeString = "\033[33m WARN"; break; + case QtCriticalMsg: typeString = "\033[31m ERROR"; break; + case QtFatalMsg: typeString = "\033[31m FATAL"; break; + } + } else { + switch (type) { + case QtDebugMsg: typeString = " DEBUG"; break; + case QtInfoMsg: typeString = " INFO"; break; + case QtWarningMsg: typeString = " WARN"; break; + case QtCriticalMsg: typeString = " ERROR"; break; + case QtFatalMsg: typeString = " FATAL"; break; + } + } + + const auto isDefault = strcmp(context.category, "default") == 0; + + const char* format = nullptr; + + if (color) { + if (type == QtFatalMsg) { + if (isDefault) format = "%s: %s\033[0m\n"; + else format = "%s %s: %s\033[0m\n"; + } else { + if (isDefault) format = "%s\033[0m: %s\n"; + else format = "%s \033[97m%s\033[0m: %s\n"; + } + } else { + if (isDefault) format = "%s: %s\n"; + else format = "%s %s: %s\n"; + } + + if (isDefault) { + printf(format, typeString, msg.toStdString().c_str()); + } else { + printf(format, typeString, context.category, msg.toStdString().c_str()); + } + + fflush(stdout); +} + +void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg) { + formatMessage(type, context, msg, COLOR_LOGS); +} + +} // namespace + +void LogManager::setup() { + COLOR_LOGS = qEnvironmentVariableIsEmpty("NO_COLOR"); + qInstallMessageHandler(&messageHandler); +} diff --git a/src/core/logging.hpp b/src/core/logging.hpp new file mode 100644 index 00000000..c2d8d13f --- /dev/null +++ b/src/core/logging.hpp @@ -0,0 +1,6 @@ +#pragma once + +class LogManager { +public: + static void setup(); +}; diff --git a/src/core/main.cpp b/src/core/main.cpp index f3fef81d..12f3eb38 100644 --- a/src/core/main.cpp +++ b/src/core/main.cpp @@ -22,10 +22,12 @@ #include #include +#include "logging.hpp" #include "plugin.hpp" #include "rootwrapper.hpp" int qs_main(int argc, char** argv) { + LogManager::setup(); QString configFilePath; QString workingDirectory; @@ -330,11 +332,9 @@ int qs_main(int argc, char** argv) { file.close(); } + qInfo() << "shell id:" << shellId; - if (printCurrent) { - qInfo() << "shell id:" << shellId; - return 0; - } + if (printCurrent) return 0; for (auto [var, val]: envOverrides.asKeyValueRange()) { qputenv(var.toUtf8(), val.toUtf8());