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_);
}
}
}

View File

@@ -16,7 +16,8 @@ namespace details {
class SPDLOG_API file_helper
{
public:
explicit file_helper() = default;
file_helper() = default;
explicit file_helper(const file_event_handlers_t& event_handlers);
file_helper(const file_helper &) = delete;
file_helper &operator=(const file_helper &) = delete;
@@ -50,6 +51,7 @@ private:
const unsigned int open_interval_ = 10;
std::FILE *fd_{nullptr};
filename_t filename_;
file_event_handlers_t event_handlers_{nullptr, nullptr, nullptr};
};
} // namespace details
} // namespace spdlog