add file event handlers

This commit is contained in:
seker
2021-11-11 00:08:24 +08:00
parent 6aafa89d20
commit c47ae3b15d
10 changed files with 75 additions and 28 deletions

View File

@@ -20,6 +20,10 @@
namespace spdlog {
namespace details {
SPDLOG_INLINE file_helper::file_helper(const file_event_handlers_t& event_handlers)
: event_handlers_(event_handlers)
{}
SPDLOG_INLINE file_helper::~file_helper()
{
close();
@@ -52,6 +56,10 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate)
}
if (!os::fopen_s(&fd_, fname, mode))
{
if (event_handlers_.after_open)
{
event_handlers_.after_open(filename_, fd_);
}
return;
}
@@ -79,8 +87,18 @@ SPDLOG_INLINE void file_helper::close()
{
if (fd_ != nullptr)
{
if (event_handlers_.before_close)
{
event_handlers_.before_close(filename_, fd_);
}
std::fclose(fd_);
fd_ = nullptr;
if (event_handlers_.after_close)
{
event_handlers_.after_close(filename_);
}
}
}