11#include <spdlog/spdlog.h>
21 unsigned int nBytes = 0;
24 spdlog::error(
"Failed to get available events for file monitoring: {}",
getErrnoString(errno));
27 auto buffer = std::vector<char>(nBytes + 1,
'\0');
31 spdlog::error(
"Failed to read events for file monitoring: {}",
getErrnoString(errno));
35 spdlog::debug(
"No events read for file monitoring");
41 const auto *
event =
reinterpret_cast<inotify_event *
>(&buffer[
static_cast<size_t>(idx)]);
50 idx +=
static_cast<ssize_t
>(
sizeof(inotify_event) +
event->len);
58 : _fDescriptor(inotify_init()), _filePath(std::move(filePath)), _notifyEvents(notifyEvents)
62 throw std::ios_base::failure(
"Failed to initialize inotify");
69 throw std::ios_base::failure(
"Failed to add watch descriptor");
75 throw std::ios_base::failure(
"Failed to set file descriptor to non-blocking mode");
97 spdlog::error(
"Failed to remove watch descriptor: {}",
getErrnoString(errno));
99 catch (
const std::exception &e)
101 std::cerr <<
"Failed to remove watch descriptor and also logger thrown an exception: "
115 spdlog::error(
"Failed to close file descriptor: {}",
getErrnoString(errno));
117 catch (
const std::exception &e)
119 std::cerr <<
"Failed to close file descriptor and also logger thrown an exception: "
std::string getErrnoString(int errVal)
constexpr int SLEEP_INTERVAL_MS
Sleep interval for the file monitor.
constexpr int SLEEP_INTERVAL_MS
const void * _userPtr
User pointer.
FNotifyCallback _notifyCallback
Callback function.
int _fDescriptor
File descriptor.
void threadFunc() const noexcept
std::atomic_flag _shouldStop
Flag to stop monitoring.
std::unique_ptr< std::thread > _thread
Thread.
FileMonitor(std::filesystem::path filePath, uint32_t notifyEvents=IN_MODIFY)
int _wDescriptor
Watch descriptor.
uint32_t _notifyEvents
Notify types.
std::filesystem::path _filePath
File path.