libfswatch  1.9.3
monitor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015 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 "filter.hpp"
34 # include <vector>
35 # include <string>
36 # ifdef HAVE_CXX_MUTEX
37 # include <mutex>
38 # endif
39 # include <atomic>
40 # include <chrono>
41 # include <map>
42 # include "event.hpp"
43 # include "../c/cmonitor.h"
44 
48 namespace fsw
49 {
60  typedef void FSW_EVENT_CALLBACK(const std::vector<event>&, void *);
61 
63 
146  class monitor
147  {
148  public:
162  monitor(std::vector<std::string> paths,
164  void *context = nullptr);
165 
180  virtual ~monitor();
181 
185  monitor(const monitor& orig) = delete;
186 
190  monitor& operator=(const monitor& that) = delete;
191 
200  void set_property(const std::string& name, const std::string& value);
201 
210  void set_properties(const std::map<std::string, std::string> options);
211 
221  std::string get_property(std::string name);
222 
236  void set_latency(double latency);
237 
250 
263  void set_allow_overflow(bool overflow);
264 
279  void set_recursive(bool recursive);
280 
298 
306  void add_filter(const monitor_filter& filter);
307 
316  void set_filters(const std::vector<monitor_filter>& filters);
317 
331  void set_follow_symlinks(bool follow);
332 
341  void *get_context() const;
342 
355  void set_context(void *context);
356 
378  void start();
379 
394  void stop();
395 
403  bool is_running();
404 
412  void add_event_type_filter(const fsw_event_type_filter& filter);
413 
423  const std::vector<fsw_event_type_filter>& filters);
424 
431  void set_watch_access(bool access);
432 
433  protected:
443  bool accept_event_type(fsw_event_flag event_type) const;
444 
454  bool accept_path(const std::string& path) const;
455 
465  bool accept_path(const char *path) const;
466 
474  void notify_events(const std::vector<event>& events) const;
475 
486  void notify_overflow(const std::string& path) const;
487 
497  std::vector<fsw_event_flag> filter_flags(const event& evt) const;
498 
513  virtual void run() = 0;
514 
525  virtual void on_stop();
526 
527  protected:
533  std::vector<std::string> paths;
534 
541  std::map<std::string, std::string> properties;
542 
549 
553  void *context = nullptr;
554 
558  double latency = 1.0;
559 
565  bool fire_idle_event = false;
566 
571  bool allow_overflow = false;
572 
576  bool recursive = false;
577 
581  bool follow_symlinks = false;
582 
586  bool directory_only = false;
587 
591  bool watch_access = false;
592 
596  bool running = false;
597 
601  bool should_stop = false;
602 
603 # ifdef HAVE_CXX_MUTEX
604 
608  mutable std::mutex run_mutex;
609 
613  mutable std::mutex notify_mutex;
614 # endif
615 
616  private:
617  std::chrono::milliseconds get_latency_ms() const;
618  std::vector<compiled_monitor_filter> filters;
619  std::vector<fsw_event_type_filter> event_type_filters;
620 
621 #ifdef HAVE_CXX_MUTEX
622 # ifdef HAVE_CXX_ATOMIC
623 # define HAVE_INACTIVITY_CALLBACK
624  static void inactivity_callback(monitor *mon);
625  mutable std::atomic<std::chrono::milliseconds> last_notification;
626 # endif
627 #endif
628  };
629 
630  typedef monitor *(*FSW_FN_MONITOR_CREATOR)(std::vector<std::string> paths,
631  FSW_EVENT_CALLBACK *callback,
632  void *context);
633 
659  {
660  public:
676  std::vector<std::string> paths,
677  FSW_EVENT_CALLBACK *callback,
678  void *context = nullptr);
679 
694  static monitor *create_monitor(const std::string& name,
695  std::vector<std::string> paths,
696  FSW_EVENT_CALLBACK *callback,
697  void *context = nullptr);
698 
704  static std::vector<std::string> get_types();
705 
715  static bool exists_type(const std::string& name);
716 
724  static bool exists_type(const fsw_monitor_type& type);
725 
732  static void register_creator(const std::string& name,
733  FSW_FN_MONITOR_CREATOR creator);
740  static void register_creator_by_type(const fsw_monitor_type& type,
741  FSW_FN_MONITOR_CREATOR creator);
742 
743  monitor_factory() = delete;
744  monitor_factory(const monitor_factory& orig) = delete;
745  monitor_factory& operator=(const monitor_factory& that) = delete;
746  private:
747  static std::map<std::string, FSW_FN_MONITOR_CREATOR>& creators_by_string();
748  static std::map<fsw_monitor_type, FSW_FN_MONITOR_CREATOR>& creators_by_type();
749  };
750 
760  template<class M>
762  {
763  public:
764 
771  monitor_registrant(const std::string& name, const fsw_monitor_type& type)
772  {
773  FSW_FN_MONITOR_CREATOR default_creator =
774  [](std::vector<std::string> paths,
775  FSW_EVENT_CALLBACK *callback,
776  void *context = nullptr) -> monitor *
777  {
778  return new M(paths, callback, context);
779  };
780 
781  monitor_factory::register_creator(name, default_creator);
782  monitor_factory::register_creator_by_type(type, default_creator);
783  }
784  };
785 
810 # define REGISTER_MONITOR(classname, monitor_type) \
811 private: \
812 static const monitor_registrant<classname> monitor_factory_registrant;
813 
831 # define REGISTER_MONITOR_IMPL(classname, monitor_type) \
832 const monitor_registrant<classname> classname::monitor_factory_registrant(#classname, monitor_type);
833 }
834 
835 #endif /* FSW__MONITOR_H */
void set_event_type_filters(const std::vector< fsw_event_type_filter > &filters)
Set the event type filters.
Definition: monitor.cpp:114
void * context
Pointer to context data that will be passed to the monitor::callback.
Definition: monitor.hpp:553
fsw_event_flag
Backend-agnostic change flags.
Definition: cevent.h:63
Base class of all monitors.
Definition: monitor.hpp:146
std::mutex run_mutex
Mutex used to serialize access to the monitor state from multiple threads.
Definition: monitor.hpp:608
Helper class to register monitor factories.
Definition: monitor.hpp:761
void notify_overflow(const std::string &path) const
Notify an overflow event.
Definition: monitor.cpp:377
void start()
Start the monitor.
Definition: monitor.cpp:315
bool allow_overflow
If true, queue overflow events will be notified to the caller, otherwise the monitor will throw a lib...
Definition: monitor.hpp:571
Main namespace of libfswatch.
Definition: event.cpp:23
void set_follow_symlinks(bool follow)
Follow symlinks.
Definition: monitor.cpp:164
bool should_stop
Flag indicating whether the monitor should preemptively stop.
Definition: monitor.hpp:601
monitor & operator=(const monitor &that)=delete
This class is not copy assignable.
bool watch_access
Flag indicating whether file access should be watched.
Definition: monitor.hpp:591
Definition: monitor.cpp:36
Type representing a file change event.
Definition: event.hpp:47
bool accept_path(const std::string &path) const
Check whether a path should be accepted.
void set_filters(const std::vector< monitor_filter > &filters)
Set the path filters.
Definition: monitor.cpp:156
std::mutex notify_mutex
Mutex used to serialize access to the notify_events() method.
Definition: monitor.hpp:613
void set_properties(const std::map< std::string, std::string > options)
Sets the custom properties.
Definition: monitor.cpp:146
void set_directory_only(bool directory_only)
Watch directories only.
Definition: monitor.cpp:104
static monitor * create_monitor(fsw_monitor_type type, std::vector< std::string > paths, FSW_EVENT_CALLBACK *callback, void *context=nullptr)
Creates a monitor of the specified type.
bool recursive
If true, directories will be scanned recursively.
Definition: monitor.hpp:576
void set_allow_overflow(bool overflow)
Notify buffer overflows as change events.
Definition: monitor.cpp:73
Event type filter.
Definition: cfilter.h:55
double latency
Latency of the monitor.
Definition: monitor.hpp:558
void add_filter(const monitor_filter &filter)
Add a path filter.
Definition: monitor.cpp:121
void notify_events(const std::vector< event > &events) const
Notify change events.
Definition: monitor.cpp:387
std::map< std::string, std::string > properties
Map of custom properties.
Definition: monitor.hpp:541
bool follow_symlinks
If true, symbolic links are followed.
Definition: monitor.hpp:581
bool is_running()
Check whether the monitor is running.
Definition: monitor.cpp:356
void set_context(void *context)
Set the context data.
Definition: monitor.cpp:219
void set_watch_access(bool access)
Monitor file access.
Definition: monitor.cpp:169
fsw_monitor_type
Available monitors.
Definition: cmonitor.h:43
std::vector< fsw_event_flag > filter_flags(const event &evt) const
Filter event types.
Definition: monitor.cpp:362
bool fire_idle_event
If true, the monitor will notify an event when idle.
Definition: monitor.hpp:565
static void register_creator(const std::string &name, FSW_FN_MONITOR_CREATOR creator)
Registers a creator for the specified monitor type name.
Definition: monitor.cpp:461
void set_recursive(bool recursive)
Recursively scan subdirectories.
Definition: monitor.cpp:99
Header of the fsw::event class.
std::string get_property(std::string name)
Gets the value of a property.
Definition: monitor.cpp:151
Object factory class for fsw::monitor instances.
Definition: monitor.hpp:658
virtual void run()=0
Execute monitor loop.
virtual void on_stop()
Execute an implementation-specific stop handler.
Definition: monitor.cpp:486
virtual ~monitor()
Destructs a monitor instance.
Definition: monitor.cpp:224
bool accept_event_type(fsw_event_flag event_type) const
Check whether an event should be accepted.
Definition: monitor.cpp:174
std::vector< std::string > paths
List of paths to watch.
Definition: monitor.hpp:533
static void register_creator_by_type(const fsw_monitor_type &type, FSW_FN_MONITOR_CREATOR creator)
Registers a creator for the specified monitor type.
Definition: monitor.cpp:467
static bool exists_type(const std::string &name)
Checks whether a monitor of the type specified by name exists.
void * get_context() const
Get the pointer to the context data.
Definition: monitor.cpp:214
void set_fire_idle_event(bool fire_idle_event)
Sets the fire idle event flag.
Definition: monitor.cpp:89
monitor(std::vector< std::string > paths, FSW_EVENT_CALLBACK *callback, void *context=nullptr)
Constructs a monitor watching the specified paths.
bool directory_only
Flag indicating whether only directories should be monitored.
Definition: monitor.hpp:586
void set_latency(double latency)
Sets the latency.
Definition: monitor.cpp:78
FSW_EVENT_CALLBACK * callback
Callback to which change events should be notified.
Definition: monitor.hpp:548
static std::vector< std::string > get_types()
Get the available monitor types.
Definition: monitor.cpp:474
void stop()
Stop the monitor.
Definition: monitor.cpp:343
void FSW_EVENT_CALLBACK(const std::vector< event > &, void *)
Function definition of an event callback.
Definition: monitor.hpp:60
Header of the fsw::monitor_filter class.
Path filters used to accept or reject file change events.
Definition: filter.hpp:57
void set_property(const std::string &name, const std::string &value)
Sets a custom property.
Definition: monitor.cpp:141
bool running
Flag indicating whether the monitor is in the running state.
Definition: monitor.hpp:596
void add_event_type_filter(const fsw_event_type_filter &filter)
Add an event type filter.
Definition: monitor.cpp:109
monitor_registrant(const std::string &name, const fsw_monitor_type &type)
Constructs a monitor registrant for the specified type.
Definition: monitor.hpp:771