2021-12-05 17:54:36 +00:00
|
|
|
#include "dLogger.h"
|
|
|
|
|
2021-12-11 12:29:34 +00:00
|
|
|
dLogger::dLogger(const std::string& outpath, bool logToConsole, bool logDebugStatements) {
|
2021-12-05 17:54:36 +00:00
|
|
|
m_logToConsole = logToConsole;
|
2021-12-11 12:29:34 +00:00
|
|
|
m_logDebugStatements = logDebugStatements;
|
2021-12-05 17:54:36 +00:00
|
|
|
m_outpath = outpath;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
mFile = std::ofstream(m_outpath);
|
|
|
|
if (!mFile) { printf("Couldn't open %s for writing!\n", outpath.c_str()); }
|
|
|
|
#else
|
|
|
|
fp = fopen(outpath.c_str(), "wt");
|
|
|
|
if (fp == NULL) { printf("Couldn't open %s for writing!\n", outpath.c_str()); }
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
dLogger::~dLogger() {
|
|
|
|
#ifdef _WIN32
|
|
|
|
mFile.close();
|
|
|
|
#else
|
|
|
|
if (fp != nullptr) {
|
|
|
|
fclose(fp);
|
|
|
|
fp = nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-11 12:29:34 +00:00
|
|
|
void dLogger::vLog(const char* format, va_list args) {
|
2021-12-05 17:54:36 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
time_t t = time(NULL);
|
|
|
|
struct tm time;
|
|
|
|
localtime_s(&time, &t);
|
|
|
|
char timeStr[70];
|
|
|
|
strftime(timeStr, sizeof(timeStr), "%d-%m-%y %H:%M:%S", &time);
|
|
|
|
char message[2048];
|
2022-08-01 20:23:01 +00:00
|
|
|
vsnprintf(message, 2048, format, args);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2021-12-11 12:29:34 +00:00
|
|
|
if (m_logToConsole) std::cout << "[" << timeStr << "] " << message;
|
|
|
|
mFile << "[" << timeStr << "] " << message;
|
2021-12-05 17:54:36 +00:00
|
|
|
#else
|
|
|
|
time_t t = time(NULL);
|
2021-12-13 15:57:43 +00:00
|
|
|
struct tm* time = localtime(&t);
|
|
|
|
char timeStr[70];
|
|
|
|
strftime(timeStr, sizeof(timeStr), "%d-%m-%y %H:%M:%S", time);
|
2021-12-05 17:54:36 +00:00
|
|
|
char message[2048];
|
2022-08-01 20:23:01 +00:00
|
|
|
vsnprintf(message, 2048, format, args);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (m_logToConsole) {
|
|
|
|
fputs("[", stdout);
|
|
|
|
fputs(timeStr, stdout);
|
|
|
|
fputs("] ", stdout);
|
|
|
|
fputs(message, stdout);
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (fp != nullptr) {
|
|
|
|
fputs("[", fp);
|
|
|
|
fputs(timeStr, fp);
|
|
|
|
fputs("] ", fp);
|
|
|
|
fputs(message, fp);
|
|
|
|
} else {
|
|
|
|
printf("Logger not initialized!\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-11 12:29:34 +00:00
|
|
|
void dLogger::LogBasic(const char* format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
vLog(format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2021-12-11 12:29:34 +00:00
|
|
|
void dLogger::LogBasic(const std::string& message) {
|
|
|
|
LogBasic(message.c_str());
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2021-12-11 12:29:34 +00:00
|
|
|
void dLogger::Log(const char* className, const char* format, ...) {
|
2021-12-05 17:54:36 +00:00
|
|
|
va_list args;
|
2022-07-25 02:26:51 +00:00
|
|
|
std::string log = "[" + std::string(className) + "] " + std::string(format) + "\n";
|
2021-12-05 17:54:36 +00:00
|
|
|
va_start(args, format);
|
2021-12-11 12:29:34 +00:00
|
|
|
vLog(log.c_str(), args);
|
2021-12-05 17:54:36 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dLogger::Log(const std::string& className, const std::string& message) {
|
|
|
|
Log(className.c_str(), message.c_str());
|
|
|
|
}
|
|
|
|
|
2021-12-11 12:29:34 +00:00
|
|
|
void dLogger::LogDebug(const char* className, const char* format, ...) {
|
|
|
|
if (!m_logDebugStatements) return;
|
|
|
|
va_list args;
|
|
|
|
std::string log = "[" + std::string(className) + "] " + std::string(format);
|
|
|
|
va_start(args, format);
|
|
|
|
vLog(log.c_str(), args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dLogger::LogDebug(const std::string& className, const std::string& message) {
|
|
|
|
LogDebug(className.c_str(), message.c_str());
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
void dLogger::Flush() {
|
|
|
|
#ifdef _WIN32
|
|
|
|
mFile.flush();
|
|
|
|
#else
|
|
|
|
if (fp != nullptr) {
|
|
|
|
std::fflush(fp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|