mirror of
https://github.com/hyprwm/hyprutils.git
synced 2025-12-25 01:20:04 +01:00
stuf
This commit is contained in:
parent
7afd31c262
commit
b3657e3135
3 changed files with 48 additions and 15 deletions
|
|
@ -31,7 +31,7 @@ namespace Hyprutils::CLI {
|
|||
CLogger(CLogger&) = delete;
|
||||
CLogger(CLogger&&) = delete;
|
||||
|
||||
void setTrace(bool enabled);
|
||||
void setLogLevel(eLogLevel level);
|
||||
void setTime(bool enabled);
|
||||
void setEnableStdout(bool enabled);
|
||||
void setEnableColor(bool enabled);
|
||||
|
|
@ -46,7 +46,7 @@ namespace Hyprutils::CLI {
|
|||
if (!m_shouldLogAtAll)
|
||||
return;
|
||||
|
||||
if (level == LOG_TRACE && !m_trace)
|
||||
if (level < m_logLevel)
|
||||
return;
|
||||
|
||||
std::string logMsg = std::vformat(fmt.get(), std::make_format_args(args...));
|
||||
|
|
@ -57,7 +57,7 @@ namespace Hyprutils::CLI {
|
|||
Memory::CUniquePointer<CLoggerImpl> m_impl;
|
||||
|
||||
// this has to be here as part of important optimization of trace logs
|
||||
bool m_trace = false;
|
||||
eLogLevel m_logLevel = LOG_DEBUG;
|
||||
|
||||
// this has to be here as part of important optimization of disabled logging
|
||||
bool m_shouldLogAtAll = false;
|
||||
|
|
@ -69,12 +69,20 @@ namespace Hyprutils::CLI {
|
|||
// 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.
|
||||
// Logger connections keep their own logLevel. They inherit it at creation, but can be changed
|
||||
class CLoggerConnection {
|
||||
public:
|
||||
CLoggerConnection(CLogger& logger);
|
||||
~CLoggerConnection();
|
||||
|
||||
CLoggerConnection(const CLoggerConnection&) = delete;
|
||||
CLoggerConnection(CLoggerConnection&) = delete;
|
||||
|
||||
// Allow move
|
||||
CLoggerConnection(CLoggerConnection&&) = default;
|
||||
|
||||
void setName(const std::string_view& name);
|
||||
void setLogLevel(eLogLevel level);
|
||||
|
||||
void log(eLogLevel level, const std::string_view& msg);
|
||||
|
||||
|
|
@ -87,7 +95,7 @@ namespace Hyprutils::CLI {
|
|||
if (!m_logger->m_shouldLogAtAll)
|
||||
return;
|
||||
|
||||
if (level == LOG_TRACE && !m_logger->m_trace)
|
||||
if (level < m_logLevel)
|
||||
return;
|
||||
|
||||
std::string logMsg = std::vformat(fmt.get(), std::make_format_args(args...));
|
||||
|
|
@ -96,7 +104,8 @@ namespace Hyprutils::CLI {
|
|||
|
||||
private:
|
||||
Memory::CWeakPointer<CLoggerImpl> m_impl;
|
||||
CLogger* m_logger = nullptr;
|
||||
CLogger* m_logger = nullptr;
|
||||
eLogLevel m_logLevel = LOG_DEBUG;
|
||||
|
||||
std::string m_name = "";
|
||||
};
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ CLogger::CLogger() {
|
|||
|
||||
CLogger::~CLogger() = default;
|
||||
|
||||
void CLogger::setTrace(bool enabled) {
|
||||
m_trace = enabled;
|
||||
void CLogger::setLogLevel(eLogLevel level) {
|
||||
m_logLevel = level;
|
||||
}
|
||||
|
||||
void CLogger::setTime(bool enabled) {
|
||||
|
|
@ -63,7 +63,7 @@ void CLogger::log(eLogLevel level, const std::string_view& msg) {
|
|||
if (!m_shouldLogAtAll)
|
||||
return;
|
||||
|
||||
if (level == LOG_TRACE && !m_trace)
|
||||
if (level < m_logLevel)
|
||||
return;
|
||||
|
||||
m_impl->log(level, msg);
|
||||
|
|
@ -148,7 +148,7 @@ void CLoggerImpl::appendToRolling(const std::string& s) {
|
|||
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(CLogger& logger) : m_impl(logger.m_impl), m_logger(&logger), m_logLevel(logger.m_logLevel) {
|
||||
;
|
||||
}
|
||||
|
||||
|
|
@ -158,6 +158,10 @@ void CLoggerConnection::setName(const std::string_view& name) {
|
|||
m_name = name;
|
||||
}
|
||||
|
||||
void CLoggerConnection::setLogLevel(eLogLevel level) {
|
||||
m_logLevel = level;
|
||||
}
|
||||
|
||||
void CLoggerConnection::log(eLogLevel level, const std::string_view& msg) {
|
||||
if (!m_impl || !m_logger)
|
||||
return;
|
||||
|
|
@ -165,7 +169,7 @@ void CLoggerConnection::log(eLogLevel level, const std::string_view& msg) {
|
|||
if (!m_logger->m_shouldLogAtAll)
|
||||
return;
|
||||
|
||||
if (level == LOG_TRACE && !m_logger->m_trace)
|
||||
if (level < m_logLevel)
|
||||
return;
|
||||
|
||||
m_impl->log(level, msg, m_name);
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ TEST(CLI, Logger) {
|
|||
|
||||
EXPECT_EQ(logger.rollingLog(), "DEBUG ]: Hello!");
|
||||
|
||||
logger.setTrace(true);
|
||||
logger.setLogLevel(LOG_TRACE);
|
||||
|
||||
logger.log(Hyprutils::CLI::LOG_TRACE, "Hello, {}!", "Trace");
|
||||
|
||||
|
|
@ -27,15 +27,21 @@ TEST(CLI, Logger) {
|
|||
CLoggerConnection connection(logger);
|
||||
connection.setName("conn");
|
||||
|
||||
connection.log(Hyprutils::CLI::LOG_TRACE, "Hello from connection!");
|
||||
|
||||
EXPECT_EQ(logger.rollingLog(), "DEBUG ]: Hello!\nTRACE ]: Hello, Trace!\nTRACE from conn ]: Hello from connection!");
|
||||
|
||||
connection.setLogLevel(Hyprutils::CLI::LOG_WARN);
|
||||
|
||||
connection.log(Hyprutils::CLI::LOG_DEBUG, "Hello from connection!");
|
||||
|
||||
EXPECT_EQ(logger.rollingLog(), "DEBUG ]: Hello!\nTRACE ]: Hello, Trace!\nDEBUG from conn ]: Hello from connection!");
|
||||
EXPECT_EQ(logger.rollingLog(), "DEBUG ]: Hello!\nTRACE ]: Hello, Trace!\nTRACE from conn ]: Hello from connection!");
|
||||
|
||||
logger.setEnableStdout(false);
|
||||
|
||||
logger.log(Hyprutils::CLI::LOG_ERR, "Error");
|
||||
|
||||
EXPECT_EQ(logger.rollingLog(), "DEBUG ]: Hello!\nTRACE ]: Hello, Trace!\nDEBUG from conn ]: Hello from connection!");
|
||||
EXPECT_EQ(logger.rollingLog(), "DEBUG ]: Hello!\nTRACE ]: Hello, Trace!\nTRACE from conn ]: Hello from connection!");
|
||||
|
||||
auto res = logger.setOutputFile("./loggerFile.log");
|
||||
EXPECT_TRUE(res);
|
||||
|
|
@ -65,9 +71,23 @@ TEST(CLI, Logger) {
|
|||
|
||||
// spam some logs to check rolling
|
||||
for (size_t i = 0; i < 200; ++i) {
|
||||
logger.log(LOG_DEBUG, "Log log log!");
|
||||
logger.log(LOG_ERR, "Oh noes!!!");
|
||||
}
|
||||
|
||||
EXPECT_TRUE(logger.rollingLog().size() < 4096);
|
||||
EXPECT_TRUE(logger.rollingLog().starts_with("DEBUG")); // test the breaking is done correctly
|
||||
EXPECT_TRUE(logger.rollingLog().starts_with("ERR")); // test the breaking is done correctly
|
||||
|
||||
// test scoping
|
||||
CLogger* pLogger = new CLogger();
|
||||
CLoggerConnection* pConnection = new CLoggerConnection(*pLogger);
|
||||
|
||||
pLogger->setEnableStdout(false);
|
||||
|
||||
pConnection->log(LOG_DEBUG, "This shouldn't log anything.");
|
||||
|
||||
EXPECT_TRUE(pLogger->rollingLog().empty());
|
||||
|
||||
delete pLogger;
|
||||
|
||||
pConnection->log(LOG_DEBUG, "This shouldn't do anything, or crash.");
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue