#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 59 of file FileHelpers.cpp.
61{
63 {
64 throw std::ios_base::failure("Failed to initialize inotify");
65 }
66
69 {
71 throw std::ios_base::failure("Failed to add watch descriptor");
72 }
73
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 {
101 std::cerr << "Failed to remove watch descriptor and also logger thrown an exception: "
103 }
104 }
106 }
107
108
110 {
112 {
113 try
114 {
115 spdlog::error(
"Failed to close file descriptor: {}",
getErrnoString(errno));
116 }
117 catch (const std::exception &e)
118 {
119 std::cerr << "Failed to close file descriptor and also logger thrown an exception: "
121 }
122 }
124 }
125}
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 18 of file FileHelpers.cpp.
19{
20 while (!stopToken.stop_requested())
21 {
22
23 unsigned int nBytes = 0;
25 {
26 spdlog::error(
"Failed to get available events for file monitoring: {}",
getErrnoString(errno));
27 }
28
29 auto buffer = std::vector<char>(nBytes + 1, '\0');
31 if (nRead < 0 && errno != EAGAIN)
32 {
33 spdlog::error(
"Failed to read events for file monitoring: {}",
getErrnoString(errno));
34 }
35 else if (nRead == 0)
36 {
37 spdlog::debug("No events read for file monitoring");
38 }
39
40 ssize_t idx = 0;
42 {
43 const auto *event = std::bit_cast<inotify_event *>(&buffer[static_cast<size_t>(idx)]);
44
45
47 {
49 break;
50 }
51
52 idx += static_cast<ssize_t>(sizeof(inotify_event) + event->len);
53 }
54
56 }
57}
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: