Skip to content

Commit c36e8c4

Browse files
authored
Preserve the previous session log across restarts (#1846)
re2_framework_log.txt was opened with truncate=true, so the previous session's log was destroyed as soon as the framework was loaded. After a crash this made it impossible to inspect the log of the session that crashed: restarting the game, which is the first thing anyone does, had already overwritten it. Rename the existing log to re2_framework_log.prev.txt before creating the logger, so the previous session survives a restart. All filesystem calls use the std::error_code overloads, so a failure (e.g. a locked file) quietly falls back to the old behaviour instead of blocking startup. At most one previous session is kept: an existing .prev.txt is removed before the rename.
1 parent d7d59ad commit c36e8c4

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

‎src/REFramework.cpp‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,30 @@ try {
256256

257257
REFramework::REFramework(HMODULE reframework_module)
258258
: m_game_module{GetModuleHandle(0)}
259-
, m_logger{spdlog::basic_logger_mt("REFramework", (get_persistent_dir("re2_framework_log.txt")).string(), true)}
260259
{
261260

261+
// Preserve the previous session's log instead of overwriting it, so it can still be
262+
// inspected after a crash or a restart (re2_framework_log.txt -> re2_framework_log.prev.txt).
263+
const auto log_path = get_persistent_dir("re2_framework_log.txt");
264+
bool log_rotated = false;
265+
std::string log_prev_path;
266+
267+
{
268+
std::error_code ec;
269+
270+
if (std::filesystem::exists(log_path, ec)) {
271+
auto prev = log_path.parent_path() / (log_path.stem().string() + ".prev" + log_path.extension().string());
272+
273+
std::filesystem::remove(prev, ec);
274+
std::filesystem::rename(log_path, prev, ec);
275+
276+
log_rotated = !ec;
277+
log_prev_path = prev.string();
278+
}
279+
}
280+
281+
m_logger = spdlog::basic_logger_mt("REFramework", log_path.string(), true);
282+
262283
s_reframework_module = reframework_module;
263284
m_dist_sink = std::make_shared<spdlog::sinks::dist_sink_mt>();
264285

@@ -270,6 +291,10 @@ REFramework::REFramework(HMODULE reframework_module)
270291
spdlog::set_default_logger(m_logger);
271292
spdlog::flush_on(spdlog::level::info);
272293

294+
if (log_rotated) {
295+
spdlog::info("Previous log preserved as {}", log_prev_path);
296+
}
297+
273298
if (s_fallback_appdata) {
274299
spdlog::warn("Failed to write to current directory, falling back to appdata folder");
275300
}

0 commit comments

Comments
 (0)