refactor(log): remove use of g_priority array

This commit is contained in:
sithlord48 2026-05-10 11:34:10 -04:00 committed by Nick Bolton
parent a3f97e55e0
commit eb97b7641e
2 changed files with 10 additions and 13 deletions

View file

@ -24,9 +24,6 @@
const int kPriorityPrefixLength = 3; const int kPriorityPrefixLength = 3;
// names of priorities
static const char *g_priority[] = {"FATAL", "ERROR", "WARNING", "INFO", "DEBUG", "VERBOSE"};
// number of priorities // number of priorities
static const int g_numPriority = 6; static const int g_numPriority = 6;
@ -63,16 +60,14 @@ std::vector<char> makeMessage(const char *filename, int lineNumber, const char *
// base size includes null terminator, colon, space, etc. // base size includes null terminator, colon, space, etc.
const int baseSize = 10; const int baseSize = 10;
const int priorityMaxSize = 10;
const auto currentPriority = static_cast<int>(priority); const auto currentPriority = static_cast<int>(priority);
auto timeStr = QDateTime::currentDateTime().toString(Qt::ISODateWithMs).toStdString(); auto timeStr = QDateTime::currentDateTime().toString(Qt::ISODateWithMs).toStdString();
auto sectionName = g_priority[currentPriority]; auto sectionName = LogLevel::toOption(priority).toStdString();
size_t priorityLength = strnlen(sectionName, priorityMaxSize);
size_t messageLength = strnlen(message, SIZE_MAX); size_t messageLength = strnlen(message, SIZE_MAX);
size_t bufferSize = baseSize + timeStr.length() + priorityLength + messageLength; size_t bufferSize = baseSize + timeStr.length() + sectionName.length() + messageLength;
const auto filenameSet = filename != nullptr && filename[0] != '\0'; const auto filenameSet = filename != nullptr && filename[0] != '\0';
if (filenameSet) { if (filenameSet) {
@ -83,20 +78,22 @@ std::vector<char> makeMessage(const char *filename, int lineNumber, const char *
std::vector<char> buffer(bufferSize); std::vector<char> buffer(bufferSize);
#if HAVE_FORMAT #if HAVE_FORMAT
std::format_to_n( std::format_to_n(
buffer.data(), bufferSize, "[{}] {}: {}\n\t{}:{}", timeStr.c_str(), sectionName, message, filename, lineNumber buffer.data(), bufferSize, "[{}] {}: {}\n\t{}:{}", timeStr.c_str(), sectionName.c_str(), message, filename,
lineNumber
); );
#else #else
snprintf( snprintf(
buffer.data(), bufferSize, "[%s] %s: %s\n\t%s:%d", timeStr.c_str(), sectionName, message, filename, lineNumber buffer.data(), bufferSize, "[%s] %s: %s\n\t%s:%d", timeStr.c_str(), sectionName.c_str(), message, filename,
lineNumber
); );
#endif #endif
return buffer; return buffer;
} else { } else {
std::vector<char> buffer(bufferSize); std::vector<char> buffer(bufferSize);
#if HAVE_FORMAT #if HAVE_FORMAT
std::format_to_n(buffer.data(), bufferSize, "[{}] {}: {}", timeStr.c_str(), sectionName, message); std::format_to_n(buffer.data(), bufferSize, "[{}] {}: {}", timeStr.c_str(), sectionName.c_str(), message);
#else #else
snprintf(buffer.data(), bufferSize, "[%s] %s: %s", timeStr.c_str(), sectionName, message); snprintf(buffer.data(), bufferSize, "[%s] %s: %s", timeStr.c_str(), sectionName.c_str(), message);
#endif #endif
return buffer; return buffer;
} }
@ -221,7 +218,7 @@ bool Log::setFilter(const QString &maxPriority)
} }
for (int i = 0; i < g_numPriority; ++i) { for (int i = 0; i < g_numPriority; ++i) {
if (maxPriority == QString(g_priority[i])) { if (maxPriority == LogLevel::toOption((i))) {
setFilter(static_cast<LogLevel::Level>(i)); setFilter(static_cast<LogLevel::Level>(i));
return true; return true;
} }

View file

@ -18,7 +18,7 @@ void LogLevelTests::checkLogLevels_Valid()
QCOMPARE(LogLevel::fromOption(QStringLiteral("wArning")), LogLevel::Level::Warning); QCOMPARE(LogLevel::fromOption(QStringLiteral("wArning")), LogLevel::Level::Warning);
QCOMPARE(LogLevel::fromOption(QStringLiteral("info")), LogLevel::Level::Info); QCOMPARE(LogLevel::fromOption(QStringLiteral("info")), LogLevel::Level::Info);
QCOMPARE(LogLevel::fromOption("deBug"), LogLevel::Level::Debug); QCOMPARE(LogLevel::fromOption("deBug"), LogLevel::Level::Debug);
QCOMPARE(LogLevel::fromOption("TraCe"), LogLevel::Level::Verbose); QCOMPARE(LogLevel::fromOption("vERBOse"), LogLevel::Level::Verbose);
} }
void LogLevelTests::checkLogLevels_Invalid() void LogLevelTests::checkLogLevels_Invalid()