Avara3D 0.2.0
C++ API reference
FileLogSink.h
1//
2// FileLogSink.h
3// avara3d
4//
5// Created by Morgan Davis on 11/9/23.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_LOG_SINK_FILELOGSINK_H
10#define AVARA3D_LOG_SINK_FILELOGSINK_H
11
12#include <filesystem>
13#include <fstream>
14#include <iostream>
15#include <memory>
16#include <string>
17#include <vector>
18
19#include "a3d/log/sink/LogSink.h"
20
21namespace a3d::log {
22
29 class FileLogSink : public LogSink {
30
31 public:
32 // [Public Lifecycle Functions]
33
44 explicit FileLogSink(const std::filesystem::path& relPath,
45 int maxFiles = DEFAULT_MAX_FILES,
46 int maxFilesize = DEFAULT_MAX_FILESIZE);
47
48 FileLogSink(const FileLogSink&) = delete;
49 FileLogSink& operator=(const FileLogSink&) = delete;
50
51 FileLogSink(FileLogSink&&) = delete;
52 FileLogSink& operator=(FileLogSink&&) = delete;
53
54 ~FileLogSink() override;
55
56 // [Public Member Functions]
57
59 const std::filesystem::path& filepath() const;
60
62 int maxFiles() const;
63
65 int maxFilesize() const;
66
67 // [Public LogSink Member Functions]
68
70 void write(const std::string& output, Level level) override;
71
73 void flush() override;
74
75 private:
76 // [Private Constants]
77
78 static constexpr unsigned DEFAULT_MAX_FILES = 5;
79 static constexpr unsigned DEFAULT_MAX_FILESIZE = 1024 * 1024 * 1; // 1MB
80
81 // [Private Member Functions]
82
83 void openStream();
84 void checkRotate();
85 void rotate();
86
87 // [Private Member Variables]
88
89 std::filesystem::path _filepath;
90 int _maxFiles;
91 int _maxFilesize;
92 std::shared_ptr<std::ofstream> _fileStream;
93 };
94
95}
96
97#endif // AVARA3D_LOG_SINK_FILELOGSINK_H
LogSink that appends output to a file and rotates older files by size.
Definition: FileLogSink.h:29
FileLogSink(const std::filesystem::path &relPath, int maxFiles=DEFAULT_MAX_FILES, int maxFilesize=DEFAULT_MAX_FILESIZE)
Creates an append-mode file sink with size-based rotation.
int maxFilesize() const
Returns the configured rotation size threshold in bytes.
void flush() override
Flushes buffered output to the active file.
void write(const std::string &output, Level level) override
Appends output to the active file and performs size-based rotation when needed.
int maxFiles() const
Returns the configured maximum number of retained log files.
const std::filesystem::path & filepath() const
Returns the path of the active log file.
Interface for destinations that receive formatted Log output.
Definition: LogSink.h:19