Skip to main content

Format & Output (String Formatting and Output)

String formatting and output are high-frequency basic capabilities in the project, mainly used in scenarios such as log printing, console information output, data visualization, and business string concatenation. This module focuses on high-performance and extensible formatting solutions, which have significant performance advantages over the standard library std::stringstream/std::printf, and also support extended capabilities such as color/stylized output and customized encapsulation for log scenarios.

Core Capabilities & Selection Recommendations

Capability/LibraryCore DescriptionSelection Advice
turbo/strings/str_formatC-like formatting interface with syntax close to printf, aligned with the project's core tech stack, no additional dependenciesFirst Choice (General Formatting Scenarios):Use for all regular string formatting needs; the interface is simple and performs better than std/abseil
turbo/strings/string_builderStream-based string builder, comparable to std::stringstream, with about 50% performance improvement and no redundant memory allocationFirst Choice (High-Frequency Concatenation/Large String Construction):Use for log concatenation, batch data formatting, etc., to replace std::stringstream
abseil/strings/formatBasic formatting capabilities, indirectly introduced in the project only due to protobuf dependencies, no active integration or usage requirementsNot recommended for active use: Only compatible with legacy code; use turbo solutions for all new scenarios
fmtlib/fmtFull-featured third-party formatting library with rich syntax extensionsSecond Choice: Use only when turbo interfaces cannot meet special formatting needs (e.g., complex custom type formatting)
ftxuiIndependent terminal UI library that supports stylized output capabilities such as terminal text color, layout, and interactive interfaces; can be combined with fmt to achieve integrated formatting + style outputScenario-based Recommendation: Use ftxui for console visualization and interactive output scenarios, and match with fmt for formatting logic
xlogTerminal log formatting output encapsulated with fmt, adapted to the project's log systemExclusive Scenario: Must use xlog for log output instead of directly calling fmt/turbo formatting

Key Supplementary Notes

  1. Performance Priority: turbo::string_builder > turbo::str_format > fmtlib/fmt > abseil::format > std standard library;
  2. Stylized Output: For terminal color/layout requirements, prioritize using the independent library ftxui, and match with fmt to achieve integrated "content formatting + style rendering" instead of implementing ANSI escape characters yourself to reduce compatibility issues;
  3. Mandatory Log Scenario Specification: All log output must use xlog encapsulated interfaces; direct calls to formatting libraries for output are prohibited to ensure unified log formats.

Integration Guide