@@ -6,18 +6,16 @@ namespace plugify {
66 class FileLogger final : public ConsoleLogger {
77 public:
88 FileLogger (
9- const std::filesystem::path& logFile,
9+ std::filesystem::path logFile,
1010 Severity minSeverity = Severity::Info,
1111 size_t maxFileSize = 10 * 1024 * 1024
1212 ) // 10MB default
1313 : ConsoleLogger(minSeverity)
14- , _logPath(logFile)
14+ , _logPath(TimestampedPath(logFile))
15+ , _logBase(std::move(logFile))
1516 , _maxFileSize(maxFileSize) {
16- // Create log directory if needed
17- auto parent = _logPath.parent_path ();
18- if (!parent.empty () && !std::filesystem::exists (parent)) {
19- std::filesystem::create_directories (parent);
20- }
17+ std::error_code ec;
18+ std::filesystem::create_directories (base.parent_path (), ec);
2119
2220 _logFile.open (_logPath, std::ios::app);
2321 if (!_logFile) {
@@ -68,8 +66,9 @@ namespace plugify {
6866 }
6967
7068 private:
71- std::filesystem::path _logPath;
7269 std::ofstream _logFile;
70+ std::filesystem::path _logPath;
71+ std::filesystem::path _logBase;
7372 size_t _maxFileSize;
7473
7574 bool ShouldRotate () {
@@ -78,28 +77,24 @@ namespace plugify {
7877 }
7978
8079 void RotateLog () {
81- using namespace std ::chrono;
82-
8380 _logFile.close ();
81+ _logPath = TimestampedPath (_logBase);
82+ _logFile.open (_logPath, std::ios::trunc);
83+ }
8484
85+ // Helper: turn /logs/session.log → /logs/session-20260418_143022.log
86+ static std::filesystem::path TimestampedPath (const std::filesystem::path& base) {
87+ using namespace std ::chrono;
8588 auto now = system_clock::now ();
86- // floor to seconds so filename has no fractional seconds
87- auto seconds = floor<seconds>(now);
88-
89- // zoned_time with current_zone() -> local time
90- std::chrono::zoned_time zt{ std::chrono::current_zone (), seconds };
91-
92- // Format as: stem.YYYYMMDD_HHMMSS.extension
93- auto rotatedPath = _logPath.parent_path ()
94- / std::format (
95- " {}.{:%Y%m%d_%H%M%S}{}" ,
96- plg::as_string (_logPath.stem ()),
97- zt, // formatted using chrono spec
98- plg::as_string (_logPath.extension ())
99- );
100-
101- std::filesystem::rename (_logPath, rotatedPath);
102- _logFile.open (_logPath, std::ios::app);
89+ auto seconds = floor<std::chrono::seconds>(now);
90+
91+ return base.parent_path ()
92+ / std::format (
93+ " {}-{:%Y%m%d_%H%M%S}{}" ,
94+ plg::as_string (base.stem ()),
95+ utc_clock::from_sys (seconds),
96+ plg::as_string (base.extension ())
97+ );
10398 }
10499 };
105100}
0 commit comments