2022-01-17 20:06:00 +01:00
|
|
|
#include <hex/helpers/logger.hpp>
|
2022-03-04 11:36:37 +01:00
|
|
|
#include <hex/helpers/fs.hpp>
|
2022-01-17 20:06:00 +01:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/io/file.hpp>
|
2022-01-17 20:06:00 +01:00
|
|
|
|
2023-06-18 22:32:55 +02:00
|
|
|
namespace hex::log::impl {
|
2022-01-17 20:06:00 +01:00
|
|
|
|
2023-06-18 22:32:55 +02:00
|
|
|
static wolv::io::File s_loggerFile;
|
2023-07-23 23:37:47 +02:00
|
|
|
std::mutex g_loggerMutex;
|
2022-01-17 20:06:00 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
FILE *getDestination() {
|
2023-06-18 22:32:55 +02:00
|
|
|
if (s_loggerFile.isValid())
|
|
|
|
return s_loggerFile.getHandle();
|
2022-01-17 20:06:00 +01:00
|
|
|
else
|
|
|
|
return stdout;
|
|
|
|
}
|
|
|
|
|
2023-05-22 13:24:48 +02:00
|
|
|
wolv::io::File& getFile() {
|
2023-06-18 22:32:55 +02:00
|
|
|
return s_loggerFile;
|
2023-05-22 13:24:48 +02:00
|
|
|
}
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
bool isRedirected() {
|
2023-06-18 22:32:55 +02:00
|
|
|
return s_loggerFile.isValid();
|
2022-01-17 20:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void redirectToFile() {
|
2023-06-18 22:32:55 +02:00
|
|
|
if (s_loggerFile.isValid()) return;
|
2022-01-17 20:06:00 +01:00
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs, true)) {
|
2023-03-12 18:27:29 +01:00
|
|
|
wolv::io::fs::createDirectories(path);
|
2023-06-18 22:32:55 +02:00
|
|
|
s_loggerFile = wolv::io::File(path / hex::format("{0:%Y%m%d_%H%M%S}.log", fmt::localtime(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()))), wolv::io::File::Mode::Create);
|
|
|
|
s_loggerFile.disableBuffering();
|
2022-01-17 20:06:00 +01:00
|
|
|
|
2023-06-18 22:32:55 +02:00
|
|
|
if (s_loggerFile.isValid()) break;
|
2022-01-17 20:06:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-23 23:37:47 +02:00
|
|
|
|
|
|
|
std::vector<LogEntry>& getLogEntries() {
|
|
|
|
static std::vector<LogEntry> logEntries;
|
|
|
|
return logEntries;
|
|
|
|
}
|
|
|
|
|
2023-07-23 23:39:00 +02:00
|
|
|
void assertionHandler(bool expr, const char* expr_str, const char* file, int line) {
|
|
|
|
if (!expr) {
|
|
|
|
log::error("Assertion failed: {} at {}:{}", expr_str, file, line);
|
|
|
|
|
|
|
|
#if defined (DEBUG)
|
|
|
|
std::abort();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
}
|