Skip to content

[core] Setting up fmt with custom config structure for logging system#2964

Merged
ethouris merged 61 commits into
Haivision:devfrom
ethouris:dev-add-custom-fmt-for-logging
Aug 26, 2025
Merged

[core] Setting up fmt with custom config structure for logging system#2964
ethouris merged 61 commits into
Haivision:devfrom
ethouris:dev-add-custom-fmt-for-logging

Conversation

@ethouris
Copy link
Copy Markdown
Collaborator

@ethouris ethouris commented Jun 25, 2024

This provides the helper class fmt_sender_proxy with a helper function fmt so that nondefault formatting can be used in place. All intermediate formatting is done using std::ostringstream or std::stringstream class.

This proposes a new API for all iostream-based solutions, just like also iostream itself. For example: In order to write values of a, x, b in the form like: 17-0001dead-42 using printf you need:

printf("%d-%08x-%d", a, x, b);

And in the standard iostream, you'd have to do the following:

cout << a << "-" << hex << setfill('0') << setw(8) << x << "-" << dec << b;

(Setting width back to 0 is not necessary because it's done after every printed integer.)

With this ofmt facility the same can be written simply as:

cout << a << "-" << fmt(x, fmtc().fillzero().hex().width(8)) << "-" << b;

To use operator << this way, you have to also include ofmt_iostream.h. For the LOGC macro it is already provided.

The helper class ofmtbufstream was provided for any intermediate formatting. It's mainly a wrapper over std::stringstream, while all string-like classes are written directly to the stream (bypass formatting), while all other types (mainly numeric) are passed through fmt with default formatting. Formatting is done by another std::stringstream inside fmt_sender_proxy, which is then copied buffer-to-buffer to the std::stringstream in the ofmtbufstream class. The operator<< is still in use here for C++03-based code, and for C++11-based code there's additionally the print function, which does the same:

ostr.print(a, "-", fmt(x, fmtc().fillzero().hex().width(8)), "-", b);

It was chosen to have fmtc with manipulators implemented as its methods in order to minimize namespace pollution. This allows also to create a variable for a shortcut name of format configuration to be then applied in multiple following calls of fmt. Symbol names introduced here:

  • fmt: forced formatting function (returns the value-wrapping proxy to apply formatting when sent to the stream)
  • fmts: like fmt, but returns the formatted value as std::string
  • fmtc: format configuration structure
  • ofmtbufstream: format handler for multiple arguments
  • ofmtrefstream: format handler as a wrapper over std::ostream class
  • fmtcat: formats all arguments one by one and returns everything as std::string

Beside this solution, there were two others so far tried:

  1. The sfmt formatter that uses snprintf function and crafts the format string out of the formatter structure - [core] New formatting implementation using SFMT for the logging system #2955
  2. The solution based on the {fmt} library that reuses its formatter structure and printing functions (note: requires C++11). - [core] Implementation of logging/formatting system using {fmt} #2963

This solution, out of all that were tried, shows the lowest CPU usage.

Comment thread srtcore/srt_sfmt.h Fixed
Comment thread srtcore/srt_sfmt.h Fixed
Comment thread srtcore/logging.h Fixed
Comment thread srtcore/srt_sfmt.h Fixed
Comment thread srtcore/utilities.h Fixed
Comment thread srtcore/utilities.h Fixed
Comment thread srtcore/utilities.h Fixed
Comment thread srtcore/ofmt.h Dismissed
Comment thread srtcore/ofmt.h Dismissed
@ethouris ethouris marked this pull request as ready for review September 3, 2024 15:49
@ethouris ethouris added this to the Backlog milestone Sep 4, 2024
@ethouris ethouris mentioned this pull request Mar 21, 2025
45 tasks
@ethouris ethouris changed the base branch from master to dev April 11, 2025 09:08
@ethouris ethouris modified the milestones: Backlog, v1.6.0 Apr 11, 2025
@ethouris ethouris merged commit 022aa81 into Haivision:dev Aug 26, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants