From 23e4e25dd301de94f0119279ccbbcd72c96de131 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sun, 23 Nov 2025 16:34:04 +0000 Subject: [PATCH] backend: implement hyprutils' cli::logger --- flake.lock | 6 ++-- include/aquamarine/backend/Backend.hpp | 4 ++- src/backend/Backend.cpp | 11 +++++--- src/backend/Logger.cpp | 35 +++++++++++++++++++++++ src/backend/Logger.hpp | 39 ++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 src/backend/Logger.cpp create mode 100644 src/backend/Logger.hpp diff --git a/flake.lock b/flake.lock index 07e9687..789c71e 100644 --- a/flake.lock +++ b/flake.lock @@ -10,11 +10,11 @@ ] }, "locked": { - "lastModified": 1755795954, - "narHash": "sha256-9QoDVkjLwjiZDR+y4cMWc/FVudRu5jCIG4rn15Afa9w=", + "lastModified": 1763913838, + "narHash": "sha256-Q4ZfdlEGQU2LJ/Dif0qzF88xgIkpD6/uRsGfxOpu9jc=", "owner": "hyprwm", "repo": "hyprutils", - "rev": "b364dcb7391709acb4492e100fe750ca722992e1", + "rev": "a9fe9748ae27ec40e03c8c064cf97f483097bd22", "type": "github" }, "original": { diff --git a/include/aquamarine/backend/Backend.hpp b/include/aquamarine/backend/Backend.hpp index 8a34899..3160c9f 100644 --- a/include/aquamarine/backend/Backend.hpp +++ b/include/aquamarine/backend/Backend.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -59,7 +60,8 @@ namespace Aquamarine { struct SBackendOptions { explicit SBackendOptions(); - std::function logFunction; + std::function logFunction; + Hyprutils::Memory::CSharedPointer logConnection; }; struct SPollFD { diff --git a/src/backend/Backend.cpp b/src/backend/Backend.cpp index f0d782f..ee17b7a 100644 --- a/src/backend/Backend.cpp +++ b/src/backend/Backend.cpp @@ -12,6 +12,8 @@ #include #include +#include "Logger.hpp" + using namespace Hyprutils::Memory; using namespace Aquamarine; #define SP CSharedPointer @@ -60,6 +62,10 @@ Hyprutils::Memory::CSharedPointer Aquamarine::CBackend::create(const s backend->implementationOptions = backends; backend->self = backend; + g_logger->m_loggerConnection = options.logConnection; + g_logger->m_logFn = options.logFunction; + g_logger->updateLevels(); + if (backends.size() <= 0) return nullptr; @@ -177,10 +183,7 @@ bool Aquamarine::CBackend::start() { } void Aquamarine::CBackend::log(eBackendLogLevel level, const std::string& msg) { - if (!options.logFunction) - return; - - options.logFunction(level, msg); + g_logger->log(level, msg); } std::vector> Aquamarine::CBackend::getPollFDs() { diff --git a/src/backend/Logger.cpp b/src/backend/Logger.cpp new file mode 100644 index 0000000..f52fe95 --- /dev/null +++ b/src/backend/Logger.cpp @@ -0,0 +1,35 @@ +#include "Logger.hpp" +#include "../include/Shared.hpp" + +using namespace Aquamarine; + +static Hyprutils::CLI::eLogLevel levelToHU(eBackendLogLevel l) { + switch (l) { + case Aquamarine::AQ_LOG_DEBUG: return Hyprutils::CLI::LOG_DEBUG; + case Aquamarine::AQ_LOG_ERROR: return Hyprutils::CLI::LOG_ERR; + case Aquamarine::AQ_LOG_WARNING: return Hyprutils::CLI::LOG_WARN; + case Aquamarine::AQ_LOG_CRITICAL: return Hyprutils::CLI::LOG_CRIT; + case Aquamarine::AQ_LOG_TRACE: return Hyprutils::CLI::LOG_TRACE; + } + return Hyprutils::CLI::LOG_DEBUG; +} + +CLogger::CLogger() = default; + +void CLogger::updateLevels() { + const auto IS_TRACE = Aquamarine::isTrace(); + if (m_loggerConnection && IS_TRACE) + m_loggerConnection->setLogLevel(Hyprutils::CLI::LOG_TRACE); +} + +void CLogger::log(eBackendLogLevel level, const std::string& str) { + if (m_logFn) { + m_logFn(level, str); + return; + } + + if (m_loggerConnection) { + m_loggerConnection->log(levelToHU(level), str); + return; + } +} diff --git a/src/backend/Logger.hpp b/src/backend/Logger.hpp new file mode 100644 index 0000000..041688f --- /dev/null +++ b/src/backend/Logger.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include + +#include + +namespace Aquamarine { + class CLogger { + public: + CLogger(); + ~CLogger() = default; + + void log(eBackendLogLevel level, const std::string& str); + void updateLevels(); + + template + //NOLINTNEXTLINE + void log(eBackendLogLevel level, std::format_string fmt, Args&&... args) { + if (!m_loggerConnection && !m_logFn) + return; + + std::string logMsg = ""; + + // no need for try {} catch {} because std::format_string ensures that vformat never throw std::format_error + // because + // 1. any faulty format specifier that sucks will cause a compilation error. + // 2. and `std::bad_alloc` is catastrophic, (Almost any operation in stdlib could throw this.) + // 3. this is actually what std::format in stdlib does + logMsg += std::vformat(fmt.get(), std::make_format_args(args...)); + + log(level, logMsg); + } + + std::function m_logFn; + Hyprutils::Memory::CSharedPointer m_loggerConnection; + }; + + inline Hyprutils::Memory::CSharedPointer g_logger = Hyprutils::Memory::makeShared(); +}; \ No newline at end of file