diff --git a/src/xenia/base/logging.cc b/src/xenia/base/logging.cc index bb2af1121..3ad17d0b6 100644 --- a/src/xenia/base/logging.cc +++ b/src/xenia/base/logging.cc @@ -227,8 +227,8 @@ class Logger { } } + volatile size_t write_tail_ = 0; size_t write_head_ = 0; - size_t write_tail_ = 0; size_t read_head_ = 0; uint8_t buffer_[kBufferSize]; FILE* file_ = nullptr; @@ -238,13 +238,24 @@ class Logger { }; void InitializeLogging(const std::wstring& app_name) { - // We leak this intentionally - lots of cleanup code needs it. auto mem = memory::AlignedAlloc(0x10); logger_ = new (mem) Logger(app_name); } +void xe::ShutdownLogging() { + Logger* logger = logger_; + logger_ = nullptr; + + logger->~Logger(); + memory::AlignedFree(logger); +} + void LogLineFormat(LogLevel log_level, const char prefix_char, const char* fmt, ...) { + if (!logger_) { + return; + } + va_list args; va_start(args, fmt); int chars_written = vsnprintf(log_format_buffer_.data(), @@ -261,6 +272,10 @@ void LogLineFormat(LogLevel log_level, const char prefix_char, const char* fmt, void LogLineVarargs(LogLevel log_level, const char prefix_char, const char* fmt, va_list args) { + if (!logger_) { + return; + } + int chars_written = vsnprintf(log_format_buffer_.data(), log_format_buffer_.capacity(), fmt, args); if (chars_written < 0) { @@ -275,6 +290,10 @@ void LogLineVarargs(LogLevel log_level, const char prefix_char, const char* fmt, void LogLine(LogLevel log_level, const char prefix_char, const char* str, size_t str_length) { + if (!logger_) { + return; + } + logger_->AppendLine( xe::threading::current_thread_id(), log_level, prefix_char, str, str_length == std::string::npos ? std::strlen(str) : str_length); @@ -282,6 +301,10 @@ void LogLine(LogLevel log_level, const char prefix_char, const char* str, void LogLine(LogLevel log_level, const char prefix_char, const std::string& str) { + if (!logger_) { + return; + } + logger_->AppendLine(xe::threading::current_thread_id(), log_level, prefix_char, str.c_str(), str.length()); } diff --git a/src/xenia/base/logging.h b/src/xenia/base/logging.h index 07ac086e9..2a2d67284 100644 --- a/src/xenia/base/logging.h +++ b/src/xenia/base/logging.h @@ -29,6 +29,7 @@ enum class LogLevel { // Initializes the logging system and any outputs requested. // Must be called on startup. void InitializeLogging(const std::wstring& app_name); +void ShutdownLogging(); // Appends a line to the log with printf-style formatting. void LogLineFormat(LogLevel log_level, const char prefix_char, const char* fmt, diff --git a/src/xenia/base/main_win.cc b/src/xenia/base/main_win.cc index 76b804265..2bf1a2033 100644 --- a/src/xenia/base/main_win.cc +++ b/src/xenia/base/main_win.cc @@ -104,6 +104,7 @@ int Main() { // Call app-provided entry point. int result = entry_info.entry_point(args); + xe::ShutdownLogging(); google::ShutDownCommandLineFlags(); LocalFree(argv); return result;