Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
file_sink.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
14#include <fstream>
15#include <mutex>
16#include <filesystem>
18
19namespace kcenon::logger {
20
36public:
42 explicit file_sink(const std::string& file_path, bool append = true)
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 }
50
51 ~file_sink() override {
52 if (file_.is_open()) {
53 file_.flush();
54 file_.close();
55 }
56 }
57
58 common::VoidResult write_raw(std::string_view message) override {
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 }
75
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 }
92
93 bool is_healthy() const override {
94 return is_healthy_ && file_.is_open() && file_.good();
95 }
96
97 std::string get_name() const override {
98 return "file";
99 }
100
101 std::string get_info() const override {
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 }
114
119 std::string get_file_path() const {
120 return file_path_;
121 }
122
123private:
124 std::string file_path_;
125 std::ofstream file_;
126 mutable std::mutex mutex_;
128};
129
130} // namespace kcenon::logger
Writes log messages to a file.
Definition file_sink.h:35
std::string get_info() const override
Get sink information.
Definition file_sink.h:101
std::string get_file_path() const
Get the file path.
Definition file_sink.h:119
bool is_healthy() const override
Check if the sink is healthy.
Definition file_sink.h:93
common::VoidResult flush() override
Flush any buffered data.
Definition file_sink.h:76
common::VoidResult write_raw(std::string_view message) override
Write a pre-formatted message to the output destination.
Definition file_sink.h:58
std::string get_name() const override
Get the name of this sink.
Definition file_sink.h:97
file_sink(const std::string &file_path, bool append=true)
Construct a file sink.
Definition file_sink.h:42
Abstract interface for log output destinations (I/O only)
VoidResult ok()
common::VoidResult make_logger_void_result(logger_error_code code, const std::string &message="")
@ size
Rotate based on file size only.
Interface for log output destinations (Single Responsibility Principle) kcenon.