Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
kcenon::logger::file_sink Class Reference

Writes log messages to a file. More...

#include <file_sink.h>

Inheritance diagram for kcenon::logger::file_sink:
Inheritance graph
Collaboration diagram for kcenon::logger::file_sink:
Collaboration graph

Public Member Functions

 file_sink (const std::string &file_path, bool append=true)
 Construct a file sink.
 
 ~file_sink () override
 
common::VoidResult write_raw (std::string_view message) override
 Write a pre-formatted message to the output destination.
 
common::VoidResult flush () override
 Flush any buffered data.
 
bool is_healthy () const override
 Check if the sink is healthy.
 
std::string get_name () const override
 Get the name of this sink.
 
std::string get_info () const override
 Get sink information.
 
std::string get_file_path () const
 Get the file path.
 
- Public Member Functions inherited from kcenon::logger::output_sink_interface
virtual ~output_sink_interface ()=default
 

Private Attributes

std::string file_path_
 
std::ofstream file_
 
std::mutex mutex_
 
bool is_healthy_
 

Detailed Description

Writes log messages to a file.

Thread-safe file output sink. Handles file I/O only, no formatting logic.

Thread Safety:
All methods are thread-safe.
See also
output_sink_interface Base interface for all output sinks
logger Main logger that uses sinks to write messages
Since
1.3.0

Definition at line 35 of file file_sink.h.

Constructor & Destructor Documentation

◆ file_sink()

kcenon::logger::file_sink::file_sink ( const std::string & file_path,
bool append = true )
inlineexplicit

Construct a file sink.

Parameters
file_pathPath to the output file
appendIf true, append to existing file; if false, truncate

Definition at line 42 of file file_sink.h.

43 : file_path_(file_path)
44 , is_healthy_(false)
45 {
46 auto mode = append ? (std::ios::out | std::ios::app) : std::ios::out;
47 file_.open(file_path, mode);
48 is_healthy_ = file_.is_open() && file_.good();
49 }

References file_, and is_healthy_.

◆ ~file_sink()

kcenon::logger::file_sink::~file_sink ( )
inlineoverride

Definition at line 51 of file file_sink.h.

51 {
52 if (file_.is_open()) {
53 file_.flush();
54 file_.close();
55 }
56 }

References file_.

Member Function Documentation

◆ flush()

common::VoidResult kcenon::logger::file_sink::flush ( )
inlineoverridevirtual

Flush any buffered data.

Returns
common::VoidResult Success or error code

Implements kcenon::logger::output_sink_interface.

Definition at line 76 of file file_sink.h.

76 {
77 std::lock_guard<std::mutex> lock(mutex_);
78
79 if (!file_.is_open()) {
81 }
82
83 file_.flush();
84
85 if (file_.fail()) {
86 is_healthy_ = false;
88 }
89
90 return common::ok();
91 }
VoidResult ok()
common::VoidResult make_logger_void_result(logger_error_code code, const std::string &message="")

References file_, kcenon::logger::file_write_failed, is_healthy_, kcenon::logger::make_logger_void_result(), mutex_, and kcenon::common::ok().

Here is the call graph for this function:

◆ get_file_path()

std::string kcenon::logger::file_sink::get_file_path ( ) const
inline

Get the file path.

Returns
File path

Definition at line 119 of file file_sink.h.

119 {
120 return file_path_;
121 }

References file_path_.

◆ get_info()

std::string kcenon::logger::file_sink::get_info ( ) const
inlineoverridevirtual

Get sink information.

Returns
Diagnostic string

Reimplemented from kcenon::logger::output_sink_interface.

Definition at line 101 of file file_sink.h.

101 {
102 std::lock_guard<std::mutex> lock(mutex_);
103 std::string info = "file: " + file_path_;
104 if (file_.is_open()) {
105 try {
106 auto size = std::filesystem::file_size(file_path_);
107 info += ", size: " + std::to_string(size / 1024) + "KB";
108 } catch (...) {
109 // Ignore file size error
110 }
111 }
112 return info;
113 }
@ size
Rotate based on file size only.

References file_, file_path_, mutex_, and kcenon::logger::size.

◆ get_name()

std::string kcenon::logger::file_sink::get_name ( ) const
inlineoverridevirtual

Get the name of this sink.

Returns
Sink identifier

Implements kcenon::logger::output_sink_interface.

Definition at line 97 of file file_sink.h.

97 {
98 return "file";
99 }

◆ is_healthy()

bool kcenon::logger::file_sink::is_healthy ( ) const
inlineoverridevirtual

Check if the sink is healthy.

Returns
true if operational

Implements kcenon::logger::output_sink_interface.

Definition at line 93 of file file_sink.h.

93 {
94 return is_healthy_ && file_.is_open() && file_.good();
95 }

References file_, and is_healthy_.

◆ write_raw()

common::VoidResult kcenon::logger::file_sink::write_raw ( std::string_view message)
inlineoverridevirtual

Write a pre-formatted message to the output destination.

Parameters
messagePre-formatted log message
Returns
common::VoidResult Success or error code

Implements kcenon::logger::output_sink_interface.

Definition at line 58 of file file_sink.h.

58 {
59 std::lock_guard<std::mutex> lock(mutex_);
60
61 if (!file_.is_open() || !file_.good()) {
62 is_healthy_ = false;
64 }
65
66 file_ << message;
67
68 if (file_.fail()) {
69 is_healthy_ = false;
71 }
72
73 return common::ok();
74 }

References file_, kcenon::logger::file_write_failed, is_healthy_, kcenon::logger::make_logger_void_result(), mutex_, and kcenon::common::ok().

Here is the call graph for this function:

Member Data Documentation

◆ file_

std::ofstream kcenon::logger::file_sink::file_
private

Definition at line 125 of file file_sink.h.

Referenced by file_sink(), flush(), get_info(), is_healthy(), write_raw(), and ~file_sink().

◆ file_path_

std::string kcenon::logger::file_sink::file_path_
private

Definition at line 124 of file file_sink.h.

Referenced by get_file_path(), and get_info().

◆ is_healthy_

bool kcenon::logger::file_sink::is_healthy_
private

Definition at line 127 of file file_sink.h.

Referenced by file_sink(), flush(), is_healthy(), and write_raw().

◆ mutex_

std::mutex kcenon::logger::file_sink::mutex_
mutableprivate

Definition at line 126 of file file_sink.h.

Referenced by flush(), get_info(), and write_raw().


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