Files
BloomPatched/src/Logger/Logger.hpp

101 lines
2.9 KiB
C++
Raw Normal View History

2021-04-04 21:04:12 +01:00
#pragma once
2022-06-18 14:36:39 +01:00
#include <cstdint>
2021-04-04 21:04:12 +01:00
#include <string>
#include <mutex>
2022-06-18 14:36:39 +01:00
#include <atomic>
2021-04-04 21:04:12 +01:00
#include "src/ProjectConfig.hpp"
#include "src/Services/DateTimeService.hpp"
2021-04-04 21:04:12 +01:00
namespace Bloom
{
2022-06-18 14:36:39 +01:00
static_assert(std::atomic<std::uint32_t>::is_always_lock_free);
enum class LogLevel: std::uint8_t
2021-04-04 21:04:12 +01:00
{
2022-06-18 14:36:39 +01:00
INFO,
WARNING,
ERROR,
DEBUG,
2021-04-04 21:04:12 +01:00
};
2022-06-18 14:36:39 +01:00
class LogEntry
2021-05-25 21:50:17 +01:00
{
2022-06-18 14:36:39 +01:00
public:
std::uint32_t id = ++(LogEntry::lastLogId);
2021-04-04 21:04:12 +01:00
std::string message;
LogLevel logLevel;
QDateTime timestamp = Services::DateTimeService::currentDateTime();
2022-06-18 14:36:39 +01:00
std::string threadName;
2021-04-04 21:04:12 +01:00
2022-06-18 14:36:39 +01:00
LogEntry(std::string message, LogLevel logLevel)
: message(std::move(message))
, logLevel(logLevel)
{
2021-04-04 21:04:12 +01:00
// Get thread name
2021-06-21 00:14:31 +01:00
std::array<char, 16> threadNameBuf = {};
2021-04-04 21:04:12 +01:00
2022-06-18 14:36:39 +01:00
if (::pthread_getname_np(::pthread_self(), threadNameBuf.data(), threadNameBuf.size()) == 0) {
2021-04-04 21:04:12 +01:00
/*
* 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;
}
};
2022-06-18 14:36:39 +01:00
private:
static inline std::atomic<std::uint32_t> lastLogId = 0;
2021-04-04 21:04:12 +01:00
};
/**
* Super simple thread safe static Logger class for basic logging.
*/
class Logger
{
public:
2022-08-13 03:06:44 +01:00
static void configure(const ProjectConfig& projectConfig);
2021-04-04 21:04:12 +01:00
static void silence();
2021-04-04 21:04:12 +01:00
static void info(const std::string& message) {
2022-06-18 14:36:39 +01:00
if (Logger::infoPrintingEnabled) {
Logger::log(LogEntry(message, LogLevel::INFO));
}
2021-04-04 21:04:12 +01:00
}
static void warning(const std::string& message) {
2022-06-18 14:36:39 +01:00
if (Logger::warningPrintingEnabled) {
Logger::log(LogEntry(message, LogLevel::WARNING));
}
2021-04-04 21:04:12 +01:00
}
static void error(const std::string& message) {
2022-06-18 14:36:39 +01:00
if (Logger::errorPrintingEnabled) {
Logger::log(LogEntry(message, LogLevel::ERROR));
}
2021-04-04 21:04:12 +01:00
}
static void debug(const std::string& message) {
2022-06-18 14:36:39 +01:00
if (Logger::debugPrintingEnabled) {
Logger::log(LogEntry(message, LogLevel::DEBUG));
}
2021-04-04 21:04:12 +01:00
}
private:
static inline bool errorPrintingEnabled = true;
static inline bool warningPrintingEnabled = true;
static inline bool infoPrintingEnabled = true;
static inline bool debugPrintingEnabled = false;
2022-06-18 14:36:39 +01:00
static inline std::mutex printMutex;
2022-06-18 14:36:39 +01:00
static void log(const LogEntry& logEntry);
2021-04-04 21:04:12 +01:00
};
}