Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Romove the empty file if no log in first period in hourly logger #2386

Merged
merged 1 commit into from
Jun 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/spdlog/sinks/hourly_file_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class hourly_file_sink final : public base_sink<Mutex>
auto now = log_clock::now();
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
file_helper_.open(filename, truncate_);
remove_init_file_ = file_helper_.size() == 0;
rotation_tp_ = next_rotation_tp_();

if (max_files_ > 0)
Expand All @@ -78,10 +79,16 @@ class hourly_file_sink final : public base_sink<Mutex>
bool should_rotate = time >= rotation_tp_;
if (should_rotate)
{
if (remove_init_file_)
{
file_helper_.close();
details::os::remove(file_helper_.filename());
}
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time));
file_helper_.open(filename, truncate_);
rotation_tp_ = next_rotation_tp_();
}
remove_init_file_ = false;
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
file_helper_.write(formatted);
Expand Down Expand Up @@ -170,6 +177,7 @@ class hourly_file_sink final : public base_sink<Mutex>
bool truncate_;
uint16_t max_files_;
details::circular_q<filename_t> filenames_q_;
bool remove_init_file_;
};

using hourly_file_sink_mt = hourly_file_sink<std::mutex>;
Expand Down