fswatch 1.17.1
monitor.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014-2021 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 */
30#ifndef FSW__MONITOR_H
31# define FSW__MONITOR_H
32
33# include "libfswatch/libfswatch_config.h"
34# include "filter.hpp"
35# include <vector>
36# include <string>
37# ifdef HAVE_CXX_MUTEX
38# include <mutex>
39# endif
40# include <atomic>
41# include <chrono>
42# include <map>
43# include "event.hpp"
45
49namespace fsw
50{
61 typedef void FSW_EVENT_CALLBACK(const std::vector<event>&, void *);
62
64
148 {
149 public:
163 monitor(std::vector<std::string> paths,
165 void *context = nullptr);
166
181 virtual ~monitor();
182
186 monitor(const monitor& orig) = delete;
187
191 monitor& operator=(const monitor& that) = delete;
192
201 void set_property(const std::string& name, const std::string& value);
202
211 void set_properties(const std::map<std::string, std::string> options);
212
222 std::string get_property(std::string name);
223
237 void set_latency(double latency);
238
251
264 void set_allow_overflow(bool overflow);
265
280 void set_recursive(bool recursive);
281
299
307 void add_filter(const monitor_filter& filter);
308
317 void set_filters(const std::vector<monitor_filter>& filters);
318
332 void set_follow_symlinks(bool follow);
333
342 void *get_context() const;
343
356 void set_context(void *context);
357
368
390 void start();
391
406 void stop();
407
415 bool is_running();
416
425
435 const std::vector<fsw_event_type_filter>& filters);
436
443 void set_watch_access(bool access);
444
445 protected:
455 bool accept_event_type(fsw_event_flag event_type) const;
456
466 bool accept_path(const std::string& path) const;
467
475 void notify_events(const std::vector<event>& events) const;
476
487 void notify_overflow(const std::string& path) const;
488
498 std::vector<fsw_event_flag> filter_flags(const event& evt) const;
499
514 virtual void run() = 0;
515
526 virtual void on_stop();
527
528 protected:
534 std::vector<std::string> paths;
535
542 std::map<std::string, std::string> properties;
543
550
554 void *context = nullptr;
555
559 double latency = 1.0;
560
566 bool fire_idle_event = false;
567
572 bool allow_overflow = false;
573
577 bool recursive = false;
578
582 bool follow_symlinks = false;
583
587 bool directory_only = false;
588
592 bool watch_access = false;
593
597 bool running = false;
598
602 bool should_stop = false;
603
608 bool bubble_events = false;
609
610# ifdef HAVE_CXX_MUTEX
615 mutable std::mutex run_mutex;
616
620 mutable std::mutex notify_mutex;
621# endif
622
623 private:
624 std::chrono::milliseconds get_latency_ms() const;
625 std::vector<compiled_monitor_filter> filters;
626 std::vector<fsw_event_type_filter> event_type_filters;
627
628#ifdef HAVE_CXX_MUTEX
629# ifdef HAVE_CXX_ATOMIC
630# define HAVE_INACTIVITY_CALLBACK
631 static void inactivity_callback(monitor *mon);
632 mutable std::atomic<std::chrono::milliseconds> last_notification;
633# endif
634#endif
635 };
636}
637
638#endif /* FSW__MONITOR_H */
fsw_event_flag
Backend-agnostic change flags.
Definition: cevent.h:64
Type representing a file change event.
Definition: event.hpp:48
Base class of all monitors.
Definition: monitor.hpp:148
monitor & operator=(const monitor &that)=delete
This class is not copy assignable.
bool is_running()
Check whether the monitor is running.
Definition: monitor.cpp:317
bool directory_only
Flag indicating whether only directories should be monitored.
Definition: monitor.hpp:587
std::vector< std::string > paths
List of paths to watch.
Definition: monitor.hpp:534
std::mutex notify_mutex
Mutex used to serialize access to the notify_events() method.
Definition: monitor.hpp:620
bool allow_overflow
If true, queue overflow events will be notified to the caller, otherwise the monitor will throw a lib...
Definition: monitor.hpp:572
void stop()
Stop the monitor.
Definition: monitor.cpp:304
void add_filter(const monitor_filter &filter)
Add a path filter.
Definition: monitor.cpp:125
void * context
Pointer to context data that will be passed to the monitor::callback.
Definition: monitor.hpp:554
bool fire_idle_event
If true, the monitor will notify an event when idle.
Definition: monitor.hpp:566
void * get_context() const
Get the pointer to the context data.
Definition: monitor.cpp:209
void set_filters(const std::vector< monitor_filter > &filters)
Set the path filters.
Definition: monitor.cpp:162
bool follow_symlinks
If true, symbolic links are followed.
Definition: monitor.hpp:582
void set_latency(double latency)
Sets the latency.
Definition: monitor.cpp:81
bool accept_event_type(fsw_event_flag event_type) const
Check whether an event should be accepted.
Definition: monitor.cpp:180
bool should_stop
Flag indicating whether the monitor should preemptively stop.
Definition: monitor.hpp:602
void set_allow_overflow(bool overflow)
Notify buffer overflows as change events.
Definition: monitor.cpp:76
void notify_overflow(const std::string &path) const
Notify an overflow event.
Definition: monitor.cpp:338
bool watch_access
Flag indicating whether file access should be watched.
Definition: monitor.hpp:592
void set_context(void *context)
Set the context data.
Definition: monitor.cpp:214
virtual void on_stop()
Execute an implementation-specific stop handler.
Definition: monitor.cpp:408
void set_event_type_filters(const std::vector< fsw_event_type_filter > &filters)
Set the event type filters.
Definition: monitor.cpp:118
void set_property(const std::string &name, const std::string &value)
Sets a custom property.
Definition: monitor.cpp:147
void set_bubble_events(bool bubble_events)
Set the bubble events flag.
Definition: monitor.cpp:219
void start()
Start the monitor.
Definition: monitor.cpp:275
void set_follow_symlinks(bool follow)
Follow symlinks.
Definition: monitor.cpp:170
double latency
Latency of the monitor.
Definition: monitor.hpp:559
void set_properties(const std::map< std::string, std::string > options)
Sets the custom properties.
Definition: monitor.cpp:152
std::map< std::string, std::string > properties
Map of custom properties.
Definition: monitor.hpp:542
void set_watch_access(bool access)
Monitor file access.
Definition: monitor.cpp:175
std::vector< fsw_event_flag > filter_flags(const event &evt) const
Filter event types.
Definition: monitor.cpp:323
void set_recursive(bool recursive)
Recursively scan subdirectories.
Definition: monitor.cpp:102
FSW_EVENT_CALLBACK * callback
Callback to which change events should be notified.
Definition: monitor.hpp:549
virtual ~monitor()
Destructs a monitor instance.
Definition: monitor.cpp:224
void set_fire_idle_event(bool fire_idle_event)
Sets the fire idle event flag.
Definition: monitor.cpp:92
monitor(std::vector< std::string > paths, FSW_EVENT_CALLBACK *callback, void *context=nullptr)
Constructs a monitor watching the specified paths.
Definition: monitor.cpp:58
std::string get_property(std::string name)
Gets the value of a property.
Definition: monitor.cpp:157
bool bubble_events
Bubble events by joining flags received for the same (time, path) pair.
Definition: monitor.hpp:608
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:577
bool running
Flag indicating whether the monitor is in the running state.
Definition: monitor.hpp:597
std::mutex run_mutex
Mutex used to serialize access to the monitor state from multiple threads.
Definition: monitor.hpp:615
bool accept_path(const std::string &path) const
Check whether a path should be accepted.
Definition: monitor.cpp:192
void add_event_type_filter(const fsw_event_type_filter &filter)
Add an event type filter.
Definition: monitor.cpp:112
void set_directory_only(bool directory_only)
Watch directories only.
Definition: monitor.cpp:107
void notify_events(const std::vector< event > &events) const
Notify change events.
Definition: monitor.cpp:348
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:61
Definition: monitor.cpp:39
Event type filter.
Definition: cfilter.h:56