1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-01-16 01:37:58 +00:00

Compare commits

...

9 Commits

Author SHA1 Message Date
Gabi Melman
0fac33781d
Update daily_file_sink.h 2021-02-26 12:38:49 +02:00
Gabi Melman
3135b6a33d
Update comment 2021-02-26 12:33:43 +02:00
Gabi Melman
2686ae2322
Merge pull request #1847 from fawdlstty/v1.x
add daily sink filename format
2021-02-26 12:17:29 +02:00
fawdlstty
a709e29586 fix unique mode compile 2021-02-26 10:18:33 +08:00
fawdlstty
dd46579cb4 fix name 2021-02-26 10:17:43 +08:00
fawdlstty
f4b7210e7b remove externs 2021-02-26 09:28:12 +08:00
fawdlstty
05a0b0d7b0 use fmt::chrono and remove is_fmt flags 2021-02-26 09:21:13 +08:00
fawdlstty
c1f4d7506a replace tab 2021-02-25 23:57:37 +08:00
fawdlstty
b6ba0be550 add daily_logger_format_mt and daily_logger_format_st sink 2021-02-25 23:55:19 +08:00

View File

@ -7,6 +7,7 @@
#include <spdlog/details/file_helper.h>
#include <spdlog/details/null_mutex.h>
#include <spdlog/fmt/fmt.h>
#include <spdlog/fmt/chrono.h>
#include <spdlog/sinks/base_sink.h>
#include <spdlog/details/os.h>
#include <spdlog/details/circular_q.h>
@ -36,6 +37,19 @@ struct daily_filename_calculator
}
};
/*
* Generator of daily log file names with strftime format.
*/
struct daily_filename_format_calculator
{
static filename_t calc_filename (const filename_t &filename, const tm &now_tm)
{
// generate fmt datetime format string, e.g. {:%Y-%m-%d}.
filename_t fmt_filename = fmt::format(SPDLOG_FILENAME_T ("{{:{}}}"), filename);
return fmt::format(fmt_filename, now_tm);
}
};
/*
* Rotating file sink based on date.
* If truncate != false , the created file will be truncated.
@ -182,6 +196,8 @@ private:
using daily_file_sink_mt = daily_file_sink<std::mutex>;
using daily_file_sink_st = daily_file_sink<details::null_mutex>;
using daily_file_format_sink_mt = daily_file_sink<std::mutex, daily_filename_format_calculator>;
using daily_file_format_sink_st = daily_file_sink<details::null_mutex, daily_filename_format_calculator>;
} // namespace sinks
@ -195,10 +211,24 @@ inline std::shared_ptr<logger> daily_logger_mt(
return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files);
}
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> daily_logger_format_mt(
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0)
{
return Factory::template create<sinks::daily_file_format_sink_mt>(logger_name, filename, hour, minute, truncate, max_files);
}
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> daily_logger_st(
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0)
{
return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate, max_files);
}
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> daily_logger_format_st(
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0)
{
return Factory::template create<sinks::daily_file_format_sink_st>(logger_name, filename, hour, minute, truncate, max_files);
}
} // namespace spdlog