mirror of
https://github.com/hyprwm/aquamarine.git
synced 2025-12-20 02:30:02 +01:00
backend: implement hyprutils' cli::logger
This commit is contained in:
parent
6d0b356758
commit
23e4e25dd3
5 changed files with 87 additions and 8 deletions
6
flake.lock
generated
6
flake.lock
generated
|
|
@ -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": {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#include <hyprutils/memory/SharedPtr.hpp>
|
||||
#include <hyprutils/memory/WeakPtr.hpp>
|
||||
#include <hyprutils/signal/Signal.hpp>
|
||||
#include <hyprutils/cli/Logger.hpp>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
|
|
@ -60,6 +61,7 @@ namespace Aquamarine {
|
|||
struct SBackendOptions {
|
||||
explicit SBackendOptions();
|
||||
std::function<void(eBackendLogLevel, std::string)> logFunction;
|
||||
Hyprutils::Memory::CSharedPointer<Hyprutils::CLI::CLoggerConnection> logConnection;
|
||||
};
|
||||
|
||||
struct SPollFD {
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@
|
|||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "Logger.hpp"
|
||||
|
||||
using namespace Hyprutils::Memory;
|
||||
using namespace Aquamarine;
|
||||
#define SP CSharedPointer
|
||||
|
|
@ -60,6 +62,10 @@ Hyprutils::Memory::CSharedPointer<CBackend> 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<Hyprutils::Memory::CSharedPointer<SPollFD>> Aquamarine::CBackend::getPollFDs() {
|
||||
|
|
|
|||
35
src/backend/Logger.cpp
Normal file
35
src/backend/Logger.cpp
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
39
src/backend/Logger.hpp
Normal file
39
src/backend/Logger.hpp
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#pragma once
|
||||
|
||||
#include <aquamarine/backend/Backend.hpp>
|
||||
|
||||
#include <hyprutils/cli/Logger.hpp>
|
||||
|
||||
namespace Aquamarine {
|
||||
class CLogger {
|
||||
public:
|
||||
CLogger();
|
||||
~CLogger() = default;
|
||||
|
||||
void log(eBackendLogLevel level, const std::string& str);
|
||||
void updateLevels();
|
||||
|
||||
template <typename... Args>
|
||||
//NOLINTNEXTLINE
|
||||
void log(eBackendLogLevel level, std::format_string<Args...> fmt, Args&&... args) {
|
||||
if (!m_loggerConnection && !m_logFn)
|
||||
return;
|
||||
|
||||
std::string logMsg = "";
|
||||
|
||||
// no need for try {} catch {} because std::format_string<Args...> 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<void(eBackendLogLevel, std::string)> m_logFn;
|
||||
Hyprutils::Memory::CSharedPointer<Hyprutils::CLI::CLoggerConnection> m_loggerConnection;
|
||||
};
|
||||
|
||||
inline Hyprutils::Memory::CSharedPointer<CLogger> g_logger = Hyprutils::Memory::makeShared<CLogger>();
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue