Logger: Properly handle cases where strlen(formatted string) > capacity of log_format_buffer_ (as well as error cases)

This commit is contained in:
Dr. Chat 2017-02-25 20:03:25 -06:00
parent ed56f7e96b
commit 2bd19478a4
1 changed files with 13 additions and 7 deletions

View File

@ -231,23 +231,29 @@ void InitializeLogging(const std::wstring& app_name) {
void LogLineFormat(const char level_char, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
size_t chars_written = vsnprintf(log_format_buffer_.data(),
log_format_buffer_.capacity(), fmt, args);
int chars_written = vsnprintf(log_format_buffer_.data(),
log_format_buffer_.capacity(), fmt, args);
va_end(args);
if (chars_written != std::string::npos) {
if (chars_written >= 0 && chars_written < log_format_buffer_.capacity()) {
logger_->AppendLine(xe::threading::current_thread_id(), level_char,
log_format_buffer_.data(), chars_written);
} else {
} else if (chars_written >= 0) {
logger_->AppendLine(xe::threading::current_thread_id(), level_char, fmt,
std::strlen(fmt));
}
}
void LogLineVarargs(const char level_char, const char* fmt, va_list args) {
size_t chars_written = vsnprintf(log_format_buffer_.data(),
log_format_buffer_.capacity(), fmt, args);
int chars_written = vsnprintf(log_format_buffer_.data(),
log_format_buffer_.capacity(), fmt, args);
if (chars_written < 0) {
return;
}
auto size =
std::min(size_t(chars_written), log_format_buffer_.capacity() - 1);
logger_->AppendLine(xe::threading::current_thread_id(), level_char,
log_format_buffer_.data(), chars_written);
log_format_buffer_.data(), size);
}
void LogLine(const char level_char, const char* str, size_t str_length) {