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/Library | Core Description | Selection Advice |
|---|---|---|
| turbo/strings/str_format | C-like formatting interface with syntax close to printf, aligned with the project's core tech stack, no additional dependencies | First Choice (General Formatting Scenarios):Use for all regular string formatting needs; the interface is simple and performs better than std/abseil |
| turbo/strings/string_builder | Stream-based string builder, comparable to std::stringstream, with about 50% performance improvement and no redundant memory allocation | First Choice (High-Frequency Concatenation/Large String Construction):Use for log concatenation, batch data formatting, etc., to replace std::stringstream |
| abseil/strings/format | Basic formatting capabilities, indirectly introduced in the project only due to protobuf dependencies, no active integration or usage requirements | Not recommended for active use: Only compatible with legacy code; use turbo solutions for all new scenarios |
| fmtlib/fmt | Full-featured third-party formatting library with rich syntax extensions | Second Choice: Use only when turbo interfaces cannot meet special formatting needs (e.g., complex custom type formatting) |
| ftxui | Independent 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 output | Scenario-based Recommendation: Use ftxui for console visualization and interactive output scenarios, and match with fmt for formatting logic |
| xlog | Terminal log formatting output encapsulated with fmt, adapted to the project's log system | Exclusive Scenario: Must use xlog for log output instead of directly calling fmt/turbo formatting |
Key Supplementary Notes
- Performance Priority:
turbo::string_builder>turbo::str_format>fmtlib/fmt>abseil::format>stdstandard library; - 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;
- 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
- Integrate turbo library: Refer to integration/turbo
- Integrate fmtlib library: Refer to integration/fmt
- Integrate ftxui library: Refer to integration/ftxui
- Integrate xlog library: Refer to integration/xlog