Skip to content

Commit 2686ae2

Browse files
authored
Merge pull request #1847 from fawdlstty/v1.x
add daily sink filename format
2 parents 23dfb4e + a709e29 commit 2686ae2

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

include/spdlog/sinks/daily_file_sink.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <spdlog/details/file_helper.h>
88
#include <spdlog/details/null_mutex.h>
99
#include <spdlog/fmt/fmt.h>
10+
#include <spdlog/fmt/chrono.h>
1011
#include <spdlog/sinks/base_sink.h>
1112
#include <spdlog/details/os.h>
1213
#include <spdlog/details/circular_q.h>
@@ -36,6 +37,19 @@ struct daily_filename_calculator
3637
}
3738
};
3839

40+
/*
41+
* Generator of daily log file names by formatting
42+
*/
43+
struct daily_filename_format_calculator
44+
{
45+
// Create filename by formatting %Y_%m_%d.log
46+
static filename_t calc_filename (const filename_t &filename, const tm &now_tm)
47+
{
48+
filename_t fmt_filename = fmt::format(SPDLOG_FILENAME_T ("{{:{}}}"), filename);
49+
return fmt::format(fmt_filename, now_tm);
50+
}
51+
};
52+
3953
/*
4054
* Rotating file sink based on date.
4155
* If truncate != false , the created file will be truncated.
@@ -182,6 +196,8 @@ class daily_file_sink final : public base_sink<Mutex>
182196

183197
using daily_file_sink_mt = daily_file_sink<std::mutex>;
184198
using daily_file_sink_st = daily_file_sink<details::null_mutex>;
199+
using daily_file_format_sink_mt = daily_file_sink<std::mutex, daily_filename_format_calculator>;
200+
using daily_file_format_sink_st = daily_file_sink<details::null_mutex, daily_filename_format_calculator>;
185201

186202
} // namespace sinks
187203

@@ -195,10 +211,24 @@ inline std::shared_ptr<logger> daily_logger_mt(
195211
return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files);
196212
}
197213

214+
template<typename Factory = spdlog::synchronous_factory>
215+
inline std::shared_ptr<logger> daily_logger_format_mt(
216+
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0)
217+
{
218+
return Factory::template create<sinks::daily_file_format_sink_mt>(logger_name, filename, hour, minute, truncate, max_files);
219+
}
220+
198221
template<typename Factory = spdlog::synchronous_factory>
199222
inline std::shared_ptr<logger> daily_logger_st(
200223
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0)
201224
{
202225
return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate, max_files);
203226
}
227+
228+
template<typename Factory = spdlog::synchronous_factory>
229+
inline std::shared_ptr<logger> daily_logger_format_st(
230+
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0)
231+
{
232+
return Factory::template create<sinks::daily_file_format_sink_st>(logger_name, filename, hour, minute, truncate, max_files);
233+
}
204234
} // namespace spdlog

0 commit comments

Comments
 (0)