ORE Studio 0.0.4
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
scoped_attribute Class Reference

RAII guard that adds a thread-local attribute to Boost.Log for the duration of its scope. More...

#include <scoped_attribute.hpp>

Inheritance diagram for scoped_attribute:
Inheritance graph
Collaboration diagram for scoped_attribute:
Collaboration graph

Public Types

using iterator = boost::log::attribute_set::iterator
 

Public Member Functions

 scoped_attribute (const std::string &attribute_name)
 Constructs a scoped attribute guard.
 
 scoped_attribute (const scoped_attribute &)=delete
 
scoped_attributeoperator= (const scoped_attribute &)=delete
 
 scoped_attribute (scoped_attribute &&other) noexcept
 
scoped_attributeoperator= (scoped_attribute &&other) noexcept
 

Detailed Description

RAII guard that adds a thread-local attribute to Boost.Log for the duration of its scope.

This class adds a named boolean attribute with value true to the thread-local attribute set in Boost.Log's core. The attribute is automatically removed when the guard goes out of scope.

All log records created on the same thread while the guard is active will have this attribute, including records from nested function calls. This makes it useful for marking entire code paths that should be treated specially by certain sinks (e.g., skipping telemetry sinks to prevent recursive logging).

Example usage:

void send_telemetry() {
scoped_attribute guard("SkipTelemetry");
// All logs in this scope (and any functions called) will have
// the SkipTelemetry attribute set to true.
BOOST_LOG_SEV(lg, info) << "Sending...";
do_network_stuff(); // Logs here also have the attribute
}
RAII guard that adds a thread-local attribute to Boost.Log for the duration of its scope.
Definition scoped_attribute.hpp:57
Note
This class is not copyable but is movable.

Constructor & Destructor Documentation

◆ scoped_attribute()

scoped_attribute ( const std::string &  attribute_name)
explicit

Constructs a scoped attribute guard.

Adds a boolean attribute with the given name and value true to the thread-local attribute set.

Parameters
attribute_nameThe name of the attribute to add.