assert/logging: Stop the logging thread and flush the backends before crashing (#7146)

This commit is contained in:
SachinVin 2023-11-12 01:25:42 +05:30 committed by GitHub
parent 222b1cc0d7
commit ceeda05798
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View file

@ -6,6 +6,7 @@
#include <cstdlib> #include <cstdlib>
#include "common/common_funcs.h" #include "common/common_funcs.h"
#include "common/logging/backend.h"
#include "common/logging/log.h" #include "common/logging/log.h"
// For asserts we'd like to keep all the junk executed when an assert happens away from the // For asserts we'd like to keep all the junk executed when an assert happens away from the
@ -17,6 +18,7 @@
if (!(_a_)) [[unlikely]] { \ if (!(_a_)) [[unlikely]] { \
[]() CITRA_NO_INLINE CITRA_NO_RETURN { \ []() CITRA_NO_INLINE CITRA_NO_RETURN { \
LOG_CRITICAL(Debug, "Assertion Failed!"); \ LOG_CRITICAL(Debug, "Assertion Failed!"); \
Common::Log::Stop(); \
Crash(); \ Crash(); \
exit(1); \ exit(1); \
}(); \ }(); \
@ -28,6 +30,7 @@
if (!(_a_)) [[unlikely]] { \ if (!(_a_)) [[unlikely]] { \
[&]() CITRA_NO_INLINE CITRA_NO_RETURN { \ [&]() CITRA_NO_INLINE CITRA_NO_RETURN { \
LOG_CRITICAL(Debug, "Assertion Failed!\n" __VA_ARGS__); \ LOG_CRITICAL(Debug, "Assertion Failed!\n" __VA_ARGS__); \
Common::Log::Stop(); \
Crash(); \ Crash(); \
exit(1); \ exit(1); \
}(); \ }(); \
@ -37,6 +40,7 @@
#define UNREACHABLE() \ #define UNREACHABLE() \
([]() CITRA_NO_INLINE CITRA_NO_RETURN { \ ([]() CITRA_NO_INLINE CITRA_NO_RETURN { \
LOG_CRITICAL(Debug, "Unreachable code!"); \ LOG_CRITICAL(Debug, "Unreachable code!"); \
Common::Log::Stop(); \
Crash(); \ Crash(); \
exit(1); \ exit(1); \
}()) }())
@ -44,6 +48,7 @@
#define UNREACHABLE_MSG(...) \ #define UNREACHABLE_MSG(...) \
([&]() CITRA_NO_INLINE CITRA_NO_RETURN { \ ([&]() CITRA_NO_INLINE CITRA_NO_RETURN { \
LOG_CRITICAL(Debug, "Unreachable code!\n" __VA_ARGS__); \ LOG_CRITICAL(Debug, "Unreachable code!\n" __VA_ARGS__); \
Common::Log::Stop(); \
Crash(); \ Crash(); \
exit(1); \ exit(1); \
}()) }())

View file

@ -220,6 +220,10 @@ public:
instance->StartBackendThread(); instance->StartBackendThread();
} }
static void Stop() {
instance->StopBackendThread();
}
Impl(const Impl&) = delete; Impl(const Impl&) = delete;
Impl& operator=(const Impl&) = delete; Impl& operator=(const Impl&) = delete;
@ -330,6 +334,15 @@ private:
}); });
} }
void StopBackendThread() {
backend_thread.request_stop();
if (backend_thread.joinable()) {
backend_thread.join();
}
ForEachBackend([](Backend& backend) { backend.Flush(); });
}
Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr, Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr,
const char* function, std::string&& message) const { const char* function, std::string&& message) const {
using std::chrono::duration_cast; using std::chrono::duration_cast;
@ -421,6 +434,10 @@ void Start() {
Impl::Start(); Impl::Start();
} }
void Stop() {
Impl::Stop();
}
void DisableLoggingInTests() { void DisableLoggingInTests() {
initialization_in_progress_suppress_logging = true; initialization_in_progress_suppress_logging = true;
} }

View file

@ -16,6 +16,9 @@ void Initialize(std::string_view log_file = "");
void Start(); void Start();
/// Explictily stops the logger thread and flushes the buffers
void Stop();
void DisableLoggingInTests(); void DisableLoggingInTests();
/** /**