change logger format to include date/time

This commit is contained in:
2024-12-14 10:10:03 -06:00
parent f4d44043fd
commit bc88a30c0c

View File

@@ -1,5 +1,8 @@
#ifndef LOGGER_H
#define LOGGER_H
#include <ctime>
#include <iomanip>
#include <iostream>
#include <cstring>
#include <fstream>
#include <mutex>
@@ -29,7 +32,7 @@ public:
void print(int level, const char* message, Args... args) {
std::lock_guard<std::mutex> lock(log_mutex);
char* prepared;
asprintf(&prepared, "[%c] %s\n", logTypeChar(level), message);
asprintf(&prepared, "[%s] (%c) %s\n", getTime(), logTypeChar(level), message);
char* formatted;
asprintf(&formatted, prepared, args...);
fprintf(stdout, formatted);
@@ -48,6 +51,18 @@ public:
private:
std::map<int, std::ofstream> logfiles;
static const char* getTime() {
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
static char buffer[20];
strftime(buffer, 20, "%y/%m/%d %H:%M:%S", timeinfo);
return buffer;
}
static const char logTypeChar(int level) {
switch(level) {
default: return ' ';