This commit is contained in:
Vaxry 2025-11-23 01:41:37 +00:00
parent c052d6a865
commit 7afd31c262
Signed by: vaxry
GPG key ID: 665806380871D640
4 changed files with 95 additions and 17 deletions

View file

@ -5,6 +5,7 @@
#include <string_view>
#include "../memory/UniquePtr.hpp"
#include "../memory/WeakPtr.hpp"
namespace Hyprutils::CLI {
class CLoggerImpl;
@ -62,5 +63,41 @@ namespace Hyprutils::CLI {
bool m_shouldLogAtAll = false;
friend class CLoggerImpl;
friend class CLoggerConnection;
};
// CLoggerConnection is a "handle" to a logger, that can be created from a logger and
// allows to send messages to a logger via a proxy
// this does not allow for any changes to the logger itself, only sending logs.
class CLoggerConnection {
public:
CLoggerConnection(CLogger& logger);
~CLoggerConnection();
void setName(const std::string_view& name);
void log(eLogLevel level, const std::string_view& msg);
template <typename... Args>
// NOLINTNEXTLINE
void log(eLogLevel level, std::format_string<Args...> fmt, Args&&... args) {
if (!m_impl || !m_logger)
return;
if (!m_logger->m_shouldLogAtAll)
return;
if (level == LOG_TRACE && !m_logger->m_trace)
return;
std::string logMsg = std::vformat(fmt.get(), std::make_format_args(args...));
log(level, logMsg);
}
private:
Memory::CWeakPointer<CLoggerImpl> m_impl;
CLogger* m_logger = nullptr;
std::string m_name = "";
};
};

View file

@ -66,7 +66,19 @@ void CLogger::log(eLogLevel level, const std::string_view& msg) {
if (level == LOG_TRACE && !m_trace)
return;
std::lock_guard<std::mutex> lg(m_impl->m_logMtx);
m_impl->log(level, msg);
}
const std::string& CLogger::rollingLog() {
return m_impl->m_rollingLog;
}
CLoggerImpl::CLoggerImpl(CLogger* parent) : m_parent(parent) {
updateParentShouldLog();
}
void CLoggerImpl::log(eLogLevel level, const std::string_view& msg, const std::string_view& from) {
std::lock_guard<std::mutex> lg(m_logMtx);
std::string logPrefix = "", logPrefixColor = "";
std::string logMsg = "";
@ -94,7 +106,7 @@ void CLogger::log(eLogLevel level, const std::string_view& msg) {
break;
}
if (m_impl->m_timeEnabled) {
if (m_timeEnabled) {
#ifndef _LIBCPP_VERSION
static auto current_zone = std::chrono::current_zone();
const auto zt = std::chrono::zoned_time{current_zone, std::chrono::system_clock::now()};
@ -106,23 +118,21 @@ void CLogger::log(eLogLevel level, const std::string_view& msg) {
logMsg += std::format("@ {} ", hms);
}
if (!from.empty()) {
logMsg += "from ";
logMsg += from;
logMsg += " ";
}
logMsg += "]: ";
logMsg += msg;
if (m_impl->m_stdoutEnabled)
std::println("{}{}", m_impl->m_colorEnabled ? logPrefixColor : logPrefix, logMsg);
if (m_impl->m_fileEnabled)
m_impl->m_logOfs << logPrefix << logMsg << "\n";
if (m_stdoutEnabled)
std::println("{}{}", m_colorEnabled ? logPrefixColor : logPrefix, logMsg);
if (m_fileEnabled)
m_logOfs << logPrefix << logMsg << "\n";
m_impl->appendToRolling(logPrefix + logMsg);
}
const std::string& CLogger::rollingLog() {
return m_impl->m_rollingLog;
}
CLoggerImpl::CLoggerImpl(CLogger* parent) : m_parent(parent) {
updateParentShouldLog();
appendToRolling(logPrefix + logMsg);
}
void CLoggerImpl::updateParentShouldLog() {
@ -137,3 +147,26 @@ void CLoggerImpl::appendToRolling(const std::string& s) {
if (m_rollingLog.size() > ROLLING_LOG_SIZE)
m_rollingLog = m_rollingLog.substr(m_rollingLog.find('\n', m_rollingLog.size() - ROLLING_LOG_SIZE) + 1);
}
CLoggerConnection::CLoggerConnection(CLogger& logger) : m_impl(logger.m_impl), m_logger(&logger) {
;
}
CLoggerConnection::~CLoggerConnection() = default;
void CLoggerConnection::setName(const std::string_view& name) {
m_name = name;
}
void CLoggerConnection::log(eLogLevel level, const std::string_view& msg) {
if (!m_impl || !m_logger)
return;
if (!m_logger->m_shouldLogAtAll)
return;
if (level == LOG_TRACE && !m_logger->m_trace)
return;
m_impl->log(level, msg, m_name);
}

View file

@ -15,6 +15,7 @@ namespace Hyprutils::CLI {
void updateParentShouldLog();
void appendToRolling(const std::string& s);
void log(eLogLevel level, const std::string_view& msg, const std::string_view& from = "");
std::string m_rollingLog;
std::ofstream m_logOfs;

View file

@ -24,11 +24,18 @@ TEST(CLI, Logger) {
EXPECT_EQ(logger.rollingLog(), "DEBUG ]: Hello!\nTRACE ]: Hello, Trace!");
CLoggerConnection connection(logger);
connection.setName("conn");
connection.log(Hyprutils::CLI::LOG_DEBUG, "Hello from connection!");
EXPECT_EQ(logger.rollingLog(), "DEBUG ]: Hello!\nTRACE ]: Hello, Trace!\nDEBUG from conn ]: Hello from connection!");
logger.setEnableStdout(false);
logger.log(Hyprutils::CLI::LOG_ERR, "Error");
EXPECT_EQ(logger.rollingLog(), "DEBUG ]: Hello!\nTRACE ]: Hello, Trace!");
EXPECT_EQ(logger.rollingLog(), "DEBUG ]: Hello!\nTRACE ]: Hello, Trace!\nDEBUG from conn ]: Hello from connection!");
auto res = logger.setOutputFile("./loggerFile.log");
EXPECT_TRUE(res);
@ -57,7 +64,7 @@ TEST(CLI, Logger) {
logger.log(Hyprutils::CLI::LOG_CRIT, "rip");
// spam some logs to check rolling
for (size_t i = 0; i < 1000; ++i) {
for (size_t i = 0; i < 200; ++i) {
logger.log(LOG_DEBUG, "Log log log!");
}