Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
rotating_file_writer.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
12#pragma once
13
14#include "file_writer.h"
16
18
19#include <chrono>
20#include <vector>
21#include <sstream>
22
23namespace kcenon::logger {
24
29enum class rotation_type {
30 size,
31 daily,
32 hourly,
34};
35
61public:
69 rotating_file_writer(const std::string& filename,
70 size_t max_size,
71 size_t max_files,
72 size_t check_interval = 100);
73
81 rotating_file_writer(const std::string& filename,
82 rotation_type type,
83 size_t max_files,
84 size_t check_interval = 100);
85
95 rotating_file_writer(const std::string& filename,
96 rotation_type type,
97 size_t max_size,
98 size_t max_files,
99 size_t check_interval = 100);
100
104 std::string get_name() const override { return "rotating_file"; }
105
109 void rotate();
110
111public:
119 common::VoidResult write(const log_entry& entry) override;
120
121private:
125 bool should_rotate() const;
126
131 void perform_rotation();
132
137 std::string generate_rotated_filename(int index = -1) const;
138
142 void cleanup_old_files();
143
147 std::vector<std::string> get_backup_files() const;
148
152 bool should_rotate_by_time() const;
153
157 std::size_t get_file_size() const;
158
159private:
161 size_t max_size_;
164 size_t writes_since_check_{0};
165 std::string base_filename_;
166 std::string file_extension_;
167 std::chrono::system_clock::time_point last_rotation_time_;
168 std::chrono::system_clock::time_point current_period_start_;
169};
170
171} // namespace kcenon::logger
Core file writer for logging to files.
Definition file_writer.h:44
File writer with automatic log rotation support.
std::chrono::system_clock::time_point current_period_start_
std::chrono::system_clock::time_point last_rotation_time_
size_t check_interval_
Number of writes between rotation checks.
std::string get_name() const override
Get writer name.
File writer for logging to files with optional buffering.
DLL export/import macros for logger_system shared library support.
#define LOGGER_SYSTEM_API
rotation_type
Determines when log rotation should occur.
@ hourly
Rotate every hour.
@ size_and_time
Rotate based on both size and time.
@ daily
Rotate daily at midnight.
@ size
Rotate based on file size only.
Represents a single log entry with all associated metadata.
Definition log_entry.h:155
Writer category interfaces and type traits kcenon.