Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 48 commits into
base: master
Choose a base branch
from

Conversation

ethouris
Copy link
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 << setw(0) << "-" << dec << b;

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

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

This above can't be used directly without overloading the operator << with fmt_sender_proxy, but this is here available for the LOGC macro. The helper class ofmtstream was provided, which is mainly a wrapper around std::stringstream, but it makes all string-like types bypass the formatting facilities, 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 ofmtstream 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);

The fmtc structure was used because it minimizes scope of the manipulator names, as well as allows to create a variable with format configuration to be then applied in multiple following calls of fmt. Symbol names introduced here:

  • fmt: forced formatting function
  • fmtc: format configuration structure
  • ofmtstream: format handler for multiple arguments
  • fmtcat: formats all arguments one by one and returns everything as std::string

TO CONSIDER: Maybe ofmtbufstream would be a better name for this one so that ofmtstream can be used to wrap any std::ostream.

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.

srtcore/srt_sfmt.h Fixed Show fixed Hide fixed
srtcore/srt_sfmt.h Fixed Show fixed Hide fixed
srtcore/logging.h Fixed Show fixed Hide fixed
srtcore/srt_sfmt.h Fixed Show fixed Hide fixed
srtcore/utilities.h Fixed Show fixed Hide fixed
srtcore/utilities.h Fixed Show fixed Hide fixed
srtcore/utilities.h Fixed Show fixed Hide fixed
srtcore/ofmt.h Fixed Show fixed Hide fixed
srtcore/ofmt.h Fixed Show fixed Hide fixed
srtcore/logging.h Fixed Show fixed Hide fixed
@ethouris ethouris changed the title [core] Setting up fmt with iomanip for logging system [core] Setting up fmt with custom config structure for logging system Jul 18, 2024
srtcore/ofmt.h Dismissed Show dismissed Hide dismissed
srtcore/ofmt.h Dismissed Show dismissed Hide 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
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.

1 participant