1
0
mirror of synced 2024-12-14 16:52:53 +01:00
ImHex/lib/libimhex/include/hex/helpers/logger.hpp

108 lines
3.6 KiB
C++
Raw Normal View History

#pragma once
#include <hex.hpp>
#include <chrono>
#include <mutex>
#include <fmt/core.h>
#include <fmt/color.h>
#include <fmt/chrono.h>
#include <wolv/io/file.hpp>
namespace hex::log {
2023-06-18 22:32:55 +02:00
namespace impl {
FILE *getDestination();
wolv::io::File& getFile();
bool isRedirected();
[[maybe_unused]] void redirectToFile();
[[maybe_unused]] void enableColorPrinting();
2023-06-18 22:32:55 +02:00
2023-07-23 23:37:47 +02:00
extern std::mutex g_loggerMutex;
2023-06-18 22:32:55 +02:00
2023-07-23 23:37:47 +02:00
struct LogEntry {
std::string project;
std::string level;
std::string message;
};
std::vector<LogEntry>& getLogEntries();
2023-11-10 20:47:08 +01:00
[[maybe_unused]] inline void printPrefix(FILE *dest, const fmt::text_style &ts, const std::string &level) {
const auto now = fmt::localtime(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()));
fmt::print(dest, "[{0:%H:%M:%S}] ", now);
2023-06-18 22:32:55 +02:00
if (impl::isRedirected())
fmt::print(dest, "{0} ", level);
else
fmt::print(dest, ts, "{0} ", level);
fmt::print(dest, "[{0}] ", IMHEX_PROJECT_NAME);
2023-09-03 11:45:20 +02:00
constexpr static auto ProjectNameLength = std::char_traits<char>::length(IMHEX_PROJECT_NAME);
fmt::print(dest, "{}", std::string(ProjectNameLength > 10 ? 0 : 10 - ProjectNameLength, ' '));
}
[[maybe_unused]] void print(const fmt::text_style &ts, const std::string &level, const std::string &fmt, auto && ... args) {
2023-07-23 23:37:47 +02:00
std::scoped_lock lock(impl::g_loggerMutex);
2023-06-18 22:32:55 +02:00
auto dest = impl::getDestination();
printPrefix(dest, ts, level);
2023-07-23 23:37:47 +02:00
auto message = fmt::format(fmt::runtime(fmt), args...);
fmt::print(dest, "{}\n", message);
fflush(dest);
2023-07-23 23:37:47 +02:00
impl::getLogEntries().push_back({ IMHEX_PROJECT_NAME, level, std::move(message) });
}
}
[[maybe_unused]] void debug(const std::string &fmt, auto && ... args) {
2023-07-23 23:37:47 +02:00
#if defined(DEBUG)
2023-11-10 20:47:08 +01:00
hex::log::impl::print(fg(fmt::color::light_green) | fmt::emphasis::bold, "[DEBUG]", fmt, args...);
2023-07-23 23:37:47 +02:00
#else
impl::getLogEntries().push_back({ IMHEX_PROJECT_NAME, "[DEBUG]", fmt::format(fmt::runtime(fmt), args...) });
#endif
}
[[maybe_unused]] void info(const std::string &fmt, auto && ... args) {
2023-11-10 20:47:08 +01:00
hex::log::impl::print(fg(fmt::color::cadet_blue) | fmt::emphasis::bold, "[INFO] ", fmt, args...);
}
[[maybe_unused]] void warn(const std::string &fmt, auto && ... args) {
2023-11-10 20:47:08 +01:00
hex::log::impl::print(fg(fmt::color::orange) | fmt::emphasis::bold, "[WARN] ", fmt, args...);
}
[[maybe_unused]] void error(const std::string &fmt, auto && ... args) {
2023-11-10 20:47:08 +01:00
hex::log::impl::print(fg(fmt::color::red) | fmt::emphasis::bold, "[ERROR]", fmt, args...);
}
[[maybe_unused]] void fatal(const std::string &fmt, auto && ... args) {
2023-11-10 20:47:08 +01:00
hex::log::impl::print(fg(fmt::color::purple) | fmt::emphasis::bold, "[FATAL]", fmt, args...);
}
[[maybe_unused]] void print(const std::string &fmt, auto && ... args) {
std::scoped_lock lock(impl::g_loggerMutex);
auto dest = impl::getDestination();
auto message = fmt::format(fmt::runtime(fmt), args...);
fmt::print(dest, "{}", message);
fflush(dest);
}
[[maybe_unused]] void println(const std::string &fmt, auto && ... args) {
std::scoped_lock lock(impl::g_loggerMutex);
auto dest = impl::getDestination();
auto message = fmt::format(fmt::runtime(fmt), args...);
fmt::print(dest, "{}\n", message);
fflush(dest);
}
}