From 662935c35d49a69bc4529362b93851ae7814cd21 Mon Sep 17 00:00:00 2001 From: PixelyIon Date: Fri, 12 Nov 2021 18:30:10 +0530 Subject: [PATCH] Attempt Flushing `Logger` During Fatal Signals Any signals that lead to exception handling being triggered now attempt to flush all logs given that the log mutex is unoccupied, this is to mostly help logs be more complete when exiting isn't graceful. --- app/src/main/cpp/skyline/common/logger.cpp | 10 +++++++++- app/src/main/cpp/skyline/common/logger.h | 2 ++ app/src/main/cpp/skyline/common/signal.cpp | 7 ++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/src/main/cpp/skyline/common/logger.cpp b/app/src/main/cpp/skyline/common/logger.cpp index bb02e189..5bf969dd 100644 --- a/app/src/main/cpp/skyline/common/logger.cpp +++ b/app/src/main/cpp/skyline/common/logger.cpp @@ -12,10 +12,18 @@ namespace skyline { } void Logger::LoggerContext::Finalize() { + std::lock_guard lock(mutex); logFile.close(); } + void Logger::LoggerContext::TryFlush() { + std::unique_lock lock(mutex, std::try_to_lock); + if (lock) + logFile.flush(); + } + void Logger::LoggerContext::Flush() { + std::lock_guard lock(mutex); logFile.flush(); } @@ -60,4 +68,4 @@ namespace skyline { std::lock_guard guard(mutex); logFile << str; } -} \ No newline at end of file +} diff --git a/app/src/main/cpp/skyline/common/logger.h b/app/src/main/cpp/skyline/common/logger.h index c831fd0c..aadfc892 100644 --- a/app/src/main/cpp/skyline/common/logger.h +++ b/app/src/main/cpp/skyline/common/logger.h @@ -40,6 +40,8 @@ namespace skyline { void Finalize(); + void TryFlush(); + void Flush(); void Write(const std::string &str); diff --git a/app/src/main/cpp/skyline/common/signal.cpp b/app/src/main/cpp/skyline/common/signal.cpp index 52ed4769..d5bccc36 100644 --- a/app/src/main/cpp/skyline/common/signal.cpp +++ b/app/src/main/cpp/skyline/common/signal.cpp @@ -56,14 +56,17 @@ namespace skyline::signal { frame = SafeFrameRecurse(2, lookupFrame); hasAdvanced = true; } else { + Logger::EmulationContext.TryFlush(); terminateHandler(); // We presumably have no exception handlers left on the stack to consume the exception, it's time to quit } } lookupFrame = lookupFrame->next; } - if (!frame->next) + if (!frame->next) { + Logger::EmulationContext.TryFlush(); // We want to attempt to flush all logs before quitting terminateHandler(); // We don't know the frame's stack boundaries, the only option is to quit + } asm("MOV SP, %x0\n\t" // Stack frame is the first item on a function's stack, it's used to calculate calling function's stack pointer "MOV LR, %x1\n\t" @@ -99,6 +102,8 @@ namespace skyline::signal { terminateHandler = handler; std::set_terminate(TerminateHandler); } + + Logger::EmulationContext.TryFlush(); // We want to attempt to flush all logs in case exception handling fails and infloops } template