Boost C++ Libraries

PrevUpHomeNext

Class template basic_text_file_backend

boost::log::sinks::basic_text_file_backend — An implementation of a text file logging sink backend.

Synopsis

// In header: <boost/log/sinks/text_file_backend.hpp>

template<typename CharT> 
class basic_text_file_backend : public basic_formatting_sink_backend< CharT > {
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 base_type::target_string_type    target_string_type;             // String type to be used as a message text holder. 
  typedef base_type::record_type           record_type;                    // Log record type. 
  typedef base_type::stream_type           stream_type;                    // Stream type. 
  typedef unspecified                      path_type;                      // Path type that is used by Boost.Log. 
  typedef function1< void, stream_type & > open_handler_type;              // File open handler. 
  typedef function1< void, stream_type & > close_handler_type;             // File close handler. 
  typedef function0< bool >                time_based_rotation_predicate;  // Predicate that defines the time-based condition for file rotation. 

  // construct/copy/destruct
  basic_text_file_backend();
  template<typename... ArgsT> 
    explicit basic_text_file_backend(ArgsT...const &);
  ~basic_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);
};

Description

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.

basic_text_file_backend public construct/copy/destruct

  1. basic_text_file_backend();

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

  2. template<typename... ArgsT> 
      explicit basic_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] Note

    Read caution regarding file name pattern in the file::collector::scan_for_files documentation.

  3. ~basic_text_file_backend();

    Destructor

basic_text_file_backend public member functions

  1. template<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:
    pattern

    The name pattern for the file being written.

  2. void set_open_mode(std::ios_base::openmode mode);

    The method sets the file open mode

    Parameters:
    mode

    File open mode

  3. 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:
    collector

    The file collector function object

  4. 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:
    handler

    The file open handler function object

  5. 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:
    handler

    The file close handler function object

  6. void set_rotation_size(uintmax_t size);

    The method sets maximum file size. When the size is reached, file rotation is performed.

    [Note] 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:
    size

    The maximum file size, in characters.

  7. 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] Note

    The rotation always occurs on writing a log record, so the rotation is not strictly bound to the specified condition.

    Parameters:
    predicate

    The predicate that defines the time-based condition for file rotation. If empty, no time-based rotation will take place.

  8. void auto_flush(bool f = true);

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

  9. 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 file::collector::scan_for_files for more information.

    [Note] Note

    The method essentially delegates to the same-named function of the file collector.

    Parameters:
    method

    File scanning method

    update_counter

    If 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.

    Requires:

    File collector and the proper file name pattern have already been set.

    Returns:

    The number of files found.


PrevUpHomeNext