From 4e4a3b3355b9b804e0c04d4d07e93db5b9cd458d Mon Sep 17 00:00:00 2001 From: Nav Date: Fri, 22 Sep 2023 22:23:53 +0100 Subject: [PATCH] Removed `LogEntry` struct and other bits of tidying in the Logger --- src/Logger/Logger.cpp | 51 +++++++++++++++++++++++++++++++++---------- src/Logger/Logger.hpp | 47 +++++---------------------------------- 2 files changed, 46 insertions(+), 52 deletions(-) diff --git a/src/Logger/Logger.cpp b/src/Logger/Logger.cpp index 2892c960..0c8ec121 100644 --- a/src/Logger/Logger.cpp +++ b/src/Logger/Logger.cpp @@ -16,26 +16,26 @@ void Logger::silence() { Logger::warningPrintingEnabled = false; } -void Logger::log(const LogEntry& logEntry) { - static auto timezoneAbbreviation = Services::DateTimeService::getTimeZoneAbbreviation( - logEntry.timestamp - ).toStdString(); +void Logger::log(const std::string& message, LogLevel level) { + using Services::DateTimeService; + const auto timestamp = DateTimeService::currentDateTime(); + const auto threadName = Logger::threadName(); + static const auto timezoneAbbreviation = DateTimeService::getTimeZoneAbbreviation(timestamp).toStdString(); const auto lock = std::unique_lock(Logger::printMutex); // Print the timestamp and id in a green font color: std::cout << "\033[32m"; - std::cout << logEntry.timestamp.toString("yyyy-MM-dd hh:mm:ss.zzz ").toStdString() + std::cout << timestamp.toString("yyyy-MM-dd hh:mm:ss.zzz ").toStdString() + timezoneAbbreviation; - if (!logEntry.threadName.empty()) { - std::cout << " [" << logEntry.threadName << "]"; + if (!threadName.empty()) { + std::cout << " [" << threadName << "]"; } - std::cout << " [" << logEntry.id << "]: "; - std::cout << "\033[0m"; + std::cout << ": \033[0m"; - switch (logEntry.logLevel) { + switch (level) { case LogLevel::ERROR: { // Errors in red std::cout << "\033[31m"; @@ -58,5 +58,34 @@ void Logger::log(const LogEntry& logEntry) { } } - std::cout << logEntry.message << "\033[0m" << std::endl; + std::cout << message << "\033[0m" << std::endl; +} + +const std::string& Logger::threadName() { + static auto nameCache = std::map<::pthread_t, std::string>(); + + const auto threadId = ::pthread_self(); + + auto nameIt = nameCache.find(threadId); + if (nameIt == nameCache.end()) { + std::array threadNameBuf = {}; + + if (::pthread_getname_np(::pthread_self(), threadNameBuf.data(), threadNameBuf.size()) != 0) { + static const auto emptyName = std::string(); + return emptyName; + } + + const auto name = std::string(threadNameBuf.data()); + + /* + * The name of the main thread is also the name of the process, so we have to name the + * main thread "Bloom" (to prevent confusion). + * + * We override the main thread name when printing logs, to keep the format of the thread name in the + * logs consistent. + */ + nameIt = nameCache.insert(std::pair(threadId, name == "Bloom" ? "MT" : name)).first; + } + + return nameIt->second; } diff --git a/src/Logger/Logger.hpp b/src/Logger/Logger.hpp index fe765dd7..4c5c1fdb 100644 --- a/src/Logger/Logger.hpp +++ b/src/Logger/Logger.hpp @@ -3,13 +3,10 @@ #include #include #include -#include #include "src/ProjectConfig.hpp" #include "src/Services/DateTimeService.hpp" -static_assert(std::atomic::is_always_lock_free); - enum class LogLevel: std::uint8_t { INFO, @@ -18,39 +15,6 @@ enum class LogLevel: std::uint8_t DEBUG, }; -class LogEntry -{ -public: - std::uint32_t id = ++(LogEntry::lastLogId); - std::string message; - LogLevel logLevel; - QDateTime timestamp = Services::DateTimeService::currentDateTime(); - std::string threadName; - - LogEntry(std::string message, LogLevel logLevel) - : message(std::move(message)) - , logLevel(logLevel) - { - // Get thread name - std::array threadNameBuf = {}; - - if (::pthread_getname_np(::pthread_self(), threadNameBuf.data(), threadNameBuf.size()) == 0) { - /* - * The name of the main thread is also the name of the process, so we have to name the - * main thread "Bloom" (to prevent confusion). - * - * We override the main thread name when printing logs, to keep the format of the thread name in the - * logs consistent. - */ - this->threadName = std::string(threadNameBuf.data()); - this->threadName = this->threadName == "Bloom" ? "MT" : this->threadName; - } - }; - -private: - static inline std::atomic lastLogId = 0; -}; - /** * Super simple thread safe static Logger class for basic logging. */ @@ -63,25 +27,25 @@ public: static void info(const std::string& message) { if (Logger::infoPrintingEnabled) { - Logger::log(LogEntry(message, LogLevel::INFO)); + Logger::log(message, LogLevel::INFO); } } static void warning(const std::string& message) { if (Logger::warningPrintingEnabled) { - Logger::log(LogEntry(message, LogLevel::WARNING)); + Logger::log(message, LogLevel::WARNING); } } static void error(const std::string& message) { if (Logger::errorPrintingEnabled) { - Logger::log(LogEntry(message, LogLevel::ERROR)); + Logger::log(message, LogLevel::ERROR); } } static void debug(const std::string& message) { if (Logger::debugPrintingEnabled) { - Logger::log(LogEntry(message, LogLevel::DEBUG)); + Logger::log(message, LogLevel::DEBUG); } } @@ -93,5 +57,6 @@ private: static inline std::mutex printMutex; - static void log(const LogEntry& logEntry); + static void log(const std::string& message, LogLevel level); + static const std::string& threadName(); };