1
0
mirror of synced 2024-12-01 02:37:18 +01:00

fix: Logging race conditions

This commit is contained in:
WerWolv 2023-06-18 22:32:55 +02:00
parent 25154dd450
commit 54061c56bf
6 changed files with 30 additions and 26 deletions

View File

@ -13,9 +13,16 @@
namespace hex::log {
FILE *getDestination();
wolv::io::File& getFile();
bool isRedirected();
namespace impl {
FILE *getDestination();
wolv::io::File& getFile();
bool isRedirected();
[[maybe_unused]] void redirectToFile();
extern std::mutex s_loggerMutex;
}
namespace {
@ -24,7 +31,7 @@ namespace hex::log {
fmt::print(dest, "[{0:%H:%M:%S}] ", now);
if (isRedirected())
if (impl::isRedirected())
fmt::print(dest, "{0} ", level);
else
fmt::print(dest, ts, "{0} ", level);
@ -34,11 +41,9 @@ namespace hex::log {
template<typename... T>
[[maybe_unused]] void print(const fmt::text_style &ts, const std::string &level, const std::string &fmt, auto... args) {
auto dest = getDestination();
static std::mutex loggerMutex;
std::scoped_lock lock(loggerMutex);
std::scoped_lock lock(impl::s_loggerMutex);
auto dest = impl::getDestination();
printPrefix(dest, ts, level);
fmt::print(dest, fmt::runtime(fmt), args...);
fmt::print(dest, "\n");
@ -70,6 +75,4 @@ namespace hex::log {
hex::log::print(fg(fmt::color::purple) | fmt::emphasis::bold, "[FATAL]", fmt, args...);
}
[[maybe_unused]] void redirectToFile();
}

View File

@ -4,34 +4,35 @@
#include <wolv/io/file.hpp>
namespace hex::log {
namespace hex::log::impl {
static wolv::io::File g_loggerFile;
static wolv::io::File s_loggerFile;
std::mutex s_loggerMutex;
FILE *getDestination() {
if (g_loggerFile.isValid())
return g_loggerFile.getHandle();
if (s_loggerFile.isValid())
return s_loggerFile.getHandle();
else
return stdout;
}
wolv::io::File& getFile() {
return g_loggerFile;
return s_loggerFile;
}
bool isRedirected() {
return g_loggerFile.isValid();
return s_loggerFile.isValid();
}
void redirectToFile() {
if (g_loggerFile.isValid()) return;
if (s_loggerFile.isValid()) return;
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs, true)) {
wolv::io::fs::createDirectories(path);
g_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);
g_loggerFile.disableBuffering();
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();
if (g_loggerFile.isValid()) break;
if (s_loggerFile.isValid()) break;
}
}

View File

@ -34,7 +34,7 @@ namespace hex::crash {
log::fatal(message);
nlohmann::json crashData{
{"logFile", wolv::util::toUTF8String(hex::log::getFile().getPath())},
{"logFile", wolv::util::toUTF8String(hex::log::impl::getFile().getPath())},
{"project", wolv::util::toUTF8String(ProjectFile::getPath())},
};
@ -83,7 +83,7 @@ namespace hex::crash {
#if defined(DEBUG)
assert(!"Debug build, triggering breakpoint");
#else
std::raise(signalNumber);
std::exit(signalNumber);
#endif
}
@ -125,7 +125,7 @@ namespace hex::crash {
#if defined(DEBUG)
assert(!"Debug build, triggering breakpoint");
#else
std::terminate();
std::exit(100);
#endif
}
});

View File

@ -74,7 +74,7 @@ namespace hex {
// Redirect stdout to log file if we're not running in a terminal
if (!isatty(STDOUT_FILENO)) {
log::redirectToFile();
log::impl::redirectToFile();
}
}

View File

@ -36,7 +36,7 @@ namespace hex {
// Redirect stdout to log file if we're not running in a terminal
if (!isatty(STDOUT_FILENO)) {
log::redirectToFile();
log::impl::redirectToFile();
}
}

View File

@ -218,7 +218,7 @@ namespace hex {
}
}
} else {
log::redirectToFile();
log::impl::redirectToFile();
}
// Open new files in already existing ImHex instance