...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::log::sinks::text_file_backend — An implementation of a text file logging sink backend.
// In header: <boost/log/sinks/text_file_backend.hpp> class text_file_backend : public basic_formatted_sink_backend< char, combine_requirements< synchronized_feeding, flushing >::type > { public: // types typedef base_type::char_type char_type; // Character type. typedef base_type::string_type string_type; // String type to be used as a message text holder. typedef std::basic_ostream< char_type > stream_type; // Stream type. typedef unspecified open_handler_type; // File open handler. typedef unspecified close_handler_type; // File close handler. typedef unspecified time_based_rotation_predicate; // Predicate that defines the time-based condition for file rotation. // construct/copy/destruct text_file_backend(); template<typename... ArgsT> explicit text_file_backend(ArgsT...const &); ~text_file_backend(); // public member functions template<typename PathT> void set_file_name_pattern(PathT const &); void set_open_mode(std::ios_base::openmode); void set_file_collector(shared_ptr< file::collector > const &); void set_open_handler(open_handler_type const &); void set_close_handler(close_handler_type const &); void set_rotation_size(uintmax_t); void set_time_based_rotation(time_based_rotation_predicate const &); void auto_flush(bool = true); uintmax_t scan_for_files(file::scan_method = file::scan_matching, bool = true); void consume(record_view const &, string_type const &); void flush(); void rotate_file(); };
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.
text_file_backend
public
construct/copy/destructtext_file_backend();
Default constructor. The constructed sink backend uses default values of all the parameters.
template<typename... ArgsT> explicit text_file_backend(ArgsT...const & args);
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 |
~text_file_backend();
Destructor
text_file_backend
public member functionstemplate<typename PathT> void set_file_name_pattern(PathT const & pattern);
The method sets file name wildcard for the files being written. The wildcard supports date and time injection into the file name.
Parameters: |
|
void set_open_mode(std::ios_base::openmode mode);
The method sets the file open mode
Parameters: |
|
void 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: |
|
void 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: |
|
void 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: |
|
void 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: |
|
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.
Note | |
---|---|
The rotation always occurs on writing a log record, so the rotation is not strictly bound to the specified condition. |
Parameters: |
|
void auto_flush(bool f = true);
Sets the flag to automatically flush buffers of all attached streams after each log record
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. 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.
Note | |
---|---|
The method essentially delegates to the same-named function of the file collector. |
Parameters: |
|
||||
Requires: |
File collector and the proper file name pattern have already been set. |
||||
Returns: |
The number of files found. |
void consume(record_view const & rec, string_type const & formatted_message);
The method writes the message to the sink
void flush();
The method flushes the currently open log file
void rotate_file();
The method rotates the file