mirror of
https://github.com/open-goal/jak-project
synced 2026-05-27 08:09:29 -04:00
4d751af38e
Favors the `lg` namespace over `fmt` directly, as this will output the logs to a file / has log levels. I also made assertion errors go to a file, this unfortunately means importing `lg` and hence `fmt` which was attempted to be avoided before. But I'm not sure how else to do this aspect without re-inventing the file logging. We have a lot of commented out prints as well that we should probably cleanup at some point / switch them to trace level and default to `info` level. I noticed the pattern of disabling debug logs behind some boolean, something to consider cleaning up in the future -- if our logs were more structured (knowing where they are coming from) then a lot this boilerplate could be eliminated. Closes #1358
95 lines
2.5 KiB
C++
95 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include <ctime>
|
|
|
|
#ifdef __linux__
|
|
#include <sys/time.h>
|
|
#endif
|
|
#include <string>
|
|
|
|
#include "third-party/fmt/color.h"
|
|
#include "third-party/fmt/core.h"
|
|
|
|
namespace lg {
|
|
|
|
#ifdef __linux__
|
|
struct LogTime {
|
|
timeval tv;
|
|
};
|
|
#else
|
|
struct LogTime {
|
|
time_t tim;
|
|
};
|
|
#endif
|
|
|
|
// Logging API
|
|
enum class level { trace = 0, debug = 1, info = 2, warn = 3, error = 4, die = 5, off = 6 };
|
|
|
|
namespace internal {
|
|
// log implementation stuff, not to be called by the user
|
|
void log_message(level log_level, LogTime& now, const char* message);
|
|
void log_print(const char* message);
|
|
} // namespace internal
|
|
|
|
void set_file(const std::string& filename);
|
|
void set_flush_level(level log_level);
|
|
void set_file_level(level log_level);
|
|
void set_stdout_level(level log_level);
|
|
void set_max_debug_levels();
|
|
void initialize();
|
|
void finish();
|
|
|
|
template <typename... Args>
|
|
void log(level log_level, const std::string& format, Args&&... args) {
|
|
LogTime now;
|
|
#ifdef __linux__
|
|
gettimeofday(&now.tv, nullptr);
|
|
#else
|
|
now.tim = time(nullptr);
|
|
#endif
|
|
std::string formatted_message = fmt::format(format, std::forward<Args>(args)...);
|
|
internal::log_message(log_level, now, formatted_message.c_str());
|
|
}
|
|
|
|
template <typename... Args>
|
|
void print(const std::string& format, Args&&... args) {
|
|
std::string formatted_message = fmt::format(format, std::forward<Args>(args)...);
|
|
internal::log_print(formatted_message.c_str());
|
|
}
|
|
template <typename... Args>
|
|
void print(const fmt::text_style& ts, const std::string& format, Args&&... args) {
|
|
std::string formatted_message = fmt::format(ts, format, std::forward<Args>(args)...);
|
|
internal::log_print(formatted_message.c_str());
|
|
}
|
|
|
|
template <typename... Args>
|
|
void trace(const std::string& format, Args&&... args) {
|
|
log(level::trace, format, std::forward<Args>(args)...);
|
|
}
|
|
|
|
template <typename... Args>
|
|
void debug(const std::string& format, Args&&... args) {
|
|
log(level::debug, format, std::forward<Args>(args)...);
|
|
}
|
|
|
|
template <typename... Args>
|
|
void info(const std::string& format, Args&&... args) {
|
|
log(level::info, format, std::forward<Args>(args)...);
|
|
}
|
|
|
|
template <typename... Args>
|
|
void warn(const std::string& format, Args&&... args) {
|
|
log(level::warn, format, std::forward<Args>(args)...);
|
|
}
|
|
|
|
template <typename... Args>
|
|
void error(const std::string& format, Args&&... args) {
|
|
log(level::error, format, std::forward<Args>(args)...);
|
|
}
|
|
|
|
template <typename... Args>
|
|
void die(const std::string& format, Args&&... args) {
|
|
log(level::die, format, std::forward<Args>(args)...);
|
|
}
|
|
} // namespace lg
|