Convenience class that simply stores a Logger and/or Component passed into a constructor; and returns this Logger and Component via get_logger() and get_log_component() public accessors.
It's extremely useful (almost mandatory in conventional practice) for classes that want to log, as they can simply derive from it (passing in the desired Logger*
and Component payload (an enum
value) into the Log_context superclass constructor), at which point the get_logger() and get_log_component() functions the FLOW_LOG_...()
macros expect automatically become available without any additional code having to be written in the logging class. Here is how:
class I_like_to_have_fun_and_log_about_it :
{
public:
I_like_to_have_fun_and_log_about_it() :
Log_context(&m_logger, My_cool_components::S_FUN_HAVER),
{
FLOW_LOG_INFO(
"I can log right from the constructor and throughout *this lifetime!");
}
private:
void do_fun_stuff()
{
FLOW_LOG_INFO(
"I am about to do something cool and fun: " << 42 <<
"!");
}
};
Convenience class that simply stores a Logger and/or Component passed into a constructor; and returns...
Definition: log.hpp:1619
An implementation of Logger that logs messages to the given ostreams (e.g., cout or an ofstream for a...
Definition: simple_ostream_logger.hpp:85
#define FLOW_LOG_INFO(ARG_stream_fragment)
Logs an INFO message into flow::log::Logger *get_logger() with flow::log::Component get_log_component...
Definition: log.hpp:197
Sev
Enumeration containing one of several message severity levels, ordered from highest to lowest.
Definition: log_fwd.hpp:224
@ S_INFO
Message indicates a not-"bad" condition that is not frequent enough to be of severity Sev::S_TRACE.
Catch-all namespace for the Flow project: A collection of various production-quality modules written ...
Definition: async_fwd.hpp:75
Note that the operator=()
allows one to change the underlying Logger anytime after construction (e.g., existing_log_context = Log_context(&some_logger, Some_enum::S_SOME_COMPONENT);
).
Implementation notes
The code could be shorter by getting rid of non-copy constuctor in favor of direct member initialization by user; and by simply omitting the API for the auto-generated copy constructor and assignment. However, in this case, I wanted to clearly document the API; and since there are more than 1 constructors, it seemed better to explicitly declare them all instead of replacing some with implicitly required direct initialization (again to make API more clearly documented).
Thread safety
The only operation of interest w/r/t threads is the aforementioned implicit assignment operator. Thread safety is the same as for any struct
with no locking done therein.