From 286dcae5e087db817f3350cf442145107b25bc9c Mon Sep 17 00:00:00 2001 From: yum Date: Sat, 9 Sep 2023 21:41:36 -0700 Subject: Constrain log file, UI text field, and transcript sizes Log file is constrained to 1 MB and UI to 100-200 lines. 1k lines is too high to keep the UI from lagging. Transcript is constrained to 4k characters. Also put a 5 ms sleep in the transcription hot path. --- GUI/GUI/GUI/Logging.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'GUI') diff --git a/GUI/GUI/GUI/Logging.cpp b/GUI/GUI/GUI/Logging.cpp index 6b42bd6..5d0e23e 100644 --- a/GUI/GUI/GUI/Logging.cpp +++ b/GUI/GUI/GUI/Logging.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -34,7 +35,8 @@ void Logging::ThreadLogger::Append(wxTextCtrl* frame, const std::string&& messag void Logging::ThreadLogger::Drain() { std::scoped_lock l(mu_); - std::ofstream log_ofs("Resources/log.txt", std::ios_base::app); + const std::filesystem::path log_path("Resources/log.txt"); + std::ofstream log_ofs(log_path, std::ios_base::app); for (const auto& [frame, messages] : messages_) { for (const auto& message : messages) { if (frame) { @@ -46,14 +48,15 @@ void Logging::ThreadLogger::Drain() log_ofs << message; } - // Constrain wxTextCtrl's to 1000 lines to keep memory in check. + // Constrain wxTextCtrl's to 100-200 lines to keep memory usage / + // general snappiness in check. if (frame) { wxString allText = frame->GetValue(); wxArrayString lines = wxStringTokenize(allText, "\n"); size_t count = lines.GetCount(); - if (count > 2000) { - // Keep only the last 1000 lines. - size_t linesToRemove = count - 1000; + if (count > 200) { + // Keep only the last 100 lines. + size_t linesToRemove = count - 100; // Remove lines from the beginning lines.RemoveAt(0, linesToRemove); @@ -67,9 +70,22 @@ void Logging::ThreadLogger::Drain() } } } - log_ofs.close(); messages_.clear(); + + // Drop first 50% of lines in file if larger than 1 MB. + if (std::filesystem::file_size(log_path) > 1024 * 1024) { + std::vector lines; + std::ifstream log_ifs(log_path); + std::string line; + while (std::getline(log_ifs, line)) { + lines.push_back(std::move(line)); + } + log_ofs = std::ofstream(log_path); + for (int i = lines.size() / 2; i < lines.size(); i++) { + log_ofs << lines[i]; + } + } } std::string Logging::HidePII(const std::string&& str, -- cgit v1.2.3