refactor: Elevel => LogLevel enum class, use proper names for items in the class

This commit is contained in:
sithlord48
2025-07-07 17:44:21 -04:00
committed by Chris Rizzitello
parent 3472ff6ce5
commit 77bdde5434
27 changed files with 100 additions and 96 deletions

View File

@ -7,7 +7,7 @@
#pragma once
#include "base/ELevel.h"
#include "base/LogLevel.h"
#include "common/IInterface.h"
//! Interface for architecture dependent logging
@ -47,7 +47,7 @@ public:
/*!
Writes the given string to the log with the given level.
*/
virtual void writeLog(ELevel, const char *) = 0;
virtual void writeLog(LogLevel, const char *) = 0;
//@}
};

View File

@ -28,24 +28,25 @@ void ArchLogUnix::showLog(bool)
// do nothing
}
void ArchLogUnix::writeLog(ELevel level, const char *msg)
void ArchLogUnix::writeLog(LogLevel level, const char *msg)
{
// convert level
int priority;
switch (level) {
case kERROR:
using enum LogLevel;
case Error:
priority = LOG_ERR;
break;
case kWARNING:
case Warning:
priority = LOG_WARNING;
break;
case kNOTE:
case Note:
priority = LOG_NOTICE;
break;
case kINFO:
case Info:
priority = LOG_INFO;
break;

View File

@ -22,5 +22,5 @@ public:
void openLog(const char *name) override;
void closeLog() override;
void showLog(bool) override;
void writeLog(ELevel, const char *) override;
void writeLog(LogLevel, const char *) override;
};

View File

@ -38,17 +38,17 @@ void ArchLogWindows::showLog(bool)
// do nothing
}
void ArchLogWindows::writeLog(ELevel level, const char *msg)
void ArchLogWindows::writeLog(LogLevel level, const char *msg)
{
if (m_eventLog != nullptr) {
// convert priority
WORD type;
switch (level) {
case kERROR:
case LogLevel::Error:
type = EVENTLOG_ERROR_TYPE;
break;
case kWARNING:
case LogLevel::Warning:
type = EVENTLOG_WARNING_TYPE;
break;

View File

@ -25,7 +25,7 @@ public:
void openLog(const char *name) override;
void closeLog() override;
void showLog(bool showIfEmpty) override;
void writeLog(ELevel, const char *) override;
void writeLog(LogLevel, const char *) override;
private:
HANDLE m_eventLog;

View File

@ -4,7 +4,6 @@
# SPDX-License-Identifier: MIT
add_library(base STATIC
ELevel.h
Event.cpp
Event.h
EventQueue.cpp
@ -24,6 +23,7 @@ add_library(base STATIC
LogOutputters.h
Log.cpp
Log.h
LogLevel.h
Path.cpp
Path.h
PriorityQueue.h

View File

@ -1,28 +0,0 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#pragma once
//! Log levels
/*!
The logging priority levels in order of highest to lowest priority.
*/
enum ELevel
{
kPRINT = -1, //!< For print only (no file or time)
kFATAL, //!< For fatal errors
kERROR, //!< For serious errors
kWARNING, //!< For minor errors and warnings
kNOTE, //!< For messages about notable events
kINFO, //!< For informational messages
kDEBUG, //!< For important debugging messages
kDEBUG1, //!< For verbosity +1 debugging messages
kDEBUG2, //!< For verbosity +2 debugging messages
kDEBUG3, //!< For verbosity +3 debugging messages
kDEBUG4, //!< For verbosity +4 debugging messages
kDEBUG5 //!< For verbosity +5 debugging messages
};

View File

@ -7,8 +7,8 @@
#pragma once
#include "base/ELevel.h"
#include "base/Log.h"
#include "base/LogLevel.h"
#include "common/IInterface.h"
//! Outputter interface
@ -53,7 +53,7 @@ public:
message to all outputters in the outputter chain, otherwise
it continues. Most implementations should return true.
*/
virtual bool write(ELevel level, const char *message) = 0;
virtual bool write(LogLevel level, const char *message) = 0;
//@}
};

View File

@ -7,6 +7,7 @@
#include "base/Log.h"
#include "arch/Arch.h"
#include "base/LogLevel.h"
#include "base/LogOutputters.h"
#include "common/Constants.h"
@ -33,14 +34,14 @@ static const int g_numPriority = 11;
// for visual studio, then NDEBUG will be set (even if your VS solution
// config is Debug).
#ifndef NDEBUG
static const int g_defaultMaxPriority = kDEBUG;
static const LogLevel g_defaultMaxPriority = LogLevel::Debug;
#else
static const int g_defaultMaxPriority = kINFO;
static const LogLevel g_defaultMaxPriority = LogLevel::Info;
#endif
namespace {
ELevel getPriority(const char *&fmt)
LogLevel getPriority(const char *&fmt)
{
if (strnlen(fmt, SIZE_MAX) < kPriorityPrefixLength) {
throw std::invalid_argument("invalid format string, too short");
@ -50,7 +51,7 @@ ELevel getPriority(const char *&fmt)
throw std::invalid_argument("invalid format string, missing priority");
}
return static_cast<ELevel>(fmt[2] - '0');
return static_cast<LogLevel>(fmt[2] - '0');
}
void makeTimeString(std::vector<char> &buffer)
@ -74,7 +75,7 @@ void makeTimeString(std::vector<char> &buffer)
);
}
std::vector<char> makeMessage(const char *filename, int lineNumber, const char *message, ELevel priority)
std::vector<char> makeMessage(const char *filename, int lineNumber, const char *message, LogLevel priority)
{
// base size includes null terminator, colon, space, etc.
@ -82,12 +83,13 @@ std::vector<char> makeMessage(const char *filename, int lineNumber, const char *
const int timeBufferSize = 50;
const int priorityMaxSize = 10;
const auto currentPriority = static_cast<int>(priority);
std::vector<char> timeBuffer(timeBufferSize);
makeTimeString(timeBuffer);
size_t timestampLength = strnlen(timeBuffer.data(), timeBufferSize);
size_t priorityLength = strnlen(g_priority[priority], priorityMaxSize);
size_t priorityLength = strnlen(g_priority[currentPriority], priorityMaxSize);
size_t messageLength = strnlen(message, SIZE_MAX);
size_t bufferSize = baseSize + timestampLength + priorityLength + messageLength;
@ -99,13 +101,13 @@ std::vector<char> makeMessage(const char *filename, int lineNumber, const char *
std::vector<char> buffer(bufferSize);
snprintf(
buffer.data(), bufferSize, "[%s] %s: %s\n\t%s:%d", timeBuffer.data(), g_priority[priority], message, filename,
lineNumber
buffer.data(), bufferSize, "[%s] %s: %s\n\t%s:%d", timeBuffer.data(), g_priority[currentPriority], message,
filename, lineNumber
);
return buffer;
} else {
std::vector<char> buffer(bufferSize);
snprintf(buffer.data(), bufferSize, "[%s] %s: %s", timeBuffer.data(), g_priority[priority], message);
snprintf(buffer.data(), bufferSize, "[%s] %s: %s", timeBuffer.data(), g_priority[currentPriority], message);
return buffer;
}
}
@ -159,12 +161,13 @@ const char *Log::getFilterName() const
return getFilterName(getFilter());
}
const char *Log::getFilterName(int level) const
const char *Log::getFilterName(LogLevel level) const
{
if (level < 0) {
const auto levelIndex = static_cast<int>(level);
if (levelIndex < 0) {
return "Message";
}
return g_priority[level];
return g_priority[levelIndex];
}
void Log::print(const char *file, int line, const char *fmt, ...)
@ -172,7 +175,7 @@ void Log::print(const char *file, int line, const char *fmt, ...)
const int initBufferSize = 1024;
const int bufferResizeScale = 2;
ELevel priority = getPriority(fmt);
LogLevel priority = getPriority(fmt);
fmt += kPriorityPrefixLength;
if (priority > getFilter()) {
@ -196,7 +199,7 @@ void Log::print(const char *file, int line, const char *fmt, ...)
}
}
if (priority == kPRINT) {
if (priority == LogLevel::Print) {
output(priority, buffer.data());
} else {
auto message = makeMessage(file, line, buffer.data(), priority);
@ -240,7 +243,7 @@ bool Log::setFilter(const char *maxPriority)
if (maxPriority != nullptr) {
for (int i = 0; i < g_numPriority; ++i) {
if (strcmp(maxPriority, g_priority[i]) == 0) {
setFilter(i);
setFilter(static_cast<LogLevel>(i));
return true;
}
}
@ -249,21 +252,21 @@ bool Log::setFilter(const char *maxPriority)
return true;
}
void Log::setFilter(int maxPriority)
void Log::setFilter(LogLevel maxPriority)
{
std::scoped_lock lock{m_mutex};
m_maxPriority = maxPriority;
}
int Log::getFilter() const
LogLevel Log::getFilter() const
{
std::scoped_lock lock{m_mutex};
return m_maxPriority;
}
void Log::output(ELevel priority, const char *msg)
void Log::output(LogLevel priority, const char *msg)
{
assert(priority >= -1 && priority < g_numPriority);
assert(static_cast<int>(priority) >= -1 && static_cast<int>(priority) < g_numPriority);
assert(msg != nullptr);
if (!msg)
return;

View File

@ -87,7 +87,7 @@ public:
bool setFilter(const char *name);
//! Set the minimum priority filter (by ordinal).
void setFilter(int);
void setFilter(LogLevel);
//@}
//! @name accessors
@ -102,28 +102,28 @@ public:
void print(const char *file, int line, const char *format, ...);
//! Get the minimum priority level.
int getFilter() const;
LogLevel getFilter() const;
//! Get the filter name of the current filter level.
const char *getFilterName() const;
//! Get the filter name of a specified filter level.
const char *getFilterName(int level) const;
const char *getFilterName(LogLevel level) const;
//! Get the singleton instance of the log
static Log *getInstance();
//! Get the console filter level (messages above this are not sent to
//! console).
int getConsoleMaxLevel() const
LogLevel getConsoleMaxLevel() const
{
return kDEBUG2;
return LogLevel::Debug2;
}
//@}
private:
void output(ELevel priority, const char *msg);
void output(LogLevel priority, const char *msg);
private:
using OutputterList = std::list<ILogOutputter *>;
@ -133,7 +133,7 @@ private:
mutable std::mutex m_mutex;
OutputterList m_outputters;
OutputterList m_alwaysOutputters;
int m_maxPriority;
LogLevel m_maxPriority;
};
/*!
@ -145,7 +145,7 @@ LOG((CLOG_XXX "%d and %d are %s", x, y, x == y ? "equal" : "not equal"));
\endcode
In particular, notice the double open and close parentheses. Also note
that there is no comma after the \c CLOG_XXX. The \c XXX should be
replaced by one of enumerants in \c Log::ELevel without the leading
replaced by one of enumerants in \c Log::LogLevel without the leading
\c k. For example, \c CLOG_INFO. The special \c CLOG_PRINT level will
not be filtered and is never prefixed by the filename and line number.
@ -164,7 +164,7 @@ LOGC(x == y, (CLOG_XXX "%d and %d are equal", x, y));
\endcode
In particular, notice the parentheses around everything after the boolean
expression. Also note that there is no comma after the \c CLOG_XXX.
The \c XXX should be replaced by one of enumerants in \c Log::ELevel
The \c XXX should be replaced by one of enumerants in \c Log::LogLevel
without the leading \c k. For example, \c CLOG_INFO. The special
\c CLOG_PRINT level will not be filtered and is never prefixed by the
filename and line number.

28
src/lib/base/LogLevel.h Normal file
View File

@ -0,0 +1,28 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#pragma once
//! Log levels
/*!
The logging priority levels in order of highest to lowest priority.
*/
enum class LogLevel
{
Print = -1, //!< For print only (no file or time)
Fatal, //!< For fatal errors
Error, //!< For serious errors
Warning, //!< For minor errors and warnings
Note, //!< For messages about notable events
Info, //!< For informational messages
Debug, //!< For important debugging messages
Debug1, //!< For verbosity +1 debugging messages
Debug2, //!< For verbosity +2 debugging messages
Debug3, //!< For verbosity +3 debugging messages
Debug4, //!< For verbosity +4 debugging messages
Debug5 //!< For verbosity +5 debugging messages
};

View File

@ -38,7 +38,7 @@ void StopLogOutputter::show(bool)
// do nothing
}
bool StopLogOutputter::write(ELevel, const char *)
bool StopLogOutputter::write(LogLevel, const char *)
{
return false;
}
@ -62,9 +62,9 @@ void ConsoleLogOutputter::show(bool showIfEmpty)
// do nothing
}
bool ConsoleLogOutputter::write(ELevel level, const char *msg)
bool ConsoleLogOutputter::write(LogLevel level, const char *msg)
{
if ((level >= kFATAL) && (level <= kWARNING))
if ((level >= LogLevel::Fatal) && (level <= LogLevel::Warning))
std::cerr << msg << std::endl;
else
std::cout << msg << std::endl;
@ -96,7 +96,7 @@ void SystemLogOutputter::show(bool showIfEmpty)
ARCH->showLog(showIfEmpty);
}
bool SystemLogOutputter::write(ELevel level, const char *msg)
bool SystemLogOutputter::write(LogLevel level, const char *msg)
{
ARCH->writeLog(level, msg);
return true;
@ -143,7 +143,7 @@ void FileLogOutputter::setLogFilename(const char *logFile)
m_fileName = logFile;
}
bool FileLogOutputter::write(ELevel level, const char *message)
bool FileLogOutputter::write(LogLevel level, const char *message)
{
bool moveFile = false;

View File

@ -28,7 +28,7 @@ public:
void open(const char *title) override;
void close() override;
void show(bool showIfEmpty) override;
bool write(ELevel level, const char *message) override;
bool write(LogLevel level, const char *message) override;
};
//! Write log to console
@ -46,7 +46,7 @@ public:
void open(const char *title) override;
void close() override;
void show(bool showIfEmpty) override;
bool write(ELevel level, const char *message) override;
bool write(LogLevel level, const char *message) override;
void flush() const;
};
@ -66,7 +66,7 @@ public:
void open(const char *title) override;
void close() override;
void show(bool showIfEmpty) override;
bool write(ELevel level, const char *message) override;
bool write(LogLevel level, const char *message) override;
void setLogFilename(const char *title);
@ -88,7 +88,7 @@ public:
void open(const char *title) override;
void close() override;
void show(bool showIfEmpty) override;
bool write(ELevel level, const char *message) override;
bool write(LogLevel level, const char *message) override;
};
//! Write log to system log only

View File

@ -65,7 +65,7 @@ void logRemoteSecureCipherInfo(const SSL *ssl)
void SslLogger::logSecureLibInfo()
{
if (CLOG->getFilter() >= kDEBUG) {
if (CLOG->getFilter() >= LogLevel::Debug) {
LOG((CLOG_DEBUG "openssl version: %s", SSLeay_version(SSLEAY_VERSION)));
LOG((CLOG_DEBUG1 "openssl flags: %s", SSLeay_version(SSLEAY_CFLAGS)));
LOG((CLOG_DEBUG1 "openssl built on: %s", SSLeay_version(SSLEAY_BUILT_ON)));
@ -76,7 +76,7 @@ void SslLogger::logSecureLibInfo()
void SslLogger::logSecureCipherInfo(const SSL *ssl)
{
if (ssl && CLOG->getFilter() >= kDEBUG1) {
if (ssl && CLOG->getFilter() >= LogLevel::Debug1) {
logLocalSecureCipherInfo(ssl);
logRemoteSecureCipherInfo(ssl);
}

View File

@ -26,7 +26,7 @@ void MSWindowsDebugOutputter::show(bool showIfEmpty)
// do nothing
}
bool MSWindowsDebugOutputter::write(ELevel level, const char *msg)
bool MSWindowsDebugOutputter::write(LogLevel level, const char *msg)
{
OutputDebugString((std::string(msg) + "\n").c_str());
return true;

View File

@ -24,6 +24,6 @@ public:
void open(const char *title) override;
void close() override;
void show(bool showIfEmpty) override;
bool write(ELevel level, const char *message) override;
bool write(LogLevel level, const char *message) override;
void flush();
};

View File

@ -8,8 +8,8 @@
#include "arch/Arch.h"
#include "arch/win32/XArchWindows.h"
#include "base/ELevel.h"
#include "base/Log.h"
#include "base/LogLevel.h"
#include "base/LogOutputters.h"
#include "base/TMethodJob.h"
#include "common/Constants.h"
@ -363,7 +363,7 @@ void MSWindowsWatchdog::outputLoop(void *)
// strip out windows \r chars to prevent extra lines in log file.
std::string output = trimOutputBuffer(buffer);
m_fileLogOutputter.write(kPRINT, output.c_str());
m_fileLogOutputter.write(LogLevel::Print, output.c_str());
#if SYSAPI_WIN32
if (m_foreground) {

View File

@ -990,7 +990,7 @@ bool XWindowsClipboard::sendReply(Reply *reply)
reply->m_replied = true;
// nothing to log
if (CLOG->getFilter() < kDEBUG2) {
if (CLOG->getFilter() < LogLevel::Debug2) {
sendNotify(
reply->m_requestor, m_selection, reply->m_target, reply->m_property, static_cast<unsigned int>(reply->m_time)
);

View File

@ -12,7 +12,7 @@
void ArchStringTests::initTestCase()
{
m_arch.init();
m_log.setFilter(kDEBUG2);
m_log.setFilter(LogLevel::Debug2);
}
void ArchStringTests::convertStringWCToMB_buffer()

View File

@ -13,7 +13,7 @@
void UnicodeTests::initTestCase()
{
m_arch.init();
m_log.setFilter(kDEBUG2);
m_log.setFilter(LogLevel::Debug2);
}
void UnicodeTests::UTF32ToUTF8()

View File

@ -17,7 +17,7 @@
void ArgParserTests::initTestCase()
{
m_arch.init();
m_log.setFilter(kDEBUG2);
m_log.setFilter(LogLevel::Debug2);
static deskflow::ArgsBase base;
m_parser.setArgsBase(base);
}

View File

@ -13,7 +13,7 @@
void ClipboardTests::initTestCase()
{
m_arch.init();
m_log.setFilter(kDEBUG2);
m_log.setFilter(LogLevel::Debug2);
}
void ClipboardTests::basicFunction()

View File

@ -18,7 +18,7 @@ void ConfigTests::initTestCase()
QVERIFY(dir.mkpath("tmp/test"));
m_arch.init();
m_log.setFilter(kDEBUG2);
m_log.setFilter(LogLevel::Debug2);
}
void ConfigTests::loadFile()

View File

@ -12,7 +12,7 @@
void LanguageManagerTests::initTestCase()
{
m_arch.init();
m_log.setFilter(kDEBUG2);
m_log.setFilter(LogLevel::Debug2);
}
void LanguageManagerTests::remoteLanguages()

View File

@ -40,7 +40,7 @@ int main(int argc, char **argv)
arch.init();
Log log;
log.setFilter(kDEBUG4);
log.setFilter(LogLevel::Debug4);
::testing::GTEST_FLAG(throw_on_failure) = true;
testing::InitGoogleTest(&argc, argv);

View File

@ -13,7 +13,7 @@
void MSWindowsClipboardTests::initTestCase()
{
m_arch.init();
m_log.setFilter(kDEBUG2);
m_log.setFilter(LogLevel::Debug2);
MSWindowsClipboard clipboard(NULL);

View File

@ -19,7 +19,7 @@
void OSXKeyStateTests::initTestCase()
{
m_arch.init();
m_log.setFilter(kDEBUG2);
m_log.setFilter(LogLevel::Debug2);
}
void OSXKeyStateTests::mapModifiersFromOSX_OSXMask()