Flow 1.0.1
Flow project: Public API.
Public Member Functions | Public Attributes | List of all members
flow::log::Buffer_logger Class Reference

An implementation of Logger that logs messages to an internal std::string buffer and provides read-only access to this buffer (for example, if one wants to write out its contents when exiting program). More...

#include <buffer_logger.hpp>

Inheritance diagram for flow::log::Buffer_logger:
[legend]
Collaboration diagram for flow::log::Buffer_logger:
[legend]

Public Member Functions

 Buffer_logger (Config *config)
 Constructs logger to subsequently log to a newly constructed internal std::string buffer. More...
 
bool should_log (Sev sev, const Component &component) const override
 Implements interface method by returning true if the severity and component (which is allowed to be null) indicate it should. More...
 
bool logs_asynchronously () const override
 Implements interface method by returning false, indicating that this Logger will not need the contents of *metadata and msg passed to do_log(), as the latter will output them synchronously. More...
 
void do_log (Msg_metadata *metadata, util::String_view msg) override
 Implements interface method by synchronously logging the message and some subset of the metadata in a fashion controlled by m_config. More...
 
const std::string & buffer_str () const
 Read-only access to the buffer string containing the messages logged thus far. More...
 
const std::string buffer_str_copy () const
 Returns a copy of buffer_str() in thread-safe fashion. More...
 
- Public Member Functions inherited from flow::log::Logger
virtual bool should_log (Sev sev, const Component &component) const =0
 Given attributes of a hypothetical message that would be logged, return true if that message should be logged and false otherwise (e.g., if the verbosity of the message is above the current configured verbosity threshold for the Component specified). More...
 
virtual bool logs_asynchronously () const =0
 Must return true if do_log() at least sometimes logs the given message and metadata (e.g., time stamp) after do_log() returns; false if this never occurs (i.e., it logs synchronously, always). More...
 
virtual void do_log (Msg_metadata *metadata, util::String_view msg)=0
 Given a message and its severity, logs that message and possibly severity WITHOUT checking whether it should be logged (i.e., without performing logic that should_log() performs). More...
 
std::ostream * this_thread_ostream () const
 Returns the stream dedicated to the executing thread and this Logger, so that the caller can apply state-setting formatters to it. More...
 
- Public Member Functions inherited from flow::util::Null_interface
virtual ~Null_interface ()=0
 Boring virtual destructor. More...
 
- Public Member Functions inherited from flow::util::Unique_id_holder
 Unique_id_holder ()
 Thread-safely construct an ID whose value is different from any other object of this class, past or future.
 
 Unique_id_holder (const Unique_id_holder &)
 This copy constructor is identical in behavior to Unique_id_holder(), the default ctor. More...
 
id_t unique_id () const
 Raw unique ID identifying this object as well as any object of a derived type. More...
 
const Unique_id_holderoperator= (const Unique_id_holder &) const
 This assignment operator is a const no-op. More...
 

Public Attributes

Config *const m_config
 Reference to the config object passed to constructor. Note that object is mutable; see notes on thread safety.
 

Additional Inherited Members

- Public Types inherited from flow::util::Unique_id_holder
using id_t = uint64_t
 Raw integer type to uniquely identify a thing. 64-bit width should make overflow extremely hard to reach.
 
- Static Public Member Functions inherited from flow::log::Logger
static void this_thread_set_logged_nickname (util::String_view thread_nickname=util::String_view(), Logger *logger_ptr=0, bool also_set_os_name=true)
 Sets or unsets the current thread's logging-worthy string name; optionally sets the OS thread name (such as visible in top output). More...
 
static std::ostream & this_thread_logged_name_os_manip (std::ostream &os)
 ostream manipulator function that, if output via operator<< to an ostream, will cause the current thread's logging-worthy string name to be output to that stream. More...
 
static void set_thread_info_in_msg_metadata (Msg_metadata *msg_metadata)
 Loads msg_metadata->m_call_thread_nickname (if set) or else msg_metadata->m_call_thread_id, based on whether/how this_thread_set_logged_nickname() was last called in the current thread. More...
 
static void set_thread_info (std::string *call_thread_nickname, flow::util::Thread_id *call_thread_id)
 Same as set_thread_info_in_msg_metadata() but targets the given two variables as opposed to a Msg_metadata. More...
 
- Static Public Member Functions inherited from flow::util::Unique_id_holder
static id_t create_unique_id ()
 Short-hand for Unique_id_holder().unique_id(); useful when all you want is the unique integer itself. More...
 

Detailed Description

An implementation of Logger that logs messages to an internal std::string buffer and provides read-only access to this buffer (for example, if one wants to write out its contents when exiting program).

Thread safety

Simultaneous logging from multiple threads is safe from output corruption, in that simultaneous do_log() calls for the same Logger will log serially to each other. buffer_str_copy() is also safe. However, buffer_str() may not be thread-safe; see its doc header for more information.

See thread safety notes and to-dos regarding m_config in Simple_ostream_logger doc header. These apply here also.

There are no other mutable data (state), so that's that.

Constructor & Destructor Documentation

◆ Buffer_logger()

flow::log::Buffer_logger::Buffer_logger ( Config config)
explicit

Constructs logger to subsequently log to a newly constructed internal std::string buffer.

Parameters
configControls behavior of this Logger. In particular, it affects should_log() logic (verbosity default and per-component) and output format (such as time stamp format). See thread safety notes in class doc header. This is saved in m_config.

Member Function Documentation

◆ buffer_str()

const std::string & flow::log::Buffer_logger::buffer_str ( ) const

Read-only access to the buffer string containing the messages logged thus far.

Warning
While high-performance, this is not thread-safe, in that other threads logging to *this may cause the returned string to be subsequently accessed mid-write and therefore possibly in an undefined state. Take care to access the result only when you know no other thread is logging (e.g., when exiting program).
Returns
Read-only reference.

◆ buffer_str_copy()

const std::string flow::log::Buffer_logger::buffer_str_copy ( ) const

Returns a copy of buffer_str() in thread-safe fashion.

However, this may be quite slow if done frequently.

Returns
Newly created string.

◆ do_log()

void flow::log::Buffer_logger::do_log ( Msg_metadata metadata,
util::String_view  msg 
)
overridevirtual

Implements interface method by synchronously logging the message and some subset of the metadata in a fashion controlled by m_config.

Internally, this will involve a possible allocation (to enlarge an internal buffer when needed) and essentially copying of the msg and *metadata payloads onto the end of the buffer. This does not block (in the classic I/O sense) the calling thread. Needless to say, memory use may become a concern depending on the verbosity of the logging.

Parameters
metadataAll information to potentially log in addition to msg.
msgThe message.

Implements flow::log::Logger.

◆ logs_asynchronously()

bool flow::log::Buffer_logger::logs_asynchronously ( ) const
overridevirtual

Implements interface method by returning false, indicating that this Logger will not need the contents of *metadata and msg passed to do_log(), as the latter will output them synchronously.

Returns
See above.

Implements flow::log::Logger.

◆ should_log()

bool flow::log::Buffer_logger::should_log ( Sev  sev,
const Component component 
) const
overridevirtual

Implements interface method by returning true if the severity and component (which is allowed to be null) indicate it should.

As of this writing not thread-safe against changes to *m_config.

Parameters
sevSeverity of the message.
componentComponent of the message. Reminder: component.empty() == true is allowed.
Returns
See above.

Implements flow::log::Logger.


The documentation for this class was generated from the following files: