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 #ifndef LOGGER_H
#define LOGGER_H #define LOGGER_H
#include <ctime>
#include <iomanip>
#include <iostream>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <mutex> #include <mutex>
@@ -29,7 +32,7 @@ public:
void print(int level, const char* message, Args... args) { void print(int level, const char* message, Args... args) {
std::lock_guard<std::mutex> lock(log_mutex); std::lock_guard<std::mutex> lock(log_mutex);
char* prepared; char* prepared;
asprintf(&prepared, "[%c] %s\n", logTypeChar(level), message); asprintf(&prepared, "[%s] (%c) %s\n", getTime(), logTypeChar(level), message);
char* formatted; char* formatted;
asprintf(&formatted, prepared, args...); asprintf(&formatted, prepared, args...);
fprintf(stdout, formatted); fprintf(stdout, formatted);
@@ -48,6 +51,18 @@ public:
private: private:
std::map<int, std::ofstream> logfiles; 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) { static const char logTypeChar(int level) {
switch(level) { switch(level) {
default: return ' '; default: return ' ';