jak-project/common/log/log.h
Tyler Wilding 5d7aa7cea1
log: rotate log files with timestamps and add flag to disable ANSI colors (#2886)
Rotates the log files with a timestamp instead of copying all files and
incrementing an integer. Increases the amount of info you have when
looking at user's log files (ie. when looking at all the files, the file
creation dates are accurate).


![image](https://github.com/open-goal/jak-project/assets/13153231/61bcdf51-f0f6-4eee-b1e5-140aede5d19e)

Also simplifies the API for setting the log file, and `gk` logs are now
game specific with `jak1` or `jak2`. Which should be useful going
forward.

Lastly, added a flag to all CLIs to disable ansi colors for people that
want to do so. Though at the same time, there is finally a workaround in
jenkins to fix ANSI colors in the truncated log view -- so I'm not sure
why anyone would want to get rid of the color information. You can even
setup text editors to display the color info making log parsing much
easier. Fixes #1917

---------

Co-authored-by: ManDude <7569514+ManDude@users.noreply.github.com>
2023-08-08 17:59:37 +01:00

108 lines
2.7 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_unless_die = 6,
off = 7
};
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,
const bool should_rotate = true,
const bool append = false,
const std::string& dir = "");
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 disable_ansi_colors();
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