-
Notifications
You must be signed in to change notification settings - Fork 5k
Adding minute_file_sink_mt #2727 #2729
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
base: v1.x
Are you sure you want to change the base?
Changes from 2 commits
5306b1f
a406f27
9d8efbd
464013d
526f938
16a9463
9c14149
715414b
74a4eba
86daaf2
b1c1e4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,208 @@ | ||||||
| // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. | ||||||
| // Distributed under the MIT License (http://opensource.org/licenses/MIT) | ||||||
| //File: minute_file_sink to support every 1 min file rotation. | ||||||
| //Author Manoj Mallawaarachchie [email protected] / [email protected] | ||||||
| //example driver code: | ||||||
| //auto logger = spdlog::minute_logger_mt("minutes_basic_logger", "logs/min-log.txt",false,60); | ||||||
|
|
||||||
| #pragma once | ||||||
|
|
||||||
| #include <spdlog/common.h> | ||||||
| #include <spdlog/details/file_helper.h> | ||||||
| #include <spdlog/details/null_mutex.h> | ||||||
| #include <spdlog/fmt/fmt.h> | ||||||
| #include <spdlog/sinks/base_sink.h> | ||||||
| #include <spdlog/details/os.h> | ||||||
| #include <spdlog/details/circular_q.h> | ||||||
| #include <spdlog/details/synchronous_factory.h> | ||||||
|
|
||||||
| #include <chrono> | ||||||
| #include <cstdio> | ||||||
| #include <ctime> | ||||||
| #include <mutex> | ||||||
| #include <string> | ||||||
|
|
||||||
| namespace spdlog { | ||||||
| namespace sinks { | ||||||
|
|
||||||
| /* | ||||||
| * Generator of Minute log file names in format basename.YYYY-MM-DD-HH-MM.ext | ||||||
| */ | ||||||
| struct minute_filename_calculator | ||||||
| { | ||||||
| // Create filename for the form basename.YYYY-MM-DD-H-M | ||||||
| static filename_t calc_filename(const filename_t &filename, const tm &now_tm) | ||||||
| { | ||||||
| filename_t basename, ext; | ||||||
| std::tie(basename, ext) = details::file_helper::split_by_extension(filename); | ||||||
| return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}-{:02d}_{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, | ||||||
|
||||||
| return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}-{:02d}_{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, | |
| return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}-{:02d}_{:02d}{}")), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, under testing
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| now_tm.tm_mday, now_tm.tm_hour,now_tm.tm_min,ext);//colock details define in os-inl.h | |
| now_tm.tm_mday, now_tm.tm_hour,now_tm.tm_min,ext); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
daily_sink and hourly_sink can change the rotation time, but minute_file_sink seems to have a fixed rotation time. I suggest renaming it to every_minute_file_sink to avoid confusion.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // create hourly file sink which rotates on given time | |
| // create every minute file sink which rotates on given time |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why you would delete an empty log file.
Why is this necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part kept as it is from hourly log sink.
bool should_rotate = time >= rotation_tp_; if (should_rotate) { if (remove_init_file_) { file_helper_.close(); details::os::remove(file_helper_.filename());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow I figured out why this code is there.
Perhaps, the author of sink wanted to clean up the log file created in the constructor, if it was empty, removing it at the time of rotation.
The following scenario is assumed.
- Create log file
log.2023-01-01-12-01.txtin the constructor. - More than one minute passes without a log being recorded.
- The contents of the file to be rotated are empty. Delete it as unnecessary and record log messages in a new log file.
Can you add a comment to this code as I am having difficulty understanding the purpose of this code?
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| date.tm_min = 0; |
The most important rotation feature seems to be broken.
The minute of the next rotation time is always 1 (1 minute every hour).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tt4g
Thanks for suggestions and review,I will add configuration to make X MIN interval to do the rotation as configurable number of minutes.Hope it more flexible.I also go through other changes as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated with configurable rotation interval minutes
auto logger = spdlog::minute_logger_mt("basic_logger", "logs/min-log.txt",false,60,2);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this file as we use the other one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done