util/posix_logger.h [70:126]: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - thread_id.c_str()); // The header can be at most 28 characters (10 date + 15 time + // 3 delimiters) plus the thread ID, which should fit comfortably into the // static buffer. assert(buffer_offset <= 28 + kMaxThreadIdSize); static_assert(28 + kMaxThreadIdSize < kStackBufferSize, "stack-allocated buffer may not fit the message header"); assert(buffer_offset < buffer_size); // Print the message into the buffer. std::va_list arguments_copy; va_copy(arguments_copy, arguments); buffer_offset += std::vsnprintf(buffer + buffer_offset, buffer_size - buffer_offset, format, arguments_copy); va_end(arguments_copy); // The code below may append a newline at the end of the buffer, which // requires an extra character. if (buffer_offset >= buffer_size - 1) { // The message did not fit into the buffer. if (iteration == 0) { // Re-run the loop and use a dynamically-allocated buffer. The buffer // will be large enough for the log message, an extra newline and a // null terminator. dynamic_buffer_size = buffer_offset + 2; continue; } // The dynamically-allocated buffer was incorrectly sized. This should // not happen, assuming a correct implementation of std::(v)snprintf. // Fail in tests, recover by truncating the log message in production. assert(false); buffer_offset = buffer_size - 1; } // Add a newline if necessary. if (buffer[buffer_offset - 1] != '\n') { buffer[buffer_offset] = '\n'; ++buffer_offset; } assert(buffer_offset <= buffer_size); std::fwrite(buffer, 1, buffer_offset, fp_); std::fflush(fp_); if (iteration != 0) { delete[] buffer; } break; } } private: std::FILE* const fp_; }; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - util/windows_logger.h [64:120]: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - thread_id.c_str()); // The header can be at most 28 characters (10 date + 15 time + // 3 delimiters) plus the thread ID, which should fit comfortably into the // static buffer. assert(buffer_offset <= 28 + kMaxThreadIdSize); static_assert(28 + kMaxThreadIdSize < kStackBufferSize, "stack-allocated buffer may not fit the message header"); assert(buffer_offset < buffer_size); // Print the message into the buffer. std::va_list arguments_copy; va_copy(arguments_copy, arguments); buffer_offset += std::vsnprintf(buffer + buffer_offset, buffer_size - buffer_offset, format, arguments_copy); va_end(arguments_copy); // The code below may append a newline at the end of the buffer, which // requires an extra character. if (buffer_offset >= buffer_size - 1) { // The message did not fit into the buffer. if (iteration == 0) { // Re-run the loop and use a dynamically-allocated buffer. The buffer // will be large enough for the log message, an extra newline and a // null terminator. dynamic_buffer_size = buffer_offset + 2; continue; } // The dynamically-allocated buffer was incorrectly sized. This should // not happen, assuming a correct implementation of std::(v)snprintf. // Fail in tests, recover by truncating the log message in production. assert(false); buffer_offset = buffer_size - 1; } // Add a newline if necessary. if (buffer[buffer_offset - 1] != '\n') { buffer[buffer_offset] = '\n'; ++buffer_offset; } assert(buffer_offset <= buffer_size); std::fwrite(buffer, 1, buffer_offset, fp_); std::fflush(fp_); if (iteration != 0) { delete[] buffer; } break; } } private: std::FILE* const fp_; }; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -