quill
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
FileSink Class Reference

FileSink Writes the log messages to a file. More...

#include <FileSink.h>

Inheritance diagram for FileSink:
Inheritance graph
[legend]
Collaboration diagram for FileSink:
Collaboration graph
[legend]

Public Member Functions

 FileSink (fs::path const &filename, FileSinkConfig const &config=FileSinkConfig{}, FileEventNotifier file_event_notifier=FileEventNotifier{}, bool do_fopen=true, std::chrono::system_clock::time_point start_time=std::chrono::system_clock::now())
 Construct a FileSink object. More...
 
QUILL_ATTRIBUTE_HOT void flush_sink () override
 Flushes the stream and optionally fsyncs it.
 
- Public Member Functions inherited from StreamSink
 StreamSink (fs::path stream, FILE *file=nullptr, std::optional< PatternFormatterOptions > const &override_pattern_formatter_options=std::nullopt, FileEventNotifier file_event_notifier=FileEventNotifier{})
 Constructor for StreamSink. More...
 
QUILL_ATTRIBUTE_HOT void write_log (MacroMetadata const *, uint64_t, std::string_view, std::string_view, std::string const &, std::string_view, LogLevel, std::string_view, std::string_view, std::vector< std::pair< std::string, std::string >> const *, std::string_view, std::string_view log_statement) override
 Writes a formatted log message to the stream.
 
QUILL_ATTRIBUTE_HOT void flush_sink () override
 Flushes the stream.
 
virtual QUILL_NODISCARD fs::path const & get_filename () const noexcept
 Returns the name of the file. More...
 
QUILL_NODISCARD bool is_null () const noexcept
 Checks if the stream is null. More...
 
- Public Member Functions inherited from Sink
 Sink (std::optional< PatternFormatterOptions > override_pattern_formatter_options=std::nullopt)
 Constructor Uses the default pattern formatter.
 
virtual ~Sink ()=default
 Destructor.
 
 Sink (Sink const &)=delete
 
Sinkoperator= (Sink const &)=delete
 
void set_log_level_filter (LogLevel log_level)
 Sets a log level filter on the sink. More...
 
QUILL_NODISCARD LogLevel get_log_level_filter () const noexcept
 Returns the current log level filter set on the sink. More...
 
void add_filter (std::unique_ptr< Filter > filter)
 Adds a new filter to the sink. More...
 

Protected Member Functions

void open_file (fs::path const &filename, std::string const &mode)
 Open a file. More...
 
void close_file ()
 Close the file.
 
void fsync_file (bool force_fsync=false) noexcept
 Fsync the file descriptor.
 
- Protected Member Functions inherited from StreamSink
QUILL_ATTRIBUTE_HOT void flush ()
 Flushes the stream.
 
- Protected Member Functions inherited from Sink
virtual QUILL_ATTRIBUTE_HOT void run_periodic_tasks () noexcept
 Executes periodic tasks by the backend thread, providing an opportunity for the user to perform custom tasks. More...
 
QUILL_NODISCARD bool apply_all_filters (MacroMetadata const *log_metadata, uint64_t log_timestamp, std::string_view thread_id, std::string_view thread_name, std::string_view logger_name, LogLevel log_level, std::string_view log_message, std::string_view log_statement)
 Applies all registered filters to the log record. More...
 

Static Protected Member Functions

static QUILL_NODISCARD std::string format_datetime_string (uint64_t timestamp_ns, Timezone time_zone, std::string const &append_format_pattern)
 Format a datetime string. More...
 
static QUILL_NODISCARD std::pair< std::string, std::string > extract_stem_and_extension (fs::path const &filename) noexcept
 Extract stem and extension from a filename. More...
 
static QUILL_NODISCARD fs::path append_datetime_to_filename (fs::path const &filename, std::string const &append_filename_format_pattern, Timezone time_zone, std::chrono::system_clock::time_point timestamp) noexcept
 Append date and/or time to a filename. More...
 

Protected Attributes

FileSinkConfig _config
 
std::chrono::steady_clock::time_point _last_fsync_timestamp {}
 
std::unique_ptr< char[]> _write_buffer
 
- Protected Attributes inherited from StreamSink
fs::path _filename
 
FILE * _file {nullptr}
 
size_t _file_size {0}
 The current file size.
 
FileEventNotifier _file_event_notifier
 
bool _is_null {false}
 
bool _write_occurred {false}
 

Additional Inherited Members

- Static Public Member Functions inherited from StreamSink
static QUILL_ATTRIBUTE_HOT void safe_fwrite (void const *ptr, size_t size, size_t count, FILE *stream)
 Writes data safely to the stream. More...
 

Detailed Description

FileSink Writes the log messages to a file.

Constructor & Destructor Documentation

◆ FileSink()

FileSink::FileSink ( fs::path const &  filename,
FileSinkConfig const &  config = FileSinkConfig{},
FileEventNotifier  file_event_notifier = FileEventNotifier{},
bool  do_fopen = true,
std::chrono::system_clock::time_point  start_time = std::chrono::system_clock::now() 
)
inlineexplicit

Construct a FileSink object.

This constructor will always attempt to open the given file.

Parameters
filenamePath to the file to be opened.
configConfiguration for the FileSink.
file_event_notifierNotifies on file events.
do_fopenIf false, the file will not be opened.
start_timestart time

Member Function Documentation

◆ append_datetime_to_filename()

static QUILL_NODISCARD fs::path FileSink::append_datetime_to_filename ( fs::path const &  filename,
std::string const &  append_filename_format_pattern,
Timezone  time_zone,
std::chrono::system_clock::time_point  timestamp 
)
inlinestaticprotectednoexcept

Append date and/or time to a filename.

Parameters
filenamePath to the file.
append_filename_format_patternstrftime pattern
time_zoneTimezone to use.
timestampTimestamp to use.
Returns
Updated filename.

◆ extract_stem_and_extension()

static QUILL_NODISCARD std::pair<std::string, std::string> FileSink::extract_stem_and_extension ( fs::path const &  filename)
inlinestaticprotectednoexcept

Extract stem and extension from a filename.

Parameters
filenamePath to the file.
Returns
Pair containing stem and extension.

◆ format_datetime_string()

static QUILL_NODISCARD std::string FileSink::format_datetime_string ( uint64_t  timestamp_ns,
Timezone  time_zone,
std::string const &  append_format_pattern 
)
inlinestaticprotected

Format a datetime string.

Parameters
timestamp_nsTimestamp in nanoseconds.
time_zoneTimezone to use.
append_format_patternstrftime pattern
Returns
Formatted datetime string.

◆ open_file()

void FileSink::open_file ( fs::path const &  filename,
std::string const &  mode 
)
inlineprotected

Open a file.

Parameters
filenamePath to the file.
modeFile open mode.

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