Skip `RawLog` buffer allocation and formatting when low-level write is unsupported. This removes unnecessary allocation when it isn't supported in the first place. PiperOrigin-RevId: 1015802782
diff --git a/absl/base/internal/raw_logging.cc b/absl/base/internal/raw_logging.cc index f705a38..c076a14 100644 --- a/absl/base/internal/raw_logging.cc +++ b/absl/base/internal/raw_logging.cc
@@ -107,7 +107,6 @@ *buf += n; return result; } -#endif // ABSL_LOW_LEVEL_WRITE_SUPPORTED constexpr int kLogBufSize = 3000; @@ -136,6 +135,16 @@ DoRawLog(buf, buf_size, "[%s : %d] RAW: ", file, line); return true; } +#else // !ABSL_LOW_LEVEL_WRITE_SUPPORTED +// Nothing is formatted or written; the buffer only holds the NUL terminator +// the abort hook expects. +constexpr int kLogBufSize = 1; + +bool DefaultLogFilterAndPrefix(absl::LogSeverity, const char*, int, char**, + int*) { + return true; +} +#endif // ABSL_LOW_LEVEL_WRITE_SUPPORTED ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES absl::base_internal::AtomicHook<LogFilterAndPrefixHook> @@ -151,6 +160,7 @@ char buffer[kLogBufSize]; char* buf = buffer; int size = sizeof(buffer); + buffer[0] = '\0'; #ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED bool enabled = true; #else @@ -164,7 +174,9 @@ } #endif - enabled = log_filter_and_prefix_hook(severity, file, line, &buf, &size); + if (enabled) { + enabled = log_filter_and_prefix_hook(severity, file, line, &buf, &size); + } const char* const prefix_end = buf; #ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED @@ -180,7 +192,6 @@ #else static_cast<void>(format); static_cast<void>(ap); - static_cast<void>(enabled); #endif // Abort the process after logging a FATAL message, even if the output itself
diff --git a/absl/base/internal/raw_logging.h b/absl/base/internal/raw_logging.h index 9cc270b..19aef0b 100644 --- a/absl/base/internal/raw_logging.h +++ b/absl/base/internal/raw_logging.h
@@ -156,14 +156,18 @@ // Function type for a raw_log customization hook for suppressing messages // by severity, and for writing custom prefixes on non-suppressed messages. // -// The installed hook is called for every raw log invocation. The message will -// be logged to stderr only if the hook returns true. FATAL errors will cause -// the process to abort, even if writing to stderr is suppressed. The hook is -// also provided with an output buffer, where it can write a custom log message -// prefix. +// The installed hook is called for every raw log invocation when logging is +// enabled (for example, it is skipped when low-level stderr writes are +// unsupported or when the message is suppressed by ABSL_MIN_LOG_LEVEL). The +// message will be logged to stderr only if the hook returns true. FATAL +// errors will cause the process to abort, even if writing to stderr is +// suppressed. The hook is also provided with an output buffer, where it can +// write a custom log message prefix. // // The raw_log system does not allocate memory or grab locks. User-provided -// hooks must avoid these operations, and must not throw exceptions. +// hooks must avoid these operations, must not throw exceptions, and must not +// have side effects other than writing to 'buf' (and updating '*buf' and +// '*buf_size'). // // 'severity' is the severity level of the message being written. // 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro