1
0
mirror of synced 2024-12-03 03:37:19 +01:00
ImHex/lib/libimhex/source/helpers/logger.cpp

39 lines
1.0 KiB
C++
Raw Normal View History

#include <hex/helpers/logger.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/fmt.hpp>
#include <wolv/io/file.hpp>
2023-06-18 22:32:55 +02:00
namespace hex::log::impl {
2023-06-18 22:32:55 +02:00
static wolv::io::File s_loggerFile;
std::mutex s_loggerMutex;
FILE *getDestination() {
2023-06-18 22:32:55 +02:00
if (s_loggerFile.isValid())
return s_loggerFile.getHandle();
else
return stdout;
}
wolv::io::File& getFile() {
2023-06-18 22:32:55 +02:00
return s_loggerFile;
}
bool isRedirected() {
2023-06-18 22:32:55 +02:00
return s_loggerFile.isValid();
}
void redirectToFile() {
2023-06-18 22:32:55 +02:00
if (s_loggerFile.isValid()) return;
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs, true)) {
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();
2023-06-18 22:32:55 +02:00
if (s_loggerFile.isValid()) break;
}
}
}