Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.

Class text_file_backend

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 >
  // 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
  template<typename... ArgsT> explicit text_file_backend(ArgsT...const &);

  // 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 enable_final_rotation(bool);
  void auto_flush(bool = true);
  filesystem::path get_current_file_name() const;
  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/destruct

  1. text_file_backend();

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

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

    • enable_final_rotation - Specifies a flag, whether or not perform log file rotation on sink backend destruction. By default, is true.

    • 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 the caution note regarding file name pattern in the sinks::file::collector::scan_for_files documentation.

  3. ~text_file_backend();


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.



    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



    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.



    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.



    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.



    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.



    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.



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

  8. void enable_final_rotation(bool enable);

    The method allows to enable or disable log file rotation on sink destruction.

    By default the sink backend will rotate the log file, if it's been written to, on destruction.



    The flag indicates whether the final rotation should be performed.

  9. void auto_flush(bool enable = true);

    Sets the flag to automatically flush write buffers of the file being written after each log record.



    The flag indicates whether the automatic buffer flush should be performed.

  10. filesystem::path get_current_file_name() const;


    The name of the currently open log file. If no file is open, returns an empty path.

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

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



    File scanning method


    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.


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


    The number of files found.

  12. void consume(record_view const & rec, string_type const & formatted_message);

    The method writes the message to the sink

  13. void flush();

    The method flushes the currently open log file

  14. void rotate_file();

    The method rotates the file