Boost  v1.57.0
doxygen for www.boost.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boost::sinks::text_file_backend Class Reference

An implementation of a text file logging sink backend. More...

#include <text_file_backend.hpp>

Inheritance diagram for boost::sinks::text_file_backend:
Collaboration diagram for boost::sinks::text_file_backend:

Public Types

typedef base_type::char_type char_type
 Character type. More...
 
typedef base_type::string_type string_type
 String type to be used as a message text holder. More...
 
typedef std::basic_ostream
< char_type
stream_type
 Stream type. More...
 
typedef
boost::log::aux::light_function
< void(stream_type &) > 
open_handler_type
 File open handler. More...
 
typedef
boost::log::aux::light_function
< void(stream_type &) > 
close_handler_type
 File close handler. More...
 
typedef
boost::log::aux::light_function
< bool() > 
time_based_rotation_predicate
 Predicate that defines the time-based condition for file rotation. More...
 
typedef
base_type::frontend_requirements 
frontend_requirements
 Frontend requirements. More...
 

Public Member Functions

BOOST_LOG_API text_file_backend ()
 Default constructor. More...
 
BOOST_LOG_API ~text_file_backend ()
 Constructor. More...
 
template<typename PathT >
void set_file_name_pattern (PathT const &pattern)
 The method sets file name wildcard for the files being written. More...
 
BOOST_LOG_API void set_open_mode (std::ios_base::openmode mode)
 The method sets the file open mode. More...
 
BOOST_LOG_API void set_file_collector (shared_ptr< file::collector > const &collector)
 The method sets the log file collector function. More...
 
BOOST_LOG_API void set_open_handler (open_handler_type const &handler)
 The method sets file opening handler. More...
 
BOOST_LOG_API void set_close_handler (close_handler_type const &handler)
 The method sets file closing handler. More...
 
BOOST_LOG_API void set_rotation_size (uintmax_t size)
 The method sets maximum file size. More...
 
BOOST_LOG_API void set_time_based_rotation (time_based_rotation_predicate const &predicate)
 The method sets the predicate that defines the time-based condition for file rotation. More...
 
BOOST_LOG_API void auto_flush (bool f=true)
 Sets the flag to automatically flush buffers of all attached streams after each log record. More...
 
BOOST_LOG_API uintmax_t scan_for_files (file::scan_method method=file::scan_matching, bool update_counter=true)
 Performs scanning of the target directory for log files that may have been left from previous runs of the application. More...
 
BOOST_LOG_API void consume (record_view const &rec, string_type const &formatted_message)
 The method writes the message to the sink. More...
 
BOOST_LOG_API void flush ()
 The method flushes the currently open log file. More...
 
BOOST_LOG_API void rotate_file ()
 The method rotates the file. More...
 

Detailed Description

An implementation of a text file logging sink backend.

The sink backend puts formatted log records to a text file. The sink supports file rotation and advanced file control, such as size and file count restriction.

Member Typedef Documentation

typedef boost::log::aux::light_function< void (stream_type&) > boost::sinks::text_file_backend::close_handler_type

File close handler.

typedef boost::log::aux::light_function< void (stream_type&) > boost::sinks::text_file_backend::open_handler_type

File open handler.

Stream type.

String type to be used as a message text holder.

typedef boost::log::aux::light_function< bool () > boost::sinks::text_file_backend::time_based_rotation_predicate

Predicate that defines the time-based condition for file rotation.

Constructor & Destructor Documentation

BOOST_LOG_API boost::sinks::text_file_backend::text_file_backend ( )

Default constructor.

The constructed sink backend uses default values of all the parameters.

BOOST_LOG_API boost::sinks::text_file_backend::~text_file_backend ( )

Constructor.

Creates a sink backend with the specified named parameters. The following named parameters are supported:

  • file_name - Specifies the file name pattern where logs are actually written to. The pattern may contain directory and file name portions, but only the file name may contain placeholders. The backend supports Boost.DateTime placeholders for injecting current time and date into the file name. Also, an additional N placeholder is supported, it will be replaced with an integral increasing file counter. The placeholder may also contain width specification in the printf-compatible form (e.g. %5N). The printed file counter will always be zero-filled. If file_name is not specified, pattern "%5N.log" will be used.
  • open_mode - File open mode. The mode should be presented in form of mask compatible to std::ios_base::openmode. If not specified, trunc | out will be used.
  • rotation_size - Specifies the approximate size, in characters written, of the temporary file upon which the file is passed to the file collector. Note the size does not count any possible character conversions that may take place during writing to the file. If not specified, the file won't be rotated upon reaching any size.
  • time_based_rotation - Specifies the predicate for time-based file rotation. No time-based file rotations will be performed, if not specified.
  • auto_flush - Specifies a flag, whether or not to automatically flush the file after each written log record. By default, is false.
Note
Read the caution note regarding file name pattern in the sinks::file::collector::scan_for_files documentation.

Destructor

Member Function Documentation

BOOST_LOG_API void boost::sinks::text_file_backend::auto_flush ( bool  f = true)

Sets the flag to automatically flush buffers of all attached streams after each log record.

BOOST_LOG_API void boost::sinks::text_file_backend::consume ( record_view const &  rec,
string_type const &  formatted_message 
)

The method writes the message to the sink.

BOOST_LOG_API void boost::sinks::text_file_backend::flush ( )

The method flushes the currently open log file.

BOOST_LOG_API void boost::sinks::text_file_backend::rotate_file ( )

The method rotates the file.

BOOST_LOG_API uintmax_t boost::sinks::text_file_backend::scan_for_files ( file::scan_method  method = file::scan_matching,
bool  update_counter = true 
)

Performs scanning of the target directory for log files that may have been left from previous runs of the application.

The found files are considered by the file collector as if they were rotated.

The file scan can be performed in two ways: either all files in the target directory will be considered as log files, or only those files that satisfy the file name pattern. See documentation on sinks::file::collector::scan_for_files for more information.

Precondition
File collector and the proper file name pattern have already been set.
Parameters
methodFile scanning method
update_counterIf true and method is scan_matching, the method attempts to update the internal file counter according to the found files. The counter is unaffected otherwise.
Returns
The number of files found.
Note
The method essentially delegates to the same-named function of the file collector.
BOOST_LOG_API void boost::sinks::text_file_backend::set_close_handler ( close_handler_type const &  handler)

The method sets file closing handler.

The handler will be called every time the backend closes a temporary file. The handler may write a footer to the opened file in order to maintain file validity.

Parameters
handlerThe file close handler function object
BOOST_LOG_API void boost::sinks::text_file_backend::set_file_collector ( shared_ptr< file::collector > const &  collector)

The method sets the log file collector function.

The function is called on file rotation and is being passed the written file name.

Parameters
collectorThe file collector function object
template<typename PathT >
void boost::sinks::text_file_backend::set_file_name_pattern ( PathT const &  pattern)
inline

The method sets file name wildcard for the files being written.

The wildcard supports date and time injection into the file name.

Parameters
patternThe name pattern for the file being written.
BOOST_LOG_API void boost::sinks::text_file_backend::set_open_handler ( open_handler_type const &  handler)

The method sets file opening handler.

The handler will be called every time the backend opens a new temporary file. The handler may write a header to the opened file in order to maintain file validity.

Parameters
handlerThe file open handler function object
BOOST_LOG_API void boost::sinks::text_file_backend::set_open_mode ( std::ios_base::openmode  mode)

The method sets the file open mode.

Parameters
modeFile open mode
BOOST_LOG_API void boost::sinks::text_file_backend::set_rotation_size ( uintmax_t  size)

The method sets maximum file size.

When the size is reached, file rotation is performed.

Note
The size does not count any possible character translations that may happen in the underlying API. This may result in greater actual sizes of the written files.
Parameters
sizeThe maximum file size, in characters.
BOOST_LOG_API void boost::sinks::text_file_backend::set_time_based_rotation ( time_based_rotation_predicate const &  predicate)

The method sets the predicate that defines the time-based condition for file rotation.

Note
The rotation always occurs on writing a log record, so the rotation is not strictly bound to the specified condition.
Parameters
predicateThe predicate that defines the time-based condition for file rotation. If empty, no time-based rotation will take place.

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