Skip to content

Commit 6c257aa

Browse files
authored
Ensure file_opened_ is getting updated when rotating (#7296)
1 parent 99f59b1 commit 6c257aa

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

shared/src/native-src/lazy_rotating_file_sink-inl.h

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ SPDLOG_INLINE filename_t lazy_rotating_file_sink<Mutex>::filename()
6969
template<typename Mutex>
7070
SPDLOG_INLINE void lazy_rotating_file_sink<Mutex>::sink_it_(const details::log_msg &msg)
7171
{
72-
if (!file_opened_)
73-
{
74-
// I _think_ this is called under a mutex, so we don't need to lock here.
75-
file_opened_ = true;
76-
file_helper_.open(calc_filename(base_filename_, 0));
77-
current_size_ = file_helper_.size(); // expensive. called only once
72+
try {
73+
// let's open the file if is needed
74+
open_if_needed_();
75+
} catch (const spdlog_ex&) {
76+
file_opened_ = false;
77+
return;
7878
}
7979

8080
memory_buf_t formatted;
@@ -93,8 +93,12 @@ SPDLOG_INLINE void lazy_rotating_file_sink<Mutex>::sink_it_(const details::log_m
9393
new_size = formatted.size();
9494
}
9595
}
96-
file_helper_.write(formatted);
97-
current_size_ = new_size;
96+
97+
if (file_opened_)
98+
{
99+
file_helper_.write(formatted);
100+
current_size_ = new_size;
101+
}
98102
}
99103

100104
template<typename Mutex>
@@ -117,6 +121,7 @@ SPDLOG_INLINE void lazy_rotating_file_sink<Mutex>::rotate_()
117121
using details::os::filename_to_str;
118122
using details::os::path_exists;
119123

124+
file_opened_ = false;
120125
file_helper_.close();
121126
for (auto i = max_files_; i > 0; --i)
122127
{
@@ -137,11 +142,24 @@ SPDLOG_INLINE void lazy_rotating_file_sink<Mutex>::rotate_()
137142
{
138143
file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
139144
current_size_ = 0;
145+
file_opened_ = true;
140146
throw_spdlog_ex("lazy_rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno);
141147
}
142148
}
143149
}
144150
file_helper_.reopen(true);
151+
file_opened_ = true;
152+
}
153+
154+
template<typename Mutex>
155+
SPDLOG_INLINE void lazy_rotating_file_sink<Mutex>::open_if_needed_()
156+
{
157+
if (!file_opened_)
158+
{
159+
file_helper_.open(calc_filename(base_filename_, 0));
160+
current_size_ = file_helper_.size(); // expensive. called only once
161+
file_opened_ = true;
162+
}
145163
}
146164

147165
// delete the target if exists, and rename the src file to target

shared/src/native-src/lazy_rotating_file_sink.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class lazy_rotating_file_sink final : public base_sink<Mutex>
4040
// log.3.txt -> delete
4141
void rotate_();
4242

43+
void open_if_needed_();
44+
4345
// delete the target if exists, and rename the src file to target
4446
// return true on success, false otherwise.
4547
bool rename_file_(const filename_t &src_filename, const filename_t &target_filename);

0 commit comments

Comments
 (0)