65 lines
1.8 KiB
C++
65 lines
1.8 KiB
C++
#include <logger.hpp>
|
|
|
|
#include <cstdarg>
|
|
#include <cstdio>
|
|
#include <ctime>
|
|
#include <mutex>
|
|
|
|
namespace {
|
|
|
|
std::mutex g_log_mutex;
|
|
|
|
#if defined(__SWITCH__)
|
|
constexpr const char* kLogPath = "/switch/NXST/log.log";
|
|
#else
|
|
constexpr const char* kLogPath = "nxst.log";
|
|
#endif
|
|
|
|
void writeEntry(const char* tag, const char* fmt, va_list args)
|
|
{
|
|
char msg[2048];
|
|
vsnprintf(msg, sizeof(msg), fmt, args);
|
|
|
|
time_t now = time(nullptr);
|
|
struct tm tm_buf;
|
|
localtime_r(&now, &tm_buf);
|
|
char time_str[16];
|
|
strftime(time_str, sizeof(time_str), "%H:%M:%S", &tm_buf);
|
|
|
|
std::lock_guard<std::mutex> lock(g_log_mutex);
|
|
|
|
fprintf(stderr, "[%s]%s %s\n", time_str, tag, msg);
|
|
|
|
FILE* log_file = fopen(kLogPath, "a");
|
|
if (log_file != nullptr) {
|
|
fprintf(log_file, "[%s]%s %s\n", time_str, tag, msg);
|
|
fclose(log_file);
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
namespace nxst::log {
|
|
|
|
void write(Level level, const char* fmt, ...)
|
|
{
|
|
const char* tag = "[INFO] ";
|
|
switch (level) {
|
|
case Level::Debug: tag = "[DEBUG]"; break;
|
|
case Level::Info: tag = "[INFO] "; break;
|
|
case Level::Warn: tag = "[WARN] "; break;
|
|
case Level::Error: tag = "[ERROR]"; break;
|
|
}
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
writeEntry(tag, fmt, args);
|
|
va_end(args);
|
|
}
|
|
|
|
void debug(const char* fmt, ...) { va_list args; va_start(args, fmt); writeEntry("[DEBUG]", fmt, args); va_end(args); }
|
|
void info (const char* fmt, ...) { va_list args; va_start(args, fmt); writeEntry("[INFO] ", fmt, args); va_end(args); }
|
|
void warn (const char* fmt, ...) { va_list args; va_start(args, fmt); writeEntry("[WARN] ", fmt, args); va_end(args); }
|
|
void error(const char* fmt, ...) { va_list args; va_start(args, fmt); writeEntry("[ERROR]", fmt, args); va_end(args); }
|
|
|
|
} // namespace nxst::log
|