{"id":129,"date":"2014-03-18T03:56:23","date_gmt":"2014-03-18T07:56:23","guid":{"rendered":"https:\/\/rebrik.info\/blog\/?p=129"},"modified":"2014-03-18T04:23:12","modified_gmt":"2014-03-18T08:23:12","slug":"cp_dir_watcher-c-linux-library-for-monitoring-files-in-a-directory","status":"publish","type":"post","link":"https:\/\/rebrik.info\/blog\/?p=129","title":{"rendered":"cp_dir_watcher: C++ Linux library for monitoring files in a directory"},"content":{"rendered":"<p>I&#8217;ve posted C++ code that I wrote a while ago to monitor files in a directory:<br \/>\n<a href=\"https:\/\/github.com\/rebrik\/cp_dir_watcher\">https:\/\/github.com\/rebrik\/cp_dir_watcher<\/a><\/p>\n<p>Below is the\u00a0 README:<br \/>\ncp_dir_watcher is a C++ Linux library designed to monitor changes to files in a directory. It is released under Boost Software License: <a href=\"http:\/\/www.boost.org\/LICENSE_1_0.txt\">http:\/\/www.boost.org\/LICENSE_1_0.txt<\/a><\/p>\n<p>Possible usage scenario: a server is writing logs to a dir, and you want to make sure that the logs are constantly updated. So you write an app (using this project) which receives and processes a timeout event that is generated when nothing happens to a file or a dir for a certain time. Another scenario is to use provided classes to build a real-time parser which processes new data in the log files as soon as they are written to.<\/p>\n<p>The library consists of two classes: dir_watcher_t and file_watcher_t. Users should derive their own classes from these two classes and provide necessary functionality in the virtual callback functions defined in the base classes.<\/p>\n<p>Library comes with an example application: test00. It contains classes dir_watcher_tst_t and file_watcher_tst_t which inherit from dir_watcher_t and file_watcher_t. The test is watching the dir &#8220;.\/delme\/&#8221; and 3 files in it: f1.txt, f2.txt, f3.txt and reports events to stdout.\u00a0 Try creating, editing, deleting, renaming these files and see the reported events. You can delete or rename the dir itself while the program is running, but the dir must be present when the program starts.<\/p>\n<p>Two system libraries are used: <strong><span style=\"font-family: terminal,monaco;\">libev<\/span><\/strong> and <strong><span style=\"font-family: terminal,monaco;\">inotify<\/span><\/strong>: <strong><span style=\"font-family: terminal,monaco;\">libev<\/span><\/strong> is used to implement the main loop and <strong><span style=\"font-family: terminal,monaco;\">inotify<\/span><\/strong> reports filesystem events. These events are received by the main loop and then dispatched to the appropriate callbacks.<\/p>\n<p>To build the project open a terminal in the &#8220;test00&#8221; dir and run a command:<\/p>\n<p>make CONF=Release<\/p>\n<p>You can also use &#8220;make CONF=Debug&#8221;, &#8220;make CONF=Release clean&#8221; or just open the project in NetBeans<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve posted C++ code that I wrote a while ago to monitor files in a directory: https:\/\/github.com\/rebrik\/cp_dir_watcher Below is the\u00a0 README: cp_dir_watcher is a C++ Linux library designed to monitor changes to files in a directory. It is released under Boost Software License: http:\/\/www.boost.org\/LICENSE_1_0.txt Possible usage scenario: a server is writing logs to a dir, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,4],"tags":[10,9,8,7,6],"_links":{"self":[{"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/129"}],"collection":[{"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=129"}],"version-history":[{"count":7,"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":137,"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions\/137"}],"wp:attachment":[{"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}