Skip to content

Conversation

@ChrisThrasher
Copy link
Member

@ChrisThrasher ChrisThrasher commented Jul 12, 2024

Description

Closes #3003

The thread safety itself is solved in a simple matter of locking a mutex each time the logging function is called. This ensures that logs are not interleaved.

On the API side of things, a new sf::priv::log function is added which uses a bit of parameter pack magic to still let us use I/O stream formatters as a convenience. The only change to the public API is a new function that users can use to change the destination buffer for logs. This API is used in some new unit tests to confirm that everything works.

This is a draft PR because if accepted, this PR needs to remove sf::err entirely to finish the transformation to this new logging interface.

((m_settings.attributeFlags & ContextSettings::Debug) != 0),
" ; sRGB = ",
m_settings.sRgbCapable,
std::noboolalpha);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the biggest downsides. clang-format is configured to put each function parameter on a separate line whereas chances of operator<< are more neatly packed into lines. This isn't a dealbreaker in my opinion but it is a bit of a bummer. If we really wanted to we could still make a local std::stringstream then pass that stream to log().

@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 9914129116

Details

  • 40 of 57 (70.18%) changed or added relevant lines in 10 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall first build on error_logging at 55.981%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/SFML/Window/EGLCheck.cpp 0 1 0.0%
src/SFML/Window/EglContext.cpp 0 2 0.0%
src/SFML/Window/SensorManager.cpp 0 2 0.0%
src/SFML/Window/Window.cpp 0 2 0.0%
src/SFML/Window/Context.cpp 0 3 0.0%
src/SFML/Window/WindowImpl.cpp 0 3 0.0%
src/SFML/Window/GlContext.cpp 18 22 81.82%
Totals Coverage Status
Change from base Build 9860246112: 56.0%
Covered Lines: 11719
Relevant Lines: 19703

💛 - Coveralls

@vittorioromeo
Copy link
Member

Could you compare the pros/cons on this approach with #3010? That PR keeps the same interface and transparently adds thread-safety on top.

This PR is still using streams under the hood, so we might as well keep using streams as the user-facing syntax until we switch to a format-based approach.

The other benefit of #3010 is that it can be used to completely isolate the <ostream> dependency, as I've done here in my personal fork: Err.hpp, Err.cpp.

@eXpl0it3r eXpl0it3r modified the milestones: 3.0, 3.1 Sep 8, 2024
@eXpl0it3r eXpl0it3r removed this from SFML 3.1.0 Jun 19, 2025
@eXpl0it3r eXpl0it3r removed this from the 3.1 milestone Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sf::err() is not thread-safe

5 participants