Initial commit

This commit is contained in:
Nav
2021-04-04 21:04:12 +01:00
commit a29c5e1fec
549 changed files with 441216 additions and 0 deletions

66
src/Logger/Logger.cpp Normal file
View File

@@ -0,0 +1,66 @@
#include <iostream>
#include <thread>
#include "Logger.hpp"
using namespace Bloom;
void Logger::log(const std::string& message, LogLevel logLevel, bool print) {
auto lock = std::unique_lock(Logger::logMutex);
auto logEntry = LogEntry(message, logLevel);
Logger::logEntries.push_back(logEntry);
auto index = Logger::logEntries.size();
if (print) {
// Print the timestamp and index in a green font color:
std::cout << "\033[32m";
std::cout << logEntry.timestamp.toString("yyyy-MM-dd hh:mm:ss ").toStdString() +
DateTime::getTimeZoneAbbreviation(logEntry.timestamp).toStdString();
if (!logEntry.threadName.empty()) {
std::cout << " [" << logEntry.threadName << "]";
}
// The index serves as an ID for each log entry. Just here for convenience when referencing specific log entries.
std::cout << " [" << index << "]: ";
std::cout << "\033[0m";
switch (logLevel) {
case LogLevel::ERROR: {
// Errors in red
std::cout << "\033[31m";
std::cout << "[ERROR] ";
break;
}
case LogLevel::WARNING: {
// Warnings in yellow
std::cout << "\033[33m";
std::cout << "[WARNING] ";
break;
}
case LogLevel::INFO: {
std::cout << "[INFO] ";
break;
}
case LogLevel::DEBUG: {
std::cout << "[DEBUG] ";
break;
}
}
std::cout << logEntry.message << "\033[0m" << std::endl;
}
}
void Logger::configure(ApplicationConfig& applicationConfig) {
if (applicationConfig.debugLoggingEnabled) {
Logger::debugPrintingEnabled = true;
Logger::debug("Debug log printing has been enabled.");
}
}
void Logger::silence() {
Logger::debugPrintingEnabled = false;
Logger::infoPrintingEnabled = false;
Logger::errorPrintingEnabled = false;
Logger::warningPrintingEnabled = false;
}

103
src/Logger/Logger.hpp Normal file
View File

@@ -0,0 +1,103 @@
#pragma once
#include <memory>
#include <map>
#include <string>
#include <functional>
#include <QTimeZone>
#include <mutex>
#include "src/ApplicationConfig.hpp"
#include "src/Helpers/DateTime.hpp"
namespace Bloom
{
enum class LogLevel
{
INFO = 1,
WARNING = 2,
ERROR = 3,
DEBUG = 4,
};
struct LogEntry {
std::string threadName;
std::string message;
LogLevel logLevel;
QDateTime timestamp = DateTime::currentDateTime();
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;
}
};
};
/**
* Super simple thread safe static Logger class for basic logging.
*
* TODO: Add the ability to dump the logs to a file.
*/
class Logger
{
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 void log(const std::string& message, LogLevel logLevel, bool print);
public:
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);
}
static void warning(const std::string& message) {
Logger::log(message, LogLevel::WARNING, Logger::warningPrintingEnabled);
}
static void error(const std::string& message) {
Logger::log(message, LogLevel::ERROR, Logger::errorPrintingEnabled);
}
static void debug(const std::string& message) {
Logger::log(message, LogLevel::DEBUG, Logger::debugPrintingEnabled);
}
static void configure(ApplicationConfig& applicationConfig);
static void silence();
};
}