Removed LogEntry struct and other bits of tidying in the Logger

This commit is contained in:
Nav
2023-09-22 22:23:53 +01:00
parent d01f975167
commit 4e4a3b3355
2 changed files with 46 additions and 52 deletions

View File

@@ -3,13 +3,10 @@
#include <cstdint>
#include <string>
#include <mutex>
#include <atomic>
#include "src/ProjectConfig.hpp"
#include "src/Services/DateTimeService.hpp"
static_assert(std::atomic<std::uint32_t>::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<char, 16> 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<std::uint32_t> 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();
};