Merge pull request #2169 from seker/v1.x_file_event_handlers
file_event_handlers add before_open function
This commit is contained in:
		@@ -121,7 +121,11 @@ void rotating_example()
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    // Create a file rotating logger with 5mb size max and 3 rotated files.
 | 
					    // Create a file rotating logger with 5mb size max and 3 rotated files.
 | 
				
			||||||
    spdlog::file_event_handlers_t file_event_handlers;
 | 
					    spdlog::file_event_handlers_t file_event_handlers;
 | 
				
			||||||
        file_event_handlers.after_open = [](spdlog::filename_t filename, std::FILE* fstream)
 | 
					    file_event_handlers.before_open = [](spdlog::filename_t filename)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        spdlog::info("basic_example() : file_event_handlers.before_open     : {}", filename.c_str());
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    file_event_handlers.after_open = [](spdlog::filename_t filename, std::FILE* fstream)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        fputs("OPEN!\r\n", fstream);
 | 
					        fputs("OPEN!\r\n", fstream);
 | 
				
			||||||
        spdlog::info("basic_example() : file_event_handlers.after_open      : {}", filename.c_str());
 | 
					        spdlog::info("basic_example() : file_event_handlers.after_open      : {}", filename.c_str());
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -256,11 +256,15 @@ struct source_loc
 | 
				
			|||||||
    const char *funcname{nullptr};
 | 
					    const char *funcname{nullptr};
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct 
 | 
					typedef struct file_event_handlers
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    std::function<void(const filename_t &filename)> before_open;
 | 
				
			||||||
    std::function<void(const filename_t &filename, std::FILE *file_stream)> after_open;
 | 
					    std::function<void(const filename_t &filename, std::FILE *file_stream)> after_open;
 | 
				
			||||||
    std::function<void(const filename_t &filename, std::FILE *file_stream)> before_close;
 | 
					    std::function<void(const filename_t &filename, std::FILE *file_stream)> before_close;
 | 
				
			||||||
    std::function<void(const filename_t &filename)> after_close;
 | 
					    std::function<void(const filename_t &filename)> after_close;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    file_event_handlers(): before_open(nullptr), after_open(nullptr), before_close(nullptr), after_close(nullptr)
 | 
				
			||||||
 | 
					    {}
 | 
				
			||||||
} file_event_handlers_t;
 | 
					} file_event_handlers_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace details {
 | 
					namespace details {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -37,6 +37,10 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate)
 | 
				
			|||||||
    auto *mode = SPDLOG_FILENAME_T("ab");
 | 
					    auto *mode = SPDLOG_FILENAME_T("ab");
 | 
				
			||||||
    auto *trunc_mode = SPDLOG_FILENAME_T("wb");
 | 
					    auto *trunc_mode = SPDLOG_FILENAME_T("wb");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (event_handlers_.before_open)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        event_handlers_.before_open(filename_);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    for (int tries = 0; tries < open_tries_; ++tries)
 | 
					    for (int tries = 0; tries < open_tries_; ++tries)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        // create containing folder if not exists already.
 | 
					        // create containing folder if not exists already.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -51,7 +51,7 @@ private:
 | 
				
			|||||||
    const unsigned int open_interval_ = 10;
 | 
					    const unsigned int open_interval_ = 10;
 | 
				
			||||||
    std::FILE *fd_{nullptr};
 | 
					    std::FILE *fd_{nullptr};
 | 
				
			||||||
    filename_t filename_;
 | 
					    filename_t filename_;
 | 
				
			||||||
    file_event_handlers_t event_handlers_{nullptr, nullptr, nullptr};
 | 
					    file_event_handlers_t event_handlers_;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
} // namespace details
 | 
					} // namespace details
 | 
				
			||||||
} // namespace spdlog
 | 
					} // namespace spdlog
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,7 +20,7 @@ template<typename Mutex>
 | 
				
			|||||||
class basic_file_sink final : public base_sink<Mutex>
 | 
					class basic_file_sink final : public base_sink<Mutex>
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    explicit basic_file_sink(const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr});
 | 
					    explicit basic_file_sink(const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {});
 | 
				
			||||||
    const filename_t &filename() const;
 | 
					    const filename_t &filename() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
protected:
 | 
					protected:
 | 
				
			||||||
@@ -40,13 +40,13 @@ using basic_file_sink_st = basic_file_sink<details::null_mutex>;
 | 
				
			|||||||
// factory functions
 | 
					// factory functions
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
template<typename Factory = spdlog::synchronous_factory>
 | 
					template<typename Factory = spdlog::synchronous_factory>
 | 
				
			||||||
inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr})
 | 
					inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {})
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return Factory::template create<sinks::basic_file_sink_mt>(logger_name, filename, truncate, event_handlers);
 | 
					    return Factory::template create<sinks::basic_file_sink_mt>(logger_name, filename, truncate, event_handlers);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
template<typename Factory = spdlog::synchronous_factory>
 | 
					template<typename Factory = spdlog::synchronous_factory>
 | 
				
			||||||
inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr})
 | 
					inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {})
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return Factory::template create<sinks::basic_file_sink_st>(logger_name, filename, truncate, event_handlers);
 | 
					    return Factory::template create<sinks::basic_file_sink_st>(logger_name, filename, truncate, event_handlers);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -68,7 +68,7 @@ class daily_file_sink final : public base_sink<Mutex>
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    // create daily file sink which rotates on given time
 | 
					    // create daily file sink which rotates on given time
 | 
				
			||||||
    daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr})
 | 
					    daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {})
 | 
				
			||||||
        : base_filename_(std::move(base_filename))
 | 
					        : base_filename_(std::move(base_filename))
 | 
				
			||||||
        , rotation_h_(rotation_hour)
 | 
					        , rotation_h_(rotation_hour)
 | 
				
			||||||
        , rotation_m_(rotation_minute)
 | 
					        , rotation_m_(rotation_minute)
 | 
				
			||||||
@@ -215,28 +215,28 @@ using daily_file_format_sink_st = daily_file_sink<details::null_mutex, daily_fil
 | 
				
			|||||||
//
 | 
					//
 | 
				
			||||||
template<typename Factory = spdlog::synchronous_factory>
 | 
					template<typename Factory = spdlog::synchronous_factory>
 | 
				
			||||||
inline std::shared_ptr<logger> daily_logger_mt(
 | 
					inline std::shared_ptr<logger> daily_logger_mt(
 | 
				
			||||||
    const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr})
 | 
					    const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {})
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
 | 
					    return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
template<typename Factory = spdlog::synchronous_factory>
 | 
					template<typename Factory = spdlog::synchronous_factory>
 | 
				
			||||||
inline std::shared_ptr<logger> daily_logger_format_mt(
 | 
					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, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr})
 | 
					    const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {})
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return Factory::template create<sinks::daily_file_format_sink_mt>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
 | 
					    return Factory::template create<sinks::daily_file_format_sink_mt>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
template<typename Factory = spdlog::synchronous_factory>
 | 
					template<typename Factory = spdlog::synchronous_factory>
 | 
				
			||||||
inline std::shared_ptr<logger> daily_logger_st(
 | 
					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, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr})
 | 
					    const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {})
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
 | 
					    return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
template<typename Factory = spdlog::synchronous_factory>
 | 
					template<typename Factory = spdlog::synchronous_factory>
 | 
				
			||||||
inline std::shared_ptr<logger> daily_logger_format_st(
 | 
					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, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr})
 | 
					    const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {})
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return Factory::template create<sinks::daily_file_format_sink_st>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
 | 
					    return Factory::template create<sinks::daily_file_format_sink_st>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -46,7 +46,7 @@ class hourly_file_sink final : public base_sink<Mutex>
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    // create hourly file sink which rotates on given time
 | 
					    // create hourly file sink which rotates on given time
 | 
				
			||||||
    hourly_file_sink(filename_t base_filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr})
 | 
					    hourly_file_sink(filename_t base_filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {})
 | 
				
			||||||
        : base_filename_(std::move(base_filename))
 | 
					        : base_filename_(std::move(base_filename))
 | 
				
			||||||
        , file_helper_{event_handlers}
 | 
					        , file_helper_{event_handlers}
 | 
				
			||||||
        , truncate_(truncate)
 | 
					        , truncate_(truncate)
 | 
				
			||||||
@@ -181,14 +181,14 @@ using hourly_file_sink_st = hourly_file_sink<details::null_mutex>;
 | 
				
			|||||||
//
 | 
					//
 | 
				
			||||||
template<typename Factory = spdlog::synchronous_factory>
 | 
					template<typename Factory = spdlog::synchronous_factory>
 | 
				
			||||||
inline std::shared_ptr<logger> hourly_logger_mt(
 | 
					inline std::shared_ptr<logger> hourly_logger_mt(
 | 
				
			||||||
    const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr})
 | 
					    const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {})
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return Factory::template create<sinks::hourly_file_sink_mt>(logger_name, filename, truncate, max_files, event_handlers);
 | 
					    return Factory::template create<sinks::hourly_file_sink_mt>(logger_name, filename, truncate, max_files, event_handlers);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
template<typename Factory = spdlog::synchronous_factory>
 | 
					template<typename Factory = spdlog::synchronous_factory>
 | 
				
			||||||
inline std::shared_ptr<logger> hourly_logger_st(
 | 
					inline std::shared_ptr<logger> hourly_logger_st(
 | 
				
			||||||
    const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr})
 | 
					    const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {})
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return Factory::template create<sinks::hourly_file_sink_st>(logger_name, filename, truncate, max_files, event_handlers);
 | 
					    return Factory::template create<sinks::hourly_file_sink_st>(logger_name, filename, truncate, max_files, event_handlers);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,7 +22,7 @@ template<typename Mutex>
 | 
				
			|||||||
class rotating_file_sink final : public base_sink<Mutex>
 | 
					class rotating_file_sink final : public base_sink<Mutex>
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr});
 | 
					    rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {});
 | 
				
			||||||
    static filename_t calc_filename(const filename_t &filename, std::size_t index);
 | 
					    static filename_t calc_filename(const filename_t &filename, std::size_t index);
 | 
				
			||||||
    filename_t filename();
 | 
					    filename_t filename();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -60,14 +60,14 @@ using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
template<typename Factory = spdlog::synchronous_factory>
 | 
					template<typename Factory = spdlog::synchronous_factory>
 | 
				
			||||||
inline std::shared_ptr<logger> rotating_logger_mt(
 | 
					inline std::shared_ptr<logger> rotating_logger_mt(
 | 
				
			||||||
    const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr})
 | 
					    const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {})
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return Factory::template create<sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
 | 
					    return Factory::template create<sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
template<typename Factory = spdlog::synchronous_factory>
 | 
					template<typename Factory = spdlog::synchronous_factory>
 | 
				
			||||||
inline std::shared_ptr<logger> rotating_logger_st(
 | 
					inline std::shared_ptr<logger> rotating_logger_st(
 | 
				
			||||||
    const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr})
 | 
					    const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {})
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return Factory::template create<sinks::rotating_file_sink_st>(logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
 | 
					    return Factory::template create<sinks::rotating_file_sink_st>(logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user