diff options
| author | yum <yum.food.vr@gmail.com> | 2023-09-09 20:29:06 -0700 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2023-09-09 20:29:06 -0700 |
| commit | 80714a0295df71d6dee35182d221648680f5f7d6 (patch) | |
| tree | 102c375f1670152580fb0834baac49c81cf5ba00 | |
| parent | 59a1d34285548ca673fe551df36a71f3f8421418 (diff) | |
Constrain UI text buffers to 1000 lines
This keeps memory usage from growing without bound.
| -rw-r--r-- | GUI/GUI/GUI/Logging.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/GUI/GUI/GUI/Logging.cpp b/GUI/GUI/GUI/Logging.cpp index b5b9e55..6b42bd6 100644 --- a/GUI/GUI/GUI/Logging.cpp +++ b/GUI/GUI/GUI/Logging.cpp @@ -7,10 +7,12 @@ #endif
#include <wx/process.h>
+#include <wx/tokenzr.h>
#include <wx/txtstrm.h>
#include <fstream>
#include <regex>
+#include <sstream>
#include <string>
Logging::ThreadLogger Logging::kThreadLogger = Logging::ThreadLogger();
@@ -43,7 +45,29 @@ void Logging::ThreadLogger::Drain() }
log_ofs << message;
}
+
+ // Constrain wxTextCtrl's to 1000 lines to keep memory 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;
+
+ // Remove lines from the beginning
+ lines.RemoveAt(0, linesToRemove);
+
+ // Join the lines back into a single string
+ wxString newText = wxJoin(lines, '\n');
+
+ // Update the text in the wxTextCtrl
+ frame->Clear();
+ frame->AppendText(newText);
+ }
+ }
}
+
log_ofs.close();
messages_.clear();
}
|
