From b57730c28bb0af5af1c7a2930fea9ec45ddb1b6a Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 1 Mar 2022 00:03:28 +0100 Subject: [PATCH] sys: Disable buffering on log files --- lib/libimhex/include/hex/helpers/file.hpp | 2 ++ lib/libimhex/source/helpers/file.cpp | 16 +++++++++++----- lib/libimhex/source/helpers/logger.cpp | 1 + 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/file.hpp b/lib/libimhex/include/hex/helpers/file.hpp index fe4bcc47d..df0a623df 100644 --- a/lib/libimhex/include/hex/helpers/file.hpp +++ b/lib/libimhex/include/hex/helpers/file.hpp @@ -61,6 +61,8 @@ namespace hex { auto getHandle() { return this->m_file; } const fs::path &getPath() { return this->m_path; } + void disableBuffering(); + private: FILE *m_file; fs::path m_path; diff --git a/lib/libimhex/source/helpers/file.cpp b/lib/libimhex/source/helpers/file.cpp index 7fcf530dd..79230fc07 100644 --- a/lib/libimhex/source/helpers/file.cpp +++ b/lib/libimhex/source/helpers/file.cpp @@ -42,7 +42,7 @@ namespace hex { void File::close() { if (isValid()) { - fclose(this->m_file); + std::fclose(this->m_file); this->m_file = nullptr; } } @@ -83,19 +83,19 @@ namespace hex { void File::write(const u8 *buffer, size_t size) { if (!isValid()) return; - fwrite(buffer, size, 1, this->m_file); + std::fwrite(buffer, size, 1, this->m_file); } void File::write(const std::vector &bytes) { if (!isValid()) return; - fwrite(bytes.data(), 1, bytes.size(), this->m_file); + std::fwrite(bytes.data(), 1, bytes.size(), this->m_file); } void File::write(const std::string &string) { if (!isValid()) return; - fwrite(string.data(), string.size(), 1, this->m_file); + std::fwrite(string.data(), string.size(), 1, this->m_file); } size_t File::getSize() const { @@ -119,7 +119,7 @@ namespace hex { } void File::flush() { - fflush(this->m_file); + std::fflush(this->m_file); } bool File::remove() { @@ -127,4 +127,10 @@ namespace hex { return std::remove(this->m_path.string().c_str()) == 0; } + void File::disableBuffering() { + if (!isValid()) return; + + std::setvbuf(this->m_file, nullptr, _IONBF, 0); + } + } \ No newline at end of file diff --git a/lib/libimhex/source/helpers/logger.cpp b/lib/libimhex/source/helpers/logger.cpp index c38005b82..db6515bf5 100644 --- a/lib/libimhex/source/helpers/logger.cpp +++ b/lib/libimhex/source/helpers/logger.cpp @@ -26,6 +26,7 @@ namespace hex::log { for (const auto &path : hex::getPath(ImHexPath::Logs, true)) { fs::create_directories(path); g_loggerFile = File(path / hex::format("{0:%Y%m%d_%H%M%S}.log", fmt::localtime(std::chrono::system_clock::now())), File::Mode::Create); + g_loggerFile.disableBuffering(); if (g_loggerFile.isValid()) break; }