Help output with -h switch. Print time with each logged line. Write initial status to stderr removing noise from stdout. Changed inner loop sleep cadence.
parent
0a449e90f6
commit
bf50f10f0e
43
main.cpp
43
main.cpp
|
@ -9,6 +9,7 @@
|
|||
#include <thread>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <ctime>
|
||||
#include "efsw/efsw.hpp"
|
||||
#include "parg.h"
|
||||
|
||||
|
@ -17,6 +18,14 @@ const char * VERSION_STRING = "0.0.1";
|
|||
|
||||
class UpdateListener : public efsw::FileWatchListener
|
||||
{
|
||||
std::string getGmTime () {
|
||||
char tstr[100];
|
||||
time_t now;
|
||||
time(&now);
|
||||
std::strftime(tstr, sizeof(tstr), "%FT%TZ", std::gmtime(&now));
|
||||
return tstr;
|
||||
};
|
||||
|
||||
public:
|
||||
UpdateListener() {}
|
||||
|
||||
|
@ -25,16 +34,16 @@ public:
|
|||
switch( action )
|
||||
{
|
||||
case efsw::Actions::Add:
|
||||
std::cout << "DIR (" << dir << ") FILE (" << filename << ") has event Added" << '\n';
|
||||
std::cout << getGmTime() << " DIR (" << dir << ") FILE (" << filename << ") has event Added" << '\n';
|
||||
break;
|
||||
case efsw::Actions::Delete:
|
||||
std::cout << "DIR (" << dir << ") FILE (" << filename << ") has event Delete" << '\n';
|
||||
std::cout << getGmTime() << " DIR (" << dir << ") FILE (" << filename << ") has event Delete" << '\n';
|
||||
break;
|
||||
case efsw::Actions::Modified:
|
||||
std::cout << "DIR (" << dir << ") FILE (" << filename << ") has event Modified" << '\n';
|
||||
std::cout << getGmTime() << " DIR (" << dir << ") FILE (" << filename << ") has event Modified" << '\n';
|
||||
break;
|
||||
case efsw::Actions::Moved:
|
||||
std::cout << "DIR (" << dir << ") FILE (" << filename << ") has event Moved from (" << oldFilename << ")" << '\n';
|
||||
std::cout << getGmTime() << " DIR (" << dir << ") FILE (" << filename << ") has event Moved from (" << oldFilename << ")" << '\n';
|
||||
break;
|
||||
default:
|
||||
std::cout << "Should never happen!" << '\n';
|
||||
|
@ -50,21 +59,26 @@ main(int argc, char *argv[]) {
|
|||
struct parg_state ps;
|
||||
parg_init(&ps);
|
||||
int c = 0;
|
||||
bool showHelp = false;
|
||||
|
||||
vector<string> watch_inode;
|
||||
|
||||
while (-1 != (c = parg_getopt(&ps, argc, argv, "f:d:v"))) {
|
||||
while (-1 != (c = parg_getopt(&ps, argc, argv, "f:d:v:h"))) {
|
||||
switch (c) {
|
||||
case 1:
|
||||
printf("nonoption '%s'\n", ps.optarg);
|
||||
showHelp = true;
|
||||
break;
|
||||
case 'f':
|
||||
case 'd':
|
||||
if (NULL == ps.optarg) break;
|
||||
watch_inode.push_back(std::string(ps.optarg));
|
||||
break;
|
||||
case 'h':
|
||||
showHelp = true;
|
||||
break;
|
||||
case 'v':
|
||||
printf("Version: %s", VERSION_STRING);
|
||||
printf("Version: %s \n", VERSION_STRING);
|
||||
return 0;
|
||||
break;
|
||||
default:
|
||||
|
@ -73,6 +87,19 @@ main(int argc, char *argv[]) {
|
|||
}
|
||||
|
||||
|
||||
if (showHelp) {
|
||||
cout << R"(fscanary usage:
|
||||
fscanary.exe [options]
|
||||
-f <path to file> watch change events on specified file
|
||||
-d <path to directory> watch for filesystem events on directory and child objects
|
||||
-h show this help dialog
|
||||
|
||||
note: multiple file and directory paths can be specified
|
||||
)" << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
auto fwatcher = new efsw::FileWatcher();
|
||||
auto listener = new UpdateListener();
|
||||
vector<efsw::WatchID> watchers;
|
||||
|
@ -82,7 +109,7 @@ main(int argc, char *argv[]) {
|
|||
// It will watch the /tmp folder recursively ( the third parameter indicates that is recursive )
|
||||
// Reporting the files and directories changes to the instance of the listener
|
||||
for (auto f : watch_inode) {
|
||||
printf("Watch: %s\n", f.c_str());
|
||||
fprintf(stderr, "Watch: %s\n", f.c_str());
|
||||
watchers.push_back(fwatcher->addWatch(f, listener, true));
|
||||
}
|
||||
|
||||
|
@ -90,7 +117,7 @@ main(int argc, char *argv[]) {
|
|||
fwatcher->watch();
|
||||
|
||||
while (true) {
|
||||
std::this_thread::sleep_for(std::chrono::nanoseconds(100));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Reference in New Issue