1
0
mirror of synced 2025-01-29 19:17:28 +01:00

sys: Streamline logger functions, reorder tags for better readability

This commit is contained in:
WerWolv 2022-02-06 00:42:38 +01:00
parent 545ff9de56
commit 16a9d0c0c6

View File

@ -8,62 +8,47 @@
namespace hex::log { namespace hex::log {
FILE *getDestination();
bool isRedirected();
namespace { namespace {
void printPrefix() { void printPrefix(const fmt::text_style &ts, const std::string &level) {
const auto now = fmt::localtime(std::chrono::system_clock::now()); const auto now = fmt::localtime(std::chrono::system_clock::now());
fmt::print("[{0:%H:%M:%S}] [{1}] ", now, IMHEX_PROJECT_NAME); fmt::print("[{0:%H:%M:%S}] ", now);
fmt::print(ts, "{0} ", level);
fmt::print("[{0}] ", IMHEX_PROJECT_NAME);
}
template<typename... T>
void print(const fmt::text_style &ts, const std::string &level, const std::string &fmt, auto... args) {
printPrefix(ts, level);
fmt::print(getDestination(), fmt::runtime(fmt), args...);
fmt::print("\n");
} }
} }
FILE *getDestination(); void debug(const std::string &fmt, auto &&...args) {
bool isRedirected();
template<typename... T>
void print(fmt::format_string<T...> fmt, T &&...args) {
fmt::print(getDestination(), fmt, args...);
}
template<typename S, typename... Args>
void print(const fmt::text_style &ts, const S &fmt, const Args &...args) {
printPrefix();
if (isRedirected())
fmt::print(getDestination(), fmt::runtime(fmt), args...);
else
fmt::print(getDestination(), ts, fmt, args...);
}
void debug(const std::string &fmt, auto... args) {
#if defined(DEBUG) #if defined(DEBUG)
log::print(fg(fmt::color::green_yellow) | fmt::emphasis::bold, "[DEBUG] "); log::print(fg(fmt::color::green_yellow) | fmt::emphasis::bold, "[DEBUG]", fmt, args...);
log::print(fmt::runtime(fmt), args...);
log::print("\n");
#endif #endif
} }
void info(const std::string &fmt, auto... args) { void info(const std::string &fmt, auto &&...args) {
log::print(fg(fmt::color::cadet_blue) | fmt::emphasis::bold, "[INFO] "); log::print(fg(fmt::color::cadet_blue) | fmt::emphasis::bold, "[INFO] ", fmt, args...);
log::print(fmt::runtime(fmt), args...);
log::print("\n");
} }
void warn(const std::string &fmt, auto... args) { void warn(const std::string &fmt, auto &&...args) {
log::print(fg(fmt::color::orange) | fmt::emphasis::bold, "[WARN] "); log::print(fg(fmt::color::orange) | fmt::emphasis::bold, "[WARN] ", fmt, args...);
log::print(fmt::runtime(fmt), args...);
log::print("\n");
} }
void error(const std::string &fmt, auto... args) { void error(const std::string &fmt, auto &&...args) {
log::print(fg(fmt::color::red) | fmt::emphasis::bold, "[ERROR] "); log::print(fg(fmt::color::red) | fmt::emphasis::bold, "[ERROR]", fmt, args...);
log::print(fmt::runtime(fmt), args...);
log::print("\n");
} }
void fatal(const std::string &fmt, auto... args) { void fatal(const std::string &fmt, auto &&...args) {
log::print(fg(fmt::color::purple) | fmt::emphasis::bold, "[FATAL] "); log::print(fg(fmt::color::purple) | fmt::emphasis::bold, "[FATAL]", fmt, args...);
log::print(fmt::runtime(fmt), args...);
log::print("\n");
} }
void redirectToFile(); void redirectToFile();