Tidied Logger

This commit is contained in:
Nav
2022-06-18 14:36:39 +01:00
parent ae413163d1
commit 6384730a18
2 changed files with 72 additions and 84 deletions

View File

@@ -1,37 +1,42 @@
#pragma once
#include <memory>
#include <map>
#include <cstdint>
#include <string>
#include <functional>
#include <QTimeZone>
#include <mutex>
#include <atomic>
#include "src/ProjectConfig.hpp"
#include "src/Helpers/DateTime.hpp"
namespace Bloom
{
enum class LogLevel
static_assert(std::atomic<std::uint32_t>::is_always_lock_free);
enum class LogLevel: std::uint8_t
{
INFO = 1,
WARNING = 2,
ERROR = 3,
DEBUG = 4,
INFO,
WARNING,
ERROR,
DEBUG,
};
struct LogEntry
class LogEntry
{
std::string threadName;
public:
std::uint32_t id = ++(LogEntry::lastLogId);
std::string message;
LogLevel logLevel;
QDateTime timestamp = DateTime::currentDateTime();
std::string threadName;
LogEntry(std::string message, LogLevel logLevel): message(std::move(message)), logLevel(logLevel) {
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) {
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).
@@ -43,12 +48,13 @@ namespace Bloom
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.
*
* TODO: Add the ability to dump the logs to a file.
*/
class Logger
{
@@ -57,48 +63,38 @@ namespace Bloom
static void silence();
static void setInfoPrinting(bool enabled) {
Logger::infoPrintingEnabled = enabled;
}
static void setWarningPrinting(bool enabled) {
Logger::warningPrintingEnabled = enabled;
}
static void setErrorPrinting(bool enabled) {
Logger::errorPrintingEnabled = enabled;
}
static void info(const std::string& message) {
Logger::log(message, LogLevel::INFO, Logger::infoPrintingEnabled);
if (Logger::infoPrintingEnabled) {
Logger::log(LogEntry(message, LogLevel::INFO));
}
}
static void warning(const std::string& message) {
Logger::log(message, LogLevel::WARNING, Logger::warningPrintingEnabled);
if (Logger::warningPrintingEnabled) {
Logger::log(LogEntry(message, LogLevel::WARNING));
}
}
static void error(const std::string& message) {
Logger::log(message, LogLevel::ERROR, Logger::errorPrintingEnabled);
if (Logger::errorPrintingEnabled) {
Logger::log(LogEntry(message, LogLevel::ERROR));
}
}
static void debug(const std::string& message) {
Logger::log(message, LogLevel::DEBUG, Logger::debugPrintingEnabled);
if (Logger::debugPrintingEnabled) {
Logger::log(LogEntry(message, LogLevel::DEBUG));
}
}
private:
/**
* We keep a record of every log entry for future processing. Maybe dumping to a file or something
* of that nature when a fatal error occurs.
*/
static inline std::vector<LogEntry> logEntries;
static inline bool errorPrintingEnabled = true;
static inline bool warningPrintingEnabled = true;
static inline bool infoPrintingEnabled = true;
static inline bool debugPrintingEnabled = false;
static inline std::mutex logMutex;
static inline std::mutex printMutex;
static void log(const std::string& message, LogLevel logLevel, bool print);
static void log(const LogEntry& logEntry);
};
}