Mila 0.13.48
Deep Neural Network Library
Loading...
Searching...
No Matches
Mila::Logging::FileSink Class Referenceexport

Thread-safe logging sink that writes formatted records to a file. More...

Inheritance diagram for Mila::Logging::FileSink:
Collaboration diagram for Mila::Logging::FileSink:

Public Member Functions

 FileSink (const FileSink &)=delete
 FileSink (const std::filesystem::path &path, LogLevel initialLevel=LogLevel::Info)
 Opens path for writing and sets the minimum log level.
 FileSink (FileSink &&)=delete
 ~FileSink () override
 Flushes and closes the log file.
LogLevel getLevel () const override
 Returns the current minimum log level.
bool isEnabled (LogLevel level) const override
 Returns true if records at level would be emitted.
void log (std::string_view message, LogLevel level, const std::source_location &location=std::source_location::current()) override
 Emits a record at an explicitly specified level.
void log_critical (std::string_view message, const std::source_location &location=std::source_location::current()) override
 Emits a record at Critical level. Explicitly flushes after write.
void log_debug (std::string_view message, const std::source_location &location=std::source_location::current()) override
 Emits a record at Debug level.
void log_error (std::string_view message, const std::source_location &location=std::source_location::current()) override
 Emits a record at Error level.
void log_info (std::string_view message, const std::source_location &location=std::source_location::current()) override
 Emits a record at Info level.
void log_trace (std::string_view message, const std::source_location &location=std::source_location::current()) override
 Emits a record at Trace level.
void log_warning (std::string_view message, const std::source_location &location=std::source_location::current()) override
 Emits a record at Warning level.
FileSinkoperator= (const FileSink &)=delete
FileSinkoperator= (FileSink &&)=delete
const std::filesystem::path & path () const
 Returns the path of the log file this sink is writing to.
void setIncludeSourceLocation (bool include)
 Controls whether source location (file, line, function) is prepended to each record.
void setIncludeTimestamp (bool include)
 Controls whether a timestamp prefix is prepended to each record.
void setLevel (LogLevel level) override
 Sets the minimum level at which records are emitted.
Public Member Functions inherited from Mila::Logging::Logger
 Logger (const Logger &)=delete
 Logger (Logger &&)=delete
virtual ~Logger ()=default
Loggeroperator= (const Logger &)=delete
Loggeroperator= (Logger &&)=delete

Private Member Functions

std::string getCurrentTimestamp () const
 Builds a timestamp string in HH:MM:SS.mmm format.
std::string getLocationInfo (const std::source_location &location) const
 Builds a source location string in filename:line:function format.
void logImpl (std::string_view message, LogLevel level, const std::source_location &location)
 Core emit implementation called by all public log methods.

Static Private Member Functions

static constexpr const char * logLevelToString (LogLevel level)
 Maps a LogLevel enumerator to its fixed-width string label.

Private Attributes

LogLevel currentLevel_ = LogLevel::Info
std::ofstream fileStream_
bool includeSourceLocation_ = true
bool includeTimestamp_ = true
std::mutex logMutex_
std::filesystem::path path_

Additional Inherited Members

Static Public Member Functions inherited from Mila::Logging::Logger
static void critical (std::string_view message, const std::source_location &location=std::source_location::current())
 Emits a record at Critical level via the default logger.
static void debug (std::string_view message, const std::source_location &location=std::source_location::current())
 Emits a record at Debug level via the default logger.
static LoggerdefaultLogger ()
 Returns a reference to the registered default logger.
static void error (std::string_view message, const std::source_location &location=std::source_location::current())
 Emits a record at Error level via the default logger.
static void info (std::string_view message, const std::source_location &location=std::source_location::current())
 Emits a record at Info level via the default logger.
static void setDefaultLogger (Logger *logger)
 Registers a sink as the process-wide default logger.
static void trace (std::string_view message, const std::source_location &location=std::source_location::current())
 Emits a record at Trace level via the default logger.
static void warning (std::string_view message, const std::source_location &location=std::source_location::current())
 Emits a record at Warning level via the default logger.
Protected Member Functions inherited from Mila::Logging::Logger
 Logger ()=default

Detailed Description

Thread-safe logging sink that writes formatted records to a file.

The output file is opened with std::ios::trunc on construction so each process run produces a clean log. If the file cannot be opened the constructor throws std::runtime_error; the logger is never left in a half-open state.

Records below Error level are terminated with '\n'. Critical records receive an explicit flush to maximise visibility before a potential process termination.

// Typical wiring for the FastAPI inference server:
auto sink = std::make_shared<Mila::Logging::FileSink>(
static void setDefaultLogger(Logger *logger)
Registers a sink as the process-wide default logger.
Definition Logger.ixx:87
@ Info
Normal operational milestones (model loaded, server ready).
Definition Logger.ixx:44

Constructor & Destructor Documentation

◆ FileSink() [1/3]

Mila::Logging::FileSink::FileSink ( const std::filesystem::path & path,
LogLevel initialLevel = LogLevel::Info )
inlineexplicit

Opens path for writing and sets the minimum log level.

The file is truncated on open. Parent directories must already exist.

Parameters
pathPath to the log file.
initialLevelRecords below this level are silently discarded.
Exceptions
std::runtime_errorif the file cannot be opened.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~FileSink()

Mila::Logging::FileSink::~FileSink ( )
inlineoverride

Flushes and closes the log file.

◆ FileSink() [2/3]

Mila::Logging::FileSink::FileSink ( const FileSink & )
delete
Here is the call graph for this function:

◆ FileSink() [3/3]

Mila::Logging::FileSink::FileSink ( FileSink && )
delete
Here is the call graph for this function:

Member Function Documentation

◆ getCurrentTimestamp()

std::string Mila::Logging::FileSink::getCurrentTimestamp ( ) const
inlineprivate

Builds a timestamp string in HH:MM:SS.mmm format.

Returns
The formatted timestamp followed by a space, or an empty string if timestamps are disabled.
Here is the caller graph for this function:

◆ getLevel()

LogLevel Mila::Logging::FileSink::getLevel ( ) const
inlineoverridevirtual

Returns the current minimum log level.

Implements Mila::Logging::Logger.

◆ getLocationInfo()

std::string Mila::Logging::FileSink::getLocationInfo ( const std::source_location & location) const
inlineprivate

Builds a source location string in filename:line:function format.

The file path is reduced to the filename component only. The function name is trimmed to the bare name without namespace qualifiers or parameter list.

Parameters
locationThe source location to format.
Returns
The formatted location string followed by ": ", or an empty string if source location is disabled.
Here is the caller graph for this function:

◆ isEnabled()

bool Mila::Logging::FileSink::isEnabled ( LogLevel level) const
inlineoverridevirtual

Returns true if records at level would be emitted.

Parameters
levelThe level to test.

Implements Mila::Logging::Logger.

Here is the caller graph for this function:

◆ log()

void Mila::Logging::FileSink::log ( std::string_view message,
LogLevel level,
const std::source_location & location = std::source_location::current() )
inlineoverridevirtual

Emits a record at an explicitly specified level.

Parameters
messageThe log message.
levelThe severity level for this record.
locationAutomatically captured call-site location.

Implements Mila::Logging::Logger.

Here is the call graph for this function:

◆ log_critical()

void Mila::Logging::FileSink::log_critical ( std::string_view message,
const std::source_location & location = std::source_location::current() )
inlineoverridevirtual

Emits a record at Critical level. Explicitly flushes after write.

Implements Mila::Logging::Logger.

Here is the call graph for this function:

◆ log_debug()

void Mila::Logging::FileSink::log_debug ( std::string_view message,
const std::source_location & location = std::source_location::current() )
inlineoverridevirtual

Emits a record at Debug level.

Implements Mila::Logging::Logger.

Here is the call graph for this function:

◆ log_error()

void Mila::Logging::FileSink::log_error ( std::string_view message,
const std::source_location & location = std::source_location::current() )
inlineoverridevirtual

Emits a record at Error level.

Implements Mila::Logging::Logger.

Here is the call graph for this function:

◆ log_info()

void Mila::Logging::FileSink::log_info ( std::string_view message,
const std::source_location & location = std::source_location::current() )
inlineoverridevirtual

Emits a record at Info level.

Implements Mila::Logging::Logger.

Here is the call graph for this function:

◆ log_trace()

void Mila::Logging::FileSink::log_trace ( std::string_view message,
const std::source_location & location = std::source_location::current() )
inlineoverridevirtual

Emits a record at Trace level.

Implements Mila::Logging::Logger.

Here is the call graph for this function:

◆ log_warning()

void Mila::Logging::FileSink::log_warning ( std::string_view message,
const std::source_location & location = std::source_location::current() )
inlineoverridevirtual

Emits a record at Warning level.

Implements Mila::Logging::Logger.

Here is the call graph for this function:

◆ logImpl()

void Mila::Logging::FileSink::logImpl ( std::string_view message,
LogLevel level,
const std::source_location & location )
inlineprivate

Core emit implementation called by all public log methods.

Checks the level filter, formats the record, acquires the mutex, then writes to the file stream. Uses '\n' for all levels; Critical records receive an explicit flush to maximise visibility before a potential process termination.

Parameters
messageThe log message.
levelThe severity level of this record.
locationThe call-site source location.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ logLevelToString()

constexpr const char * Mila::Logging::FileSink::logLevelToString ( LogLevel level)
inlinestaticconstexprprivate

Maps a LogLevel enumerator to its fixed-width string label.

Parameters
levelThe level to convert.
Returns
A null-terminated string of consistent width for aligned output.
Here is the caller graph for this function:

◆ operator=() [1/2]

FileSink & Mila::Logging::FileSink::operator= ( const FileSink & )
delete
Here is the call graph for this function:

◆ operator=() [2/2]

FileSink & Mila::Logging::FileSink::operator= ( FileSink && )
delete
Here is the call graph for this function:

◆ path()

const std::filesystem::path & Mila::Logging::FileSink::path ( ) const
inline

Returns the path of the log file this sink is writing to.

Returns
The filesystem path passed at construction.
Here is the caller graph for this function:

◆ setIncludeSourceLocation()

void Mila::Logging::FileSink::setIncludeSourceLocation ( bool include)
inline

Controls whether source location (file, line, function) is prepended to each record.

Parameters
includePass false to suppress location info.

◆ setIncludeTimestamp()

void Mila::Logging::FileSink::setIncludeTimestamp ( bool include)
inline

Controls whether a timestamp prefix is prepended to each record.

Parameters
includePass false to suppress timestamps.

◆ setLevel()

void Mila::Logging::FileSink::setLevel ( LogLevel level)
inlineoverridevirtual

Sets the minimum level at which records are emitted.

Implements Mila::Logging::Logger.

Member Data Documentation

◆ currentLevel_

LogLevel Mila::Logging::FileSink::currentLevel_ = LogLevel::Info
private

◆ fileStream_

std::ofstream Mila::Logging::FileSink::fileStream_
private

◆ includeSourceLocation_

bool Mila::Logging::FileSink::includeSourceLocation_ = true
private

◆ includeTimestamp_

bool Mila::Logging::FileSink::includeTimestamp_ = true
private

◆ logMutex_

std::mutex Mila::Logging::FileSink::logMutex_
mutableprivate

◆ path_

std::filesystem::path Mila::Logging::FileSink::path_
private

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