#include <FileHelpers.hpp>
|
| void | threadFunc (const std::stop_token &stopToken) const noexcept |
| |
Invokes functions for a file for given notify events
Definition at line 65 of file FileHelpers.hpp.
◆ FileMonitor() [1/3]
| FileMonitor::FileMonitor |
( |
std::filesystem::path | filePath, |
|
|
uint32_t | notifyEvents = IN_MODIFY ) |
|
explicit |
Constructor
- Parameters
-
| [in] | filePath | Path to the file |
| [in] | notifyEvents | Events to notify |
Definition at line 58 of file FileHelpers.cpp.
60{
62 {
63 throw std::ios_base::failure("Failed to initialize inotify");
64 }
65
68 {
70 throw std::ios_base::failure("Failed to add watch descriptor");
71 }
72
74 flags < 0 || fcntl(
_fDescriptor, F_SETFL, flags | O_NONBLOCK) < 0)
75 {
77 throw std::ios_base::failure("Failed to set file descriptor to non-blocking mode");
78 }
79
80 _thread = std::make_unique<std::jthread>([
this](
const std::stop_token &sToken) {
threadFunc(sToken); });
81}
void threadFunc(const std::stop_token &stopToken) const noexcept
int _fDescriptor
File descriptor.
int _wDescriptor
Watch descriptor.
uint32_t _notifyEvents
Notify types.
std::filesystem::path _filePath
File path.
std::unique_ptr< std::jthread > _thread
Thread.
◆ FileMonitor() [2/3]
◆ FileMonitor() [3/3]
◆ ~FileMonitor()
| FileMonitor::~FileMonitor |
( |
| ) |
|
Destructor
Definition at line 83 of file FileHelpers.cpp.
84{
86 {
88 }
89
90
92 {
94 {
95 try
96 {
97 spdlog::error(
"Failed to remove watch descriptor: {}",
getErrnoString(errno));
98 }
99 catch (const std::exception &e)
100 {
102 (void)std::fprintf(stderr,
103 "Failed to remove watch descriptor and also logger thrown an exception: %s %s\n",
104 errStr.c_str(), e.what());
105 }
106 }
108 }
109
110
112 {
114 {
115 try
116 {
117 spdlog::error(
"Failed to close file descriptor: {}",
getErrnoString(errno));
118 }
119 catch (const std::exception &e)
120 {
122 (void)std::fprintf(stderr,
123 "Failed to close file descriptor and also logger thrown an exception: %s %s\n",
124 errStr.c_str(), e.what());
125 }
126 }
128 }
129}
std::string getErrnoString(int errVal)
◆ notifyCallback() [1/2]
Definition at line 105 of file FileHelpers.hpp.
FNotifyCallback _notifyCallback
Callback function.
◆ notifyCallback() [2/2]
◆ operator=() [1/2]
Move assignment operator.
◆ operator=() [2/2]
Copy assignment operator.
◆ threadFunc()
| void FileMonitor::threadFunc |
( |
const std::stop_token & | stopToken | ) |
const |
|
privatenoexcept |
Definition at line 17 of file FileHelpers.cpp.
18{
19 while (!stopToken.stop_requested())
20 {
21
22 unsigned int nBytes = 0;
24 {
25 spdlog::error(
"Failed to get available events for file monitoring: {}",
getErrnoString(errno));
26 }
27
28 auto buffer = std::vector<char>(nBytes + 1, '\0');
30 if (nRead < 0 && errno != EAGAIN)
31 {
32 spdlog::error(
"Failed to read events for file monitoring: {}",
getErrnoString(errno));
33 }
34 else if (nRead == 0)
35 {
36 spdlog::debug("No events read for file monitoring");
37 }
38
39 ssize_t idx = 0;
41 {
42 const auto *event = std::bit_cast<inotify_event *>(&buffer[static_cast<size_t>(idx)]);
43
44
46 {
48 break;
49 }
50
51 idx += static_cast<ssize_t>(sizeof(inotify_event) + event->len);
52 }
53
55 }
56}
constexpr int SLEEP_INTERVAL_MS
const void * _userPtr
User pointer.
◆ userPtr()
| void FileMonitor::userPtr |
( |
const void * | ptr | ) |
|
|
inline |
◆ _fDescriptor
| int FileMonitor::_fDescriptor {-1} |
|
private |
◆ _filePath
| std::filesystem::path FileMonitor::_filePath |
|
private |
◆ _notifyCallback
◆ _notifyEvents
| uint32_t FileMonitor::_notifyEvents |
|
private |
◆ _thread
| std::unique_ptr<std::jthread> FileMonitor::_thread |
|
private |
◆ _userPtr
| const void* FileMonitor::_userPtr = nullptr |
|
private |
◆ _wDescriptor
| int FileMonitor::_wDescriptor {-1} |
|
private |
The documentation for this class was generated from the following files: