[Base] Shutdown the logger to prevent truncation of output
This commit is contained in:
parent
157c7780af
commit
4fd03e8029
|
@ -227,8 +227,8 @@ class Logger {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
volatile size_t write_tail_ = 0;
|
||||||
size_t write_head_ = 0;
|
size_t write_head_ = 0;
|
||||||
size_t write_tail_ = 0;
|
|
||||||
size_t read_head_ = 0;
|
size_t read_head_ = 0;
|
||||||
uint8_t buffer_[kBufferSize];
|
uint8_t buffer_[kBufferSize];
|
||||||
FILE* file_ = nullptr;
|
FILE* file_ = nullptr;
|
||||||
|
@ -238,13 +238,24 @@ class Logger {
|
||||||
};
|
};
|
||||||
|
|
||||||
void InitializeLogging(const std::wstring& app_name) {
|
void InitializeLogging(const std::wstring& app_name) {
|
||||||
// We leak this intentionally - lots of cleanup code needs it.
|
|
||||||
auto mem = memory::AlignedAlloc<Logger>(0x10);
|
auto mem = memory::AlignedAlloc<Logger>(0x10);
|
||||||
logger_ = new (mem) Logger(app_name);
|
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,
|
void LogLineFormat(LogLevel log_level, const char prefix_char, const char* fmt,
|
||||||
...) {
|
...) {
|
||||||
|
if (!logger_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
int chars_written = vsnprintf(log_format_buffer_.data(),
|
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,
|
void LogLineVarargs(LogLevel log_level, const char prefix_char, const char* fmt,
|
||||||
va_list args) {
|
va_list args) {
|
||||||
|
if (!logger_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int chars_written = vsnprintf(log_format_buffer_.data(),
|
int chars_written = vsnprintf(log_format_buffer_.data(),
|
||||||
log_format_buffer_.capacity(), fmt, args);
|
log_format_buffer_.capacity(), fmt, args);
|
||||||
if (chars_written < 0) {
|
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,
|
void LogLine(LogLevel log_level, const char prefix_char, const char* str,
|
||||||
size_t str_length) {
|
size_t str_length) {
|
||||||
|
if (!logger_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
logger_->AppendLine(
|
logger_->AppendLine(
|
||||||
xe::threading::current_thread_id(), log_level, prefix_char, str,
|
xe::threading::current_thread_id(), log_level, prefix_char, str,
|
||||||
str_length == std::string::npos ? std::strlen(str) : str_length);
|
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,
|
void LogLine(LogLevel log_level, const char prefix_char,
|
||||||
const std::string& str) {
|
const std::string& str) {
|
||||||
|
if (!logger_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
logger_->AppendLine(xe::threading::current_thread_id(), log_level,
|
logger_->AppendLine(xe::threading::current_thread_id(), log_level,
|
||||||
prefix_char, str.c_str(), str.length());
|
prefix_char, str.c_str(), str.length());
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ enum class LogLevel {
|
||||||
// Initializes the logging system and any outputs requested.
|
// Initializes the logging system and any outputs requested.
|
||||||
// Must be called on startup.
|
// Must be called on startup.
|
||||||
void InitializeLogging(const std::wstring& app_name);
|
void InitializeLogging(const std::wstring& app_name);
|
||||||
|
void ShutdownLogging();
|
||||||
|
|
||||||
// Appends a line to the log with printf-style formatting.
|
// Appends a line to the log with printf-style formatting.
|
||||||
void LogLineFormat(LogLevel log_level, const char prefix_char, const char* fmt,
|
void LogLineFormat(LogLevel log_level, const char prefix_char, const char* fmt,
|
||||||
|
|
|
@ -104,6 +104,7 @@ int Main() {
|
||||||
// Call app-provided entry point.
|
// Call app-provided entry point.
|
||||||
int result = entry_info.entry_point(args);
|
int result = entry_info.entry_point(args);
|
||||||
|
|
||||||
|
xe::ShutdownLogging();
|
||||||
google::ShutDownCommandLineFlags();
|
google::ShutDownCommandLineFlags();
|
||||||
LocalFree(argv);
|
LocalFree(argv);
|
||||||
return result;
|
return result;
|
||||||
|
|
Loading…
Reference in New Issue