Merge pull request #1847 from fawdlstty/v1.x
add daily sink filename format
This commit is contained in:
		@@ -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 by formatting
 | 
			
		||||
 */
 | 
			
		||||
struct daily_filename_format_calculator
 | 
			
		||||
{
 | 
			
		||||
    // Create filename by formatting %Y_%m_%d.log
 | 
			
		||||
    static filename_t calc_filename (const filename_t &filename, const tm &now_tm)
 | 
			
		||||
    {
 | 
			
		||||
        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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user