Compare commits

...

2 Commits

Author SHA1 Message Date
Nicholas Dudfield
9753692a9a fix(logs): respect NO_COLOR for partition names
The partition name colors (e.g., Application:, NetworkOPs:) were
ignoring the NO_COLOR environment variable and always applying
color codes. Now both partition names and location suffixes
properly respect should_log_use_colors() which honors NO_COLOR
and terminal detection.
2025-10-27 11:17:08 +07:00
Nicholas Dudfield
70942e5882 fix(logs): preserve trailing newlines in location suffixes
Fixes jagged formatting where location info appeared with leading
spaces after blank lines in multiline log messages. Location suffix
now appears immediately after content with trailing newlines preserved.

Also optimizes the hot logging path:
- Single backward scan with find_last_not_of
- Exact pre-allocation to avoid reallocations
- Direct string operations instead of ostringstream
- Move semantics for final assignment
2025-10-24 11:34:15 +07:00
2 changed files with 40 additions and 9 deletions

View File

@@ -360,7 +360,8 @@ Logs::format(
if (!partition.empty())
{
#ifdef BEAST_ENHANCED_LOGGING
output += beast::detail::get_log_highlight_color();
if (beast::detail::should_log_use_colors())
output += beast::detail::get_log_highlight_color();
#endif
output += partition + ":";
}
@@ -392,7 +393,8 @@ Logs::format(
}
#ifdef BEAST_ENHANCED_LOGGING
output += "\033[0m";
if (beast::detail::should_log_use_colors())
output += "\033[0m";
#endif
output += message;

View File

@@ -155,14 +155,43 @@ Journal::ScopedStream::~ScopedStream()
#ifdef BEAST_ENHANCED_LOGGING
// Add suffix if location is enabled
if (file_ && detail::should_show_location() && !s.empty() && s != "\n")
if (file_ && detail::should_show_location() && !s.empty())
{
std::ostringstream combined;
combined << s;
if (!s.empty() && s.back() != ' ')
combined << " ";
detail::log_write_location_string(combined, file_, line_);
s = combined.str();
// Single optimized scan from the end
size_t const lastNonWhitespace = s.find_last_not_of(" \n\r\t");
// Skip if message is only whitespace (e.g., just "\n" or " \n\n")
if (lastNonWhitespace != std::string::npos)
{
// Count only the trailing newlines (tiny range)
size_t trailingNewlines = 0;
for (size_t i = lastNonWhitespace + 1; i < s.length(); ++i)
{
if (s[i] == '\n')
++trailingNewlines;
}
// Build location string once
std::ostringstream locStream;
detail::log_write_location_string(locStream, file_, line_);
std::string const location = locStream.str();
// Pre-allocate exact size → zero reallocations
size_t const finalSize = lastNonWhitespace + 1 + 1 +
location.length() + trailingNewlines;
std::string result;
result.reserve(finalSize);
// Direct string ops (no ostringstream overhead)
result.append(s, 0, lastNonWhitespace + 1);
result.push_back(' ');
result += location;
if (trailingNewlines > 0)
result.append(trailingNewlines, '\n');
s = std::move(result); // Move, no copy
}
}
#endif