Removed LogEntry struct and other bits of tidying in the Logger
This commit is contained in:
@@ -16,26 +16,26 @@ void Logger::silence() {
|
|||||||
Logger::warningPrintingEnabled = false;
|
Logger::warningPrintingEnabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Logger::log(const LogEntry& logEntry) {
|
void Logger::log(const std::string& message, LogLevel level) {
|
||||||
static auto timezoneAbbreviation = Services::DateTimeService::getTimeZoneAbbreviation(
|
using Services::DateTimeService;
|
||||||
logEntry.timestamp
|
const auto timestamp = DateTimeService::currentDateTime();
|
||||||
).toStdString();
|
const auto threadName = Logger::threadName();
|
||||||
|
static const auto timezoneAbbreviation = DateTimeService::getTimeZoneAbbreviation(timestamp).toStdString();
|
||||||
|
|
||||||
const auto lock = std::unique_lock(Logger::printMutex);
|
const auto lock = std::unique_lock(Logger::printMutex);
|
||||||
|
|
||||||
// Print the timestamp and id in a green font color:
|
// Print the timestamp and id in a green font color:
|
||||||
std::cout << "\033[32m";
|
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;
|
+ timezoneAbbreviation;
|
||||||
|
|
||||||
if (!logEntry.threadName.empty()) {
|
if (!threadName.empty()) {
|
||||||
std::cout << " [" << logEntry.threadName << "]";
|
std::cout << " [" << threadName << "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << " [" << logEntry.id << "]: ";
|
std::cout << ": \033[0m";
|
||||||
std::cout << "\033[0m";
|
|
||||||
|
|
||||||
switch (logEntry.logLevel) {
|
switch (level) {
|
||||||
case LogLevel::ERROR: {
|
case LogLevel::ERROR: {
|
||||||
// Errors in red
|
// Errors in red
|
||||||
std::cout << "\033[31m";
|
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<char, 16> 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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,13 +3,10 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <atomic>
|
|
||||||
|
|
||||||
#include "src/ProjectConfig.hpp"
|
#include "src/ProjectConfig.hpp"
|
||||||
#include "src/Services/DateTimeService.hpp"
|
#include "src/Services/DateTimeService.hpp"
|
||||||
|
|
||||||
static_assert(std::atomic<std::uint32_t>::is_always_lock_free);
|
|
||||||
|
|
||||||
enum class LogLevel: std::uint8_t
|
enum class LogLevel: std::uint8_t
|
||||||
{
|
{
|
||||||
INFO,
|
INFO,
|
||||||
@@ -18,39 +15,6 @@ enum class LogLevel: std::uint8_t
|
|||||||
DEBUG,
|
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.
|
* Super simple thread safe static Logger class for basic logging.
|
||||||
*/
|
*/
|
||||||
@@ -63,25 +27,25 @@ public:
|
|||||||
|
|
||||||
static void info(const std::string& message) {
|
static void info(const std::string& message) {
|
||||||
if (Logger::infoPrintingEnabled) {
|
if (Logger::infoPrintingEnabled) {
|
||||||
Logger::log(LogEntry(message, LogLevel::INFO));
|
Logger::log(message, LogLevel::INFO);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void warning(const std::string& message) {
|
static void warning(const std::string& message) {
|
||||||
if (Logger::warningPrintingEnabled) {
|
if (Logger::warningPrintingEnabled) {
|
||||||
Logger::log(LogEntry(message, LogLevel::WARNING));
|
Logger::log(message, LogLevel::WARNING);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void error(const std::string& message) {
|
static void error(const std::string& message) {
|
||||||
if (Logger::errorPrintingEnabled) {
|
if (Logger::errorPrintingEnabled) {
|
||||||
Logger::log(LogEntry(message, LogLevel::ERROR));
|
Logger::log(message, LogLevel::ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void debug(const std::string& message) {
|
static void debug(const std::string& message) {
|
||||||
if (Logger::debugPrintingEnabled) {
|
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 inline std::mutex printMutex;
|
||||||
|
|
||||||
static void log(const LogEntry& logEntry);
|
static void log(const std::string& message, LogLevel level);
|
||||||
|
static const std::string& threadName();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user