AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::log::FileWriter Class Reference

Log file writer. More...

#include <fileWriter.hpp>

Inheritance diagram for anch::log::FileWriter:
Collaboration diagram for anch::log::FileWriter:

Public Member Functions

 FileWriter (const anch::conf::Section &conf)
virtual ~FileWriter ()
virtual void write (const std::string &message)
Public Member Functions inherited from anch::log::Writer
 Writer (const anch::conf::Section &conf)
virtual ~Writer ()

Protected Member Functions

void writeLine (const std::string &line)
bool rotate ()
void rotateFiles ()
std::filesystem::path archive (std::uint32_t index) const
Protected Member Functions inherited from anch::log::Writer
std::string format (const std::string &msg)

Protected Attributes

std::ofstream _output
std::filesystem::path _path
std::uint64_t _maxSize
std::uint32_t _maxFiles

Detailed Description

Log file writer.

Manage file writer with file rotation on size.

Configuration:

  • path: the log file path (mandatory ; its directory has to exist) ;
  • max-size: maximum file size before rotation, in bytes, with optional unit K, M, G or T (powers of 1024). No rotation when not set ;
  • max-rotate-files: number of archives to keep (<path>.0 is the most recent). Default to 1. With 0, the file is truncated on rotation.

File system errors during rotation are ignored: the current file is kept and rotation is tried again later.

Author
Vincent Lachenal
Since
0.1

Constructor & Destructor Documentation

◆ FileWriter()

anch::log::FileWriter::FileWriter ( const anch::conf::Section & conf)

FileWriter constructor

Parameters
confthe writer's configuration
Exceptions
std::invalid_argumentinvalid configuration or file which can not be opened

◆ ~FileWriter()

virtual anch::log::FileWriter::~FileWriter ( )
virtual

FileWriter destructor

Member Function Documentation

◆ archive()

std::filesystem::path anch::log::FileWriter::archive ( std::uint32_t index) const
protected

Archive path

Parameters
indexthe archive index
Returns
<path>.<index>

◆ rotate()

bool anch::log::FileWriter::rotate ( )
protected

Check if file has to be rotate according to configuration and its size

Returns
true or false

◆ rotateFiles()

void anch::log::FileWriter::rotateFiles ( )
protected

Rotate files when current reachs the maximum file length.

◆ write()

virtual void anch::log::FileWriter::write ( const std::string & message)
virtual

Write message in the file and flush it.
Rotate log file when needed.

Parameters
messageMessage to write

Implements anch::log::Writer.

Reimplemented in anch::log::LowPriorityWriter, and anch::log::ThreadSafeWriter.

◆ writeLine()

void anch::log::FileWriter::writeLine ( const std::string & line)
protected

Write an already formatted line in the file (without flush) and rotate log file when needed.

Parameters
linethe formatted line (without end of line)

Member Data Documentation

◆ _maxFiles

std::uint32_t anch::log::FileWriter::_maxFiles
protected

Number of archives to keep

◆ _maxSize

std::uint64_t anch::log::FileWriter::_maxSize
protected

Maximum file size (0 for no rotation)

◆ _output

std::ofstream anch::log::FileWriter::_output
protected

Ouput file stream

◆ _path

std::filesystem::path anch::log::FileWriter::_path
protected

File name


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