From 7dd681d0b442e7ecc9c6dd054ff59691a6795807 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sat, 18 Jul 2009 01:23:03 +0200 Subject: inotify-watch: Print timestamp on every event --- inotify-watch.c | 25 ++++++++++++++++++------- 1 file 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 #include #include #include #include +#include #include - +#include + +#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 ...\n", *argv); + fprintf(stderr, "Usage: %s ...\n", *argv); exit(EXIT_FAILURE); } -- cgit v1.2.3-54-g00ecf