2021-04-20 21:46:48 +02:00
|
|
|
#pragma once
|
|
|
|
|
2022-02-02 17:19:50 +01:00
|
|
|
#include <chrono>
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
#include <fmt/core.h>
|
|
|
|
#include <fmt/color.h>
|
2022-02-02 17:19:50 +01:00
|
|
|
#include <fmt/chrono.h>
|
2021-04-20 21:46:48 +02:00
|
|
|
|
|
|
|
namespace hex::log {
|
|
|
|
|
2022-02-02 17:19:50 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void printPrefix() {
|
|
|
|
const auto now = fmt::localtime(std::chrono::system_clock::now());
|
|
|
|
fmt::print("[{0:%H:%M:%S}] [{1}] ", now, IMHEX_PROJECT_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
FILE *getDestination();
|
|
|
|
bool isRedirected();
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<typename... T>
|
|
|
|
void print(fmt::format_string<T...> fmt, T &&...args) {
|
2022-01-17 20:06:00 +01:00
|
|
|
fmt::print(getDestination(), fmt, args...);
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<typename S, typename... Args>
|
|
|
|
void print(const fmt::text_style &ts, const S &fmt, const Args &...args) {
|
2022-02-02 17:19:50 +01:00
|
|
|
printPrefix();
|
2022-01-17 20:06:00 +01:00
|
|
|
if (isRedirected())
|
|
|
|
fmt::print(getDestination(), fmt::runtime(fmt), args...);
|
|
|
|
else
|
|
|
|
fmt::print(getDestination(), ts, fmt, args...);
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
void debug(const std::string &fmt, auto... args) {
|
|
|
|
#if defined(DEBUG)
|
|
|
|
log::print(fg(fmt::color::green_yellow) | fmt::emphasis::bold, "[DEBUG] ");
|
|
|
|
log::print(fmt::runtime(fmt), args...);
|
|
|
|
log::print("\n");
|
|
|
|
#endif
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
void info(const std::string &fmt, auto... args) {
|
2022-01-17 20:06:00 +01:00
|
|
|
log::print(fg(fmt::color::cadet_blue) | fmt::emphasis::bold, "[INFO] ");
|
|
|
|
log::print(fmt::runtime(fmt), args...);
|
|
|
|
log::print("\n");
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
void warn(const std::string &fmt, auto... args) {
|
2022-01-17 20:06:00 +01:00
|
|
|
log::print(fg(fmt::color::orange) | fmt::emphasis::bold, "[WARN] ");
|
|
|
|
log::print(fmt::runtime(fmt), args...);
|
|
|
|
log::print("\n");
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
void error(const std::string &fmt, auto... args) {
|
2022-01-17 20:06:00 +01:00
|
|
|
log::print(fg(fmt::color::red) | fmt::emphasis::bold, "[ERROR] ");
|
|
|
|
log::print(fmt::runtime(fmt), args...);
|
|
|
|
log::print("\n");
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
void fatal(const std::string &fmt, auto... args) {
|
2022-01-17 20:06:00 +01:00
|
|
|
log::print(fg(fmt::color::purple) | fmt::emphasis::bold, "[FATAL] ");
|
|
|
|
log::print(fmt::runtime(fmt), args...);
|
|
|
|
log::print("\n");
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
void redirectToFile();
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|