@@ -69,12 +69,12 @@ SPDLOG_INLINE filename_t lazy_rotating_file_sink<Mutex>::filename()
69
69
template <typename Mutex>
70
70
SPDLOG_INLINE void lazy_rotating_file_sink<Mutex>::sink_it_(const details::log_msg &msg)
71
71
{
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 ;
78
78
}
79
79
80
80
memory_buf_t formatted;
@@ -93,8 +93,12 @@ SPDLOG_INLINE void lazy_rotating_file_sink<Mutex>::sink_it_(const details::log_m
93
93
new_size = formatted.size ();
94
94
}
95
95
}
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
+ }
98
102
}
99
103
100
104
template <typename Mutex>
@@ -117,6 +121,7 @@ SPDLOG_INLINE void lazy_rotating_file_sink<Mutex>::rotate_()
117
121
using details::os::filename_to_str;
118
122
using details::os::path_exists;
119
123
124
+ file_opened_ = false ;
120
125
file_helper_.close ();
121
126
for (auto i = max_files_; i > 0 ; --i)
122
127
{
@@ -137,11 +142,24 @@ SPDLOG_INLINE void lazy_rotating_file_sink<Mutex>::rotate_()
137
142
{
138
143
file_helper_.reopen (true ); // truncate the log file anyway to prevent it to grow beyond its limit!
139
144
current_size_ = 0 ;
145
+ file_opened_ = true ;
140
146
throw_spdlog_ex (" lazy_rotating_file_sink: failed renaming " + filename_to_str (src) + " to " + filename_to_str (target), errno);
141
147
}
142
148
}
143
149
}
144
150
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
+ }
145
163
}
146
164
147
165
// delete the target if exists, and rename the src file to target
0 commit comments