Flow 1.0.1
Flow project: Full implementation reference.
Public Member Functions | Public Attributes | Private Attributes | List of all members
flow::log::Serial_file_logger Class Reference

An internal-use implementation of Logger that logs messages to a given file-system path, blocking the calling thread while the I/O occurs, and usable safely only if logging occurs non-concurrently. More...

#include <serial_file_logger.hpp>

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

Public Member Functions

 Serial_file_logger (Logger *backup_logger_ptr, Config *config, const fs::path &log_path)
 Constructs logger to subsequently log to the given file-system path. More...
 
 ~Serial_file_logger () override
 Flushes out anything buffered, returns resources/closes output file(s); then returns. More...
 
bool should_log (Sev sev, const Component &component) const override
 See Async_file_logger::should_log(). 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() after that method returns. 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...
 
void log_flush_and_reopen ()
 Causes the log at the file-system path to be flushed/closed (if needed) and re-opened; this will occur synchronously meaning it will complete before the method returns. 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_holderoperator= (const Unique_id_holder &) const
 This assignment operator is a const no-op. More...
 

Public Attributes

Config *const m_config
 See Async_file_logger::m_config. More...
 

Private Attributes

const fs::path m_log_path
 File-system path to which to write subsequently. More...
 
fs::ofstream m_ofs
 The file to which to write. Because only the worker thread ever accesses it, no mutex is needed. More...
 
Ostream_log_msg_writer m_ofs_writer
 Stream writer via which to log messages to m_ofs. More...
 
bool m_reopening
 Starts at false, becomes true at entry to log_flush_and_reopen(), then becomes false again. 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...
 
- Protected Member Functions inherited from flow::log::Log_context
 Log_context (Logger *logger=0)
 Constructs Log_context by storing the given pointer to a Logger and a null Component. More...
 
template<typename Component_payload >
 Log_context (Logger *logger, Component_payload component_payload)
 Constructs Log_context by storing the given pointer to a Logger and a new Component storing the specified generically typed payload (an enum value). More...
 
 Log_context (const Log_context &src)
 Copy constructor that stores equal Logger* and Component values as the source. More...
 
 Log_context (Log_context &&src)
 Move constructor that makes this equal to src, while the latter becomes as-if default-constructed. More...
 
Log_contextoperator= (const Log_context &src)
 Assignment operator that behaves similarly to the copy constructor. More...
 
Log_contextoperator= (Log_context &&src)
 Move assignment operator that behaves similarly to the move constructor. More...
 
void swap (Log_context &other)
 Swaps Logger pointers and Component objects held by *this and other. More...
 
Loggerget_logger () const
 Returns the stored Logger pointer, particularly as many FLOW_LOG_*() macros expect. More...
 
const Componentget_log_component () const
 Returns reference to the stored Component object, particularly as many FLOW_LOG_*() macros expect. More...
 

Detailed Description

An internal-use implementation of Logger that logs messages to a given file-system path, blocking the calling thread while the I/O occurs, and usable safely only if logging occurs non-concurrently.

As of this writing it is essentially an internal component needed by Async_file_logger; but it is a full-fledged Logger nevertheless.

Thread safety

The file I/O-using APIs, notably do_log() and log_flush_and_reopen() but not should_log(), are unsafe to call concurrently. The logging user – as of this writing Async_file_logger but it's fully conceivable other uses exist – must therefore provide any anti-concurrency measures (use one thread; a strand; mutex; etc.).

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

Todo:
Consider having Serial_file_logger internally buffer any attempted log requests that it couldn't write to file due to I/O error. The logic already attempts re-open repeatedly but doesn't attempt to re-log failed lines.

Definition at line 46 of file serial_file_logger.hpp.

Constructor & Destructor Documentation

◆ Serial_file_logger()

flow::log::Serial_file_logger::Serial_file_logger ( Logger backup_logger_ptr,
Config config,
const fs::path &  log_path 
)
explicit

Constructs logger to subsequently log to the given file-system path.

It will append. This constructor itself does not perform any I/O operations.

Parameters
configSee Async_file_logger ctor.
log_pathSee Async_file_logger ctor.
backup_logger_ptrSee Async_file_logger ctor.

Definition at line 27 of file serial_file_logger.cpp.

References FLOW_LOG_INFO, log_flush_and_reopen(), and m_log_path.

Here is the call graph for this function:

◆ ~Serial_file_logger()

flow::log::Serial_file_logger::~Serial_file_logger ( )
override

Flushes out anything buffered, returns resources/closes output file(s); then returns.

Definition at line 50 of file serial_file_logger.cpp.

References FLOW_LOG_INFO, m_log_path, and m_ofs.

Member Function Documentation

◆ do_log()

void flow::log::Serial_file_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.

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

Implements flow::log::Logger.

Definition at line 63 of file serial_file_logger.cpp.

References FLOW_LOG_WARNING, flow::log::Ostream_log_msg_writer::log(), log_flush_and_reopen(), m_log_path, m_ofs, and m_ofs_writer.

Here is the call graph for this function:

◆ log_flush_and_reopen()

void flow::log::Serial_file_logger::log_flush_and_reopen ( )

Causes the log at the file-system path to be flushed/closed (if needed) and re-opened; this will occur synchronously meaning it will complete before the method returns.

Definition at line 111 of file serial_file_logger.cpp.

References FLOW_LOG_INFO, FLOW_LOG_SET_LOGGER, FLOW_LOG_TRACE, FLOW_LOG_WARNING, m_log_path, m_ofs, and m_reopening.

Referenced by do_log(), and Serial_file_logger().

Here is the caller graph for this function:

◆ logs_asynchronously()

bool flow::log::Serial_file_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() after that method returns.

Returns
See above.

Implements flow::log::Logger.

Definition at line 193 of file serial_file_logger.cpp.

◆ should_log()

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

Member Data Documentation

◆ m_config

Config* const flow::log::Serial_file_logger::m_config

See Async_file_logger::m_config.

Definition at line 111 of file serial_file_logger.hpp.

◆ m_log_path

const fs::path flow::log::Serial_file_logger::m_log_path
private

File-system path to which to write subsequently.

Definition at line 117 of file serial_file_logger.hpp.

Referenced by do_log(), log_flush_and_reopen(), Serial_file_logger(), and ~Serial_file_logger().

◆ m_ofs

fs::ofstream flow::log::Serial_file_logger::m_ofs
private

The file to which to write. Because only the worker thread ever accesses it, no mutex is needed.

Definition at line 120 of file serial_file_logger.hpp.

Referenced by do_log(), log_flush_and_reopen(), and ~Serial_file_logger().

◆ m_ofs_writer

Ostream_log_msg_writer flow::log::Serial_file_logger::m_ofs_writer
private

Stream writer via which to log messages to m_ofs.

A m_ofs_writer.log() call synchronously writes to m_ofs and flushes it to the output device (file). Same thread safety situation as m_ofs (only worker thread access it).

Definition at line 127 of file serial_file_logger.hpp.

Referenced by do_log().

◆ m_reopening

bool flow::log::Serial_file_logger::m_reopening
private

Starts at false, becomes true at entry to log_flush_and_reopen(), then becomes false again.

Simple anti-recursion measure.

Definition at line 133 of file serial_file_logger.hpp.

Referenced by log_flush_and_reopen().


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