Skip to content

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

Open
wants to merge 11 commits into
base: v1.x
Choose a base branch
from
Prev Previous commit
Next Next commit
Review fixes
Fixes as per suggestions
  • Loading branch information
mmanoj authored May 4, 2023
commit 9d8efbde23d0404b56324b4351ccb7bf58a15dc5
31 changes: 20 additions & 11 deletions include/spdlog/sinks/minute_file_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ struct minute_filename_calculator
{
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,
now_tm.tm_mday, now_tm.tm_hour,now_tm.tm_min,ext);//colock details define in os-inl.h
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,
now_tm.tm_mday, now_tm.tm_hour,now_tm.tm_min,ext);
}
};

Expand All @@ -49,15 +49,23 @@ template<typename Mutex, typename FileNameCalc = minute_filename_calculator>
class minute_file_sink final : public base_sink<Mutex>
Copy link
Contributor

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.

{
public:
// create hourly file sink which rotates on given time
// create every minute file sink which rotates on given time
minute_file_sink(
filename_t base_filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers &event_handlers = {})
filename_t base_filename, bool truncate = false, uint16_t max_files = 0,
int rotation_minute = 0, const file_event_handlers &event_handlers = {})
: base_filename_(std::move(base_filename))
, file_helper_{event_handlers}
, truncate_(truncate)
, max_files_(max_files)
,rotation_m_(rotation_minute)
, filenames_q_()
{

if (rotation_minute < 0 || rotation_minute > 59)
{
throw_spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
Copy link
Owner

@gabime gabime May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Why can't it be greater than 59? what would happen if someone wants 90 minutes for example?
  2. should be "minute_file_sink: Invalid rotation_minutes value in ctor"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gabime

Thanks for the feedback and got your point, remove the validation considering the concern.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmanoj Note: In the current rotation implementation, rotation_m_ should not be allowed to be 0.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tt4g
Implement the check for 0 min.

}

auto now = log_clock::now();
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
file_helper_.open(filename, truncate_);
Expand Down Expand Up @@ -125,7 +133,7 @@ class minute_file_sink final : public base_sink<Mutex>
break;
}
filenames.emplace_back(filename);
now -= std::chrono::minutes(1);
now -= std::chrono::minutes(rotation_m_);
}
for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter)
{
Expand All @@ -143,14 +151,14 @@ class minute_file_sink final : public base_sink<Mutex>
{
auto now = log_clock::now();
tm date = now_tm(now);
date.tm_min = 0;

date.tm_sec = 0;
auto rotation_time = log_clock::from_time_t(std::mktime(&date));
if (rotation_time > now)
{
return rotation_time;
}
return {rotation_time + std::chrono::minutes(1)};
return {rotation_time + std::chrono::minutes(rotation_m_)};
}

// Delete the file N rotations ago.
Expand Down Expand Up @@ -180,6 +188,7 @@ class minute_file_sink final : public base_sink<Mutex>
details::file_helper file_helper_;
bool truncate_;
uint16_t max_files_;
int rotation_m_;
details::circular_q<filename_t> filenames_q_;
bool remove_init_file_;
};
Expand All @@ -194,15 +203,15 @@ using minute_file_sink_st = minute_file_sink<details::null_mutex>;
//
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> minute_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false,
uint16_t max_files = 0, const file_event_handlers &event_handlers = {})
uint16_t max_files = 0, int minute = 0, const file_event_handlers &event_handlers = {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint16_t max_files = 0, int minute = 0, const file_event_handlers &event_handlers = {})
uint16_t max_files = 0, int minute = 1, const file_event_handlers &event_handlers = {})

minute = 0 will thrown.

{
return Factory::template create<sinks::minute_file_sink_mt>(logger_name, filename, truncate, max_files, event_handlers);
return Factory::template create<sinks::minute_file_sink_mt>(logger_name, filename, truncate, max_files, minute ,event_handlers);
}

template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> minute_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false,
uint16_t max_files = 0, const file_event_handlers &event_handlers = {})
uint16_t max_files = 0,int minute = 0, const file_event_handlers &event_handlers = {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint16_t max_files = 0,int minute = 0, const file_event_handlers &event_handlers = {})
uint16_t max_files = 0, int minute = 1, const file_event_handlers &event_handlers = {})

minute = 0 will thrown.

{
return Factory::template create<sinks::minute_file_sink_st>(logger_name, filename, truncate, max_files, event_handlers);
return Factory::template create<sinks::minute_file_sink_st>(logger_name, filename, truncate, max_files, minute, event_handlers);
}
} // namespace spdlog
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy