-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cc
110 lines (90 loc) · 2.87 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <efsw/efsw.hpp>
#include <filesystem>
#include <iostream>
#include <signal.h>
#include <thread>
bool STOP = false;
void sigend(int)
{
std::cout << std::endl << "Bye bye" << std::endl;
STOP = true;
}
std::string getActionName(efsw::Action action)
{
switch (action) {
case efsw::Actions::Add: return "Add";
case efsw::Actions::Modified: return "Modified";
case efsw::Actions::Delete: return "Delete";
case efsw::Actions::Moved: return "Moved";
default: break;
}
return "Bad Action";
}
efsw::WatchID handleWatchID(efsw::WatchID watchid)
{
switch (watchid) {
case efsw::Errors::FileNotFound:
case efsw::Errors::FileRepeated:
case efsw::Errors::FileOutOfScope:
case efsw::Errors::FileRemote:
case efsw::Errors::WatcherFailed:
case efsw::Errors::Unspecified: {
std::cout << efsw::Errors::Log::getLastErrorLog().c_str() << std::endl;
break;
}
default: {
std::cout << "Added WatchID: " << watchid << std::endl;
}
}
return watchid;
}
/// Processes a file action
class UpdateListener : public efsw::FileWatchListener
{
public:
UpdateListener() {}
void handleFileAction(efsw::WatchID watchid,
const std::string &dir,
const std::string &filename,
efsw::Action action,
std::string oldFilename = "") override
{
std::cout << "Watch ID " << watchid << " DIR ("
<< dir + ") FILE ("
+ (oldFilename.empty() ? "" : "from file " + oldFilename + " to ")
+ filename + ") has event "
<< getActionName(action) << std::endl;
}
};
int main(int argc, char **argv)
{
signal(SIGABRT, sigend);
signal(SIGINT, sigend);
signal(SIGTERM, sigend);
std::cout << "Press ^C to exit demo" << std::endl;
bool useGeneric = false;
auto filepath = std::filesystem::current_path().parent_path().parent_path();
std::cout << filepath << std::endl;
auto *ul = new UpdateListener;
/// create the file watcher object
efsw::FileWatcher fileWatcher(useGeneric);
fileWatcher.followSymlinks(false);
fileWatcher.allowOutOfScopeLinks(false);
/// starts watching
fileWatcher.watch();
/// add a watch to the system
handleWatchID(fileWatcher.addWatch((filepath / "bin-64").string(), ul, true));
/// adds another watch after started watching...
std::this_thread::sleep_for(std::chrono::seconds(1));
efsw::WatchID watchID = handleWatchID(
fileWatcher.addWatch((filepath / "build").string(), ul, true));
/// delete the watch
if (watchID > 0) {
std::this_thread::sleep_for(std::chrono::seconds(1));
fileWatcher.removeWatch(watchID);
}
while (!STOP) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}