diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2009-07-18 01:23:03 +0200 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2009-07-18 01:23:03 +0200 |
commit | 7dd681d0b442e7ecc9c6dd054ff59691a6795807 (patch) | |
tree | ae51afe45291c47dd9e6878b70541a5d5e30d90a | |
parent | 9aa1ee91b874409668455997e46871a49dcbc7a3 (diff) |
-rw-r--r-- | inotify-watch.c | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/inotify-watch.c b/inotify-watch.c index 9196c1f..b6d8c82 100644 --- a/inotify-watch.c +++ b/inotify-watch.c @@ -8,23 +8,34 @@ * All rights reserved. */ +#define _GNU_SOURCE + #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <time.h> #include <sys/inotify.h> - +#include <sys/time.h> + +#define log(fmt, args...) do { \ + struct timeval now; \ + char buf[sizeof("XX:XX:XX")]; \ + if (gettimeofday(&now, NULL) == 0 && \ + strftime(buf, sizeof(buf), "%T", localtime(&now.tv_sec)) != 0) { \ + fprintf(stdout, "%s.%06lu " fmt, buf, now.tv_usec, ##args); \ + } } while(0) +#define log_cont(fmt, args...) fprintf(stdout, fmt, ##args) #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x)) +#define DECLARE_M2STR(x) { x, #x } struct mask2string { uint32_t mask; const char *desc; }; -#define DECLARE_M2STR(x) { x, #x } - static const struct mask2string events[] = { DECLARE_M2STR(IN_ACCESS), DECLARE_M2STR(IN_MODIFY), @@ -53,16 +64,16 @@ static void inotify_print_event(struct inotify_event *inev) unsigned int i; /* stat? */ - printf("(%s) wd=%04x, cookie=%04x, len=%04x, name=\"%s\" :", + log("(%s) wd=%04x, cookie=%04x, len=%04x, name=\"%s\" :", inev->mask & IN_ISDIR ? "dir" : "file", inev->wd, inev->cookie, inev->len, inev->len > 0 ? inev->name : ""); for (i = 0; i < ARRAY_SIZE(events); i++) if (inev->mask & events[i].mask) - printf(" %s", events[i].desc); + log_cont(" %s", events[i].desc); - printf("\n"); + log_cont("\n"); } int main(int argc, char *argv[]) @@ -73,7 +84,7 @@ int main(int argc, char *argv[]) char buf[1024]; if (argc < 2) { - printf("Usage: %s <path> ...\n", *argv); + fprintf(stderr, "Usage: %s <path> ...\n", *argv); exit(EXIT_FAILURE); } |