Flow 1.0.2
Flow project: Full implementation 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>
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. More... | |
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_holder & | operator= (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. More... | |
Private Attributes | |
util::String_ostream | m_os |
Like ostringstream but allows for fast access directly into its internal string buffer. More... | |
Ostream_log_msg_writer | m_os_writer |
Wrapper around m_os that will take care of prefacing each message with time stamp, etc. More... | |
util::Mutex_non_recursive | m_log_mutex |
Mutex protecting against log messages being logged, accessing m_os concurrently and thus corrupting (or at best garbling) the output string m_os.str() . More... | |
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. More... | |
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... | |
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).
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.
Definition at line 44 of file buffer_logger.hpp.
|
explicit |
Constructs logger to subsequently log to a newly constructed internal std::string
buffer.
config | Controls 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. |
Definition at line 27 of file buffer_logger.cpp.
const std::string & flow::log::Buffer_logger::buffer_str | ( | ) | const |
Read-only access to the buffer string containing the messages logged thus far.
*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).Definition at line 53 of file buffer_logger.cpp.
References m_os, and flow::util::String_ostream::str().
Referenced by 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.
Definition at line 58 of file buffer_logger.cpp.
References buffer_str(), and m_log_mutex.
|
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.
metadata | All information to potentially log in addition to msg . |
msg | The message. |
Implements flow::log::Logger.
Definition at line 44 of file buffer_logger.cpp.
References flow::log::Ostream_log_msg_writer::log(), m_log_mutex, and m_os_writer.
|
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.
Implements flow::log::Logger.
Definition at line 39 of file buffer_logger.cpp.
|
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
.
sev | Severity of the message. |
component | Component of the message. Reminder: component.empty() == true is allowed. |
Implements flow::log::Logger.
Definition at line 34 of file buffer_logger.cpp.
Config* const flow::log::Buffer_logger::m_config |
Reference to the config object passed to constructor. Note that object is mutable; see notes on thread safety.
Definition at line 120 of file buffer_logger.hpp.
|
mutableprivate |
Mutex protecting against log messages being logged, accessing m_os concurrently and thus corrupting (or at best garbling) the output string m_os.str()
.
Definition at line 135 of file buffer_logger.hpp.
Referenced by buffer_str_copy(), and do_log().
|
private |
Like ostringstream
but allows for fast access directly into its internal string buffer.
Definition at line 126 of file buffer_logger.hpp.
Referenced by buffer_str().
|
private |
Wrapper around m_os that will take care of prefacing each message with time stamp, etc.
Definition at line 129 of file buffer_logger.hpp.
Referenced by do_log().