fswatch 1.21.0
Loading...
Searching...
No Matches
monitor.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014-2022 Enrico M. Crisostomo
3 *
4 * This program is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation; either version 3, or (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 * details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
15 */
29#ifndef FSW__MONITOR_H
30# define FSW__MONITOR_H
31
32# include "filter.hpp"
33# include <vector>
34# include <string>
35# include <mutex>
36# include <atomic>
37# include <chrono>
38# include <map>
39# include <regex>
40# include "event.hpp"
42
46namespace fsw
47{
58 typedef void FSW_EVENT_CALLBACK(const std::vector<event>&, void *);
59
61
142 {
143 public:
157 monitor(std::vector<std::string> paths,
159 void *context = nullptr);
160
175 virtual ~monitor();
176
180 monitor(const monitor& orig) = delete;
181
185 monitor& operator=(const monitor& that) = delete;
186
195 void set_property(const std::string& name, const std::string& value);
196
205 void set_properties(const std::map<std::string, std::string> options);
206
216 std::string get_property(std::string name);
217
231 void set_latency(double latency);
232
245
258 void set_allow_overflow(bool overflow);
259
274 void set_recursive(bool recursive);
275
293
301 void add_filter(const monitor_filter& filter);
302
311 void set_filters(const std::vector<monitor_filter>& filters);
312
325
337 void add_prune_filter(const monitor_filter& filter);
338
347 void set_prune_filters(const std::vector<monitor_filter>& filters);
348
362 void set_follow_symlinks(bool follow);
363
372 void *get_context() const;
373
386 void set_context(void *context);
387
398
420 void start();
421
436 void stop();
437
445 bool is_running();
446
455
465 const std::vector<fsw_event_type_filter>& filters);
466
473 void set_watch_access(bool access);
474
475 protected:
485 bool accept_event_type(fsw_event_flag event_type) const;
486
496 bool accept_path(const std::string& path) const;
497
511 bool should_prune_path(const std::string& path,
512 bool is_dir,
513 bool is_root_path) const;
514
522 void notify_events(const std::vector<event>& events) const;
523
534 void notify_overflow(const std::string& path) const;
535
545 std::vector<fsw_event_flag> filter_flags(const event& evt) const;
546
561 virtual void run() = 0;
562
573 virtual void on_stop();
574
575 protected:
581 std::vector<std::string> paths;
582
589 std::map<std::string, std::string> properties;
590
597
601 void *context = nullptr;
602
606 double latency = 1.0;
607
613 bool fire_idle_event = false;
614
619 bool allow_overflow = false;
620
624 bool recursive = false;
625
629 bool follow_symlinks = false;
630
634 bool directory_only = false;
635
639 bool watch_access = false;
640
644 bool running = false;
645
649 bool should_stop = false;
650
655 bool bubble_events = false;
656
661 mutable std::mutex run_mutex;
662
666 mutable std::mutex notify_mutex;
667
668 private:
669 std::chrono::milliseconds get_latency_ms() const;
670 std::vector<compiled_monitor_filter> filters;
671 std::vector<std::regex> prune_filters;
672 std::vector<fsw_event_type_filter> event_type_filters;
673 fsw_filter_mode filter_mode = fsw_filter_mode::filter_mode_legacy;
674
675 static void inactivity_callback(monitor *mon);
676 mutable std::atomic<std::chrono::milliseconds> last_notification;
677 };
678}
679
680#endif /* FSW__MONITOR_H */
fsw_event_flag
Backend-agnostic change flags.
Definition cevent.h:66
fsw_filter_mode
Path filter evaluation mode.
Definition cfilter.h:53
Type representing a file change event.
Definition event.hpp:65
Base class of all monitors.
Definition monitor.hpp:142
monitor & operator=(const monitor &that)=delete
This class is not copy assignable.
bool is_running()
Check whether the monitor is running.
Definition monitor.cpp:381
bool directory_only
Flag indicating whether only directories should be monitored.
Definition monitor.hpp:634
std::vector< std::string > paths
List of paths to watch.
Definition monitor.hpp:581
std::mutex notify_mutex
Mutex used to serialize access to the notify_events() method.
Definition monitor.hpp:666
bool allow_overflow
If true, queue overflow events will be notified to the caller, otherwise the monitor will throw a lib...
Definition monitor.hpp:619
void stop()
Stop the monitor.
Definition monitor.cpp:368
void add_filter(const monitor_filter &filter)
Add a path filter.
Definition monitor.cpp:115
void set_filter_mode(fsw_filter_mode mode)
Set the path filter evaluation mode.
Definition monitor.cpp:193
void * context
Pointer to context data that will be passed to the monitor::callback.
Definition monitor.hpp:601
bool fire_idle_event
If true, the monitor will notify an event when idle.
Definition monitor.hpp:613
void * get_context() const
Get the pointer to the context data.
Definition monitor.cpp:278
void set_filters(const std::vector< monitor_filter > &filters)
Set the path filters.
Definition monitor.cpp:173
bool follow_symlinks
If true, symbolic links are followed.
Definition monitor.hpp:629
void set_latency(double latency)
Sets the latency.
Definition monitor.cpp:71
bool accept_event_type(fsw_event_flag event_type) const
Check whether an event should be accepted.
Definition monitor.cpp:214
bool should_stop
Flag indicating whether the monitor should preemptively stop.
Definition monitor.hpp:649
void set_allow_overflow(bool overflow)
Notify buffer overflows as change events.
Definition monitor.cpp:66
void notify_overflow(const std::string &path) const
Notify an overflow event.
Definition monitor.cpp:402
bool watch_access
Flag indicating whether file access should be watched.
Definition monitor.hpp:639
void set_context(void *context)
Set the context data.
Definition monitor.cpp:283
virtual void on_stop()
Execute an implementation-specific stop handler.
Definition monitor.cpp:491
void set_event_type_filters(const std::vector< fsw_event_type_filter > &filters)
Set the event type filters.
Definition monitor.cpp:108
void set_property(const std::string &name, const std::string &value)
Sets a custom property.
Definition monitor.cpp:158
void set_bubble_events(bool bubble_events)
Set the bubble events flag.
Definition monitor.cpp:288
void start()
Start the monitor.
Definition monitor.cpp:340
void set_follow_symlinks(bool follow)
Follow symlinks.
Definition monitor.cpp:204
void add_prune_filter(const monitor_filter &filter)
Add a traversal prune filter.
Definition monitor.cpp:137
double latency
Latency of the monitor.
Definition monitor.hpp:606
void set_properties(const std::map< std::string, std::string > options)
Sets the custom properties.
Definition monitor.cpp:163
std::map< std::string, std::string > properties
Map of custom properties.
Definition monitor.hpp:589
void set_watch_access(bool access)
Monitor file access.
Definition monitor.cpp:209
std::vector< fsw_event_flag > filter_flags(const event &evt) const
Filter event types.
Definition monitor.cpp:387
bool should_prune_path(const std::string &path, bool is_dir, bool is_root_path) const
Check whether a directory should be pruned from recursive scans.
Definition monitor.cpp:266
void set_recursive(bool recursive)
Recursively scan subdirectories.
Definition monitor.cpp:92
FSW_EVENT_CALLBACK * callback
Callback to which change events should be notified.
Definition monitor.hpp:596
void set_prune_filters(const std::vector< monitor_filter > &filters)
Set the traversal prune filters.
Definition monitor.cpp:183
virtual ~monitor()
Destructs a monitor instance.
Definition monitor.cpp:293
void set_fire_idle_event(bool fire_idle_event)
Sets the fire idle event flag.
Definition monitor.cpp:82
std::string get_property(std::string name)
Gets the value of a property.
Definition monitor.cpp:168
bool bubble_events
Bubble events by joining flags received for the same (time, path) pair.
Definition monitor.hpp:655
monitor(const monitor &orig)=delete
This class is not copy constructible.
virtual void run()=0
Execute monitor loop.
bool recursive
If true, directories will be scanned recursively.
Definition monitor.hpp:624
bool running
Flag indicating whether the monitor is in the running state.
Definition monitor.hpp:644
std::mutex run_mutex
Mutex used to serialize access to the monitor state from multiple threads.
Definition monitor.hpp:661
bool accept_path(const std::string &path) const
Check whether a path should be accepted.
Definition monitor.cpp:226
void add_event_type_filter(const fsw_event_type_filter &filter)
Add an event type filter.
Definition monitor.cpp:102
void set_directory_only(bool directory_only)
Watch directories only.
Definition monitor.cpp:97
void notify_events(const std::vector< event > &events) const
Notify change events.
Definition monitor.cpp:412
Header of the libfswatch library defining the monitor types.
Header of the fsw::event class.
Header of the fsw::monitor_filter class.
Main namespace of libfswatch.
Definition event.cpp:24
monitor_filter { std::string text monitor_filter
Path filters used to accept or reject file change events.
Definition filter.hpp:67
void FSW_EVENT_CALLBACK(const std::vector< event > &, void *)
Function definition of an event callback.
Definition monitor.hpp:58
Definition monitor.cpp:40
Event type filter.
Definition cfilter.h:70