2021-04-20 21:46:48 +02:00
|
|
|
#pragma once
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
#include <hex.hpp>
|
|
|
|
|
2022-02-02 17:19:50 +01:00
|
|
|
#include <chrono>
|
2022-09-19 16:54:19 +02:00
|
|
|
#include <mutex>
|
2022-02-02 17:19:50 +01:00
|
|
|
|
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
|
|
|
|
2023-05-22 13:24:48 +02:00
|
|
|
#include <wolv/io/file.hpp>
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
namespace hex::log {
|
|
|
|
|
2022-02-06 00:42:38 +01:00
|
|
|
FILE *getDestination();
|
2023-05-22 13:24:48 +02:00
|
|
|
wolv::io::File& getFile();
|
2022-02-06 00:42:38 +01:00
|
|
|
bool isRedirected();
|
|
|
|
|
2022-02-02 17:19:50 +01:00
|
|
|
namespace {
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
[[maybe_unused]] void printPrefix(FILE *dest, const fmt::text_style &ts, const std::string &level) {
|
2023-05-06 10:07:22 +02:00
|
|
|
const auto now = fmt::localtime(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()));
|
2022-02-16 21:31:47 +01:00
|
|
|
|
|
|
|
fmt::print(dest, "[{0:%H:%M:%S}] ", now);
|
|
|
|
|
|
|
|
if (isRedirected())
|
|
|
|
fmt::print(dest, "{0} ", level);
|
|
|
|
else
|
|
|
|
fmt::print(dest, ts, "{0} ", level);
|
|
|
|
|
|
|
|
fmt::print(dest, "[{0}] ", IMHEX_PROJECT_NAME);
|
2022-02-02 17:19:50 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 00:42:38 +01:00
|
|
|
template<typename... T>
|
2022-03-27 00:01:28 +01:00
|
|
|
[[maybe_unused]] void print(const fmt::text_style &ts, const std::string &level, const std::string &fmt, auto... args) {
|
2022-02-16 21:31:47 +01:00
|
|
|
auto dest = getDestination();
|
|
|
|
|
2023-06-12 08:24:36 +02:00
|
|
|
static std::mutex loggerMutex;
|
|
|
|
std::scoped_lock lock(loggerMutex);
|
|
|
|
|
2022-02-16 21:31:47 +01:00
|
|
|
printPrefix(dest, ts, level);
|
|
|
|
fmt::print(dest, fmt::runtime(fmt), args...);
|
|
|
|
fmt::print(dest, "\n");
|
2022-02-06 00:42:38 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
}
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
[[maybe_unused]] void debug(const std::string &fmt, auto &&...args) {
|
2022-01-24 20:53:17 +01:00
|
|
|
#if defined(DEBUG)
|
2022-07-30 11:26:51 +02:00
|
|
|
hex::log::print(fg(fmt::color::light_green) | fmt::emphasis::bold, "[DEBUG]", fmt, args...);
|
2022-03-27 00:01:28 +01:00
|
|
|
#else
|
|
|
|
hex::unused(fmt, args...);
|
2022-01-24 20:53:17 +01:00
|
|
|
#endif
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
[[maybe_unused]] void info(const std::string &fmt, auto &&...args) {
|
2022-04-07 12:47:45 +02:00
|
|
|
hex::log::print(fg(fmt::color::cadet_blue) | fmt::emphasis::bold, "[INFO] ", fmt, args...);
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
[[maybe_unused]] void warn(const std::string &fmt, auto &&...args) {
|
2022-04-07 12:47:45 +02:00
|
|
|
hex::log::print(fg(fmt::color::orange) | fmt::emphasis::bold, "[WARN] ", fmt, args...);
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
[[maybe_unused]] void error(const std::string &fmt, auto &&...args) {
|
2022-04-07 12:47:45 +02:00
|
|
|
hex::log::print(fg(fmt::color::red) | fmt::emphasis::bold, "[ERROR]", fmt, args...);
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
[[maybe_unused]] void fatal(const std::string &fmt, auto &&...args) {
|
2022-04-07 12:47:45 +02:00
|
|
|
hex::log::print(fg(fmt::color::purple) | fmt::emphasis::bold, "[FATAL]", fmt, args...);
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
[[maybe_unused]] void redirectToFile();
|
2022-01-17 20:06:00 +01:00
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|