@@ -107,14 +107,40 @@ namespace dechamps_cpplog {
107
107
Logger (this ) << " Host process: " << GetModuleName ();
108
108
}
109
109
110
- FileLogSink::FileLogSink (const std::filesystem::path& path) : stream(path, std::ios::app | std::ios::out) {
111
- Logger (this ) << " Logfile opened: " << path;
110
+ FileLogSink::FileLogSink (std::filesystem::path path, Options options) : path(std::move(path)), options(std::move(options)), stream(this ->path, std::ios::app | std::ios::out) {
111
+ Logger (this ) << " Logfile opened: " << this ->path ;
112
+ CheckSize ();
112
113
}
113
114
114
115
FileLogSink::~FileLogSink () {
115
116
Logger (this ) << " Closing logfile" ;
116
117
}
117
118
119
+ void FileLogSink::Write (const std::string_view str) {
120
+ if (!stream.is_open ()) return ;
121
+
122
+ stream_sink.Write (str);
123
+
124
+ const auto size = str.size ();
125
+ if (size >= bytesRemainingUntilSizeCheck) CheckSize ();
126
+ else bytesRemainingUntilSizeCheck -= size;
127
+ }
128
+
129
+ void FileLogSink::CheckSize () {
130
+ bytesRemainingUntilSizeCheck = (std::numeric_limits<uintmax_t >::max)();
131
+
132
+ const auto sizeBytes = std::filesystem::file_size (path);
133
+ const auto maxSizeBytes = options.maxSizeBytes ;
134
+ Logger (this ) << " Current log file size is " << sizeBytes << " bytes (maximum allowed: " << maxSizeBytes << " bytes)" ;
135
+
136
+ if (sizeBytes < maxSizeBytes) {
137
+ bytesRemainingUntilSizeCheck = options.sizeCheckPeriodBytes ;
138
+ return ;
139
+ }
140
+ Logger (this ) << " Closing logfile as the maximum size is exceeded" ;
141
+ stream.close ();
142
+ }
143
+
118
144
void ThreadSafeLogSink::Write (const std::string_view str) {
119
145
std::scoped_lock lock (mutex);
120
146
backend.Write (str);
0 commit comments