From 4007862e9f521cefed577e68bf9fc5df9b28085e Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 19 Jun 2015 14:30:24 +0200 Subject: initial support for throttling --- journal-notify.c | 23 ++++++++++++++++++++--- journal-notify.h | 1 + 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/journal-notify.c b/journal-notify.c index 2be80ce..cdbcac0 100644 --- a/journal-notify.c +++ b/journal-notify.c @@ -113,6 +113,9 @@ int main(int argc, char **argv) { pid_t child_pid, wpid; int status; + struct timeval tv_last = (struct timeval){ 0 }, tv_now = (struct timeval){ 0 }; + unsigned int notification_count = 0; + program = argv[0]; /* get command line options - part I @@ -159,7 +162,7 @@ int main(int argc, char **argv) { /* reinitialize getopt() by resetting optind to 0 */ optind = 0; - /* get command line options - part II*/ + /* get command line options - part II */ while ((i = getopt_long(argc, argv, optstring, options_long, NULL)) != -1) { switch (i) { case 'a': @@ -237,6 +240,7 @@ int main(int argc, char **argv) { } } + /* main loop */ while (1) { if ((rc = sd_journal_next(journal)) < 0) { fprintf(stderr, "Failed to iterate to next entry: %s\n", strerror(-rc)); @@ -252,9 +256,22 @@ int main(int argc, char **argv) { continue; } - /* Looks like there is a but in libsystemd journal handling + gettimeofday(&tv_now, NULL); + + if (tv_now.tv_sec == tv_last.tv_sec) { + if (notification_count >= 5) { + fprintf(stderr, "Already showed five notifications, ignoring!\n"); + continue; + } + notification_count++; + } else { + tv_last = tv_now; + notification_count = 1; + } + + /* Looks like there is a bug in libsystemd journal handling * that jumps to wrong entries. Just skip old entries until we - * have a recent ones. */ + * have a recent one. */ if (old_entry > 0) { fprintf(stderr, "This is an old entry, ignoring.\n"); continue; diff --git a/journal-notify.h b/journal-notify.h index 0dd2962..d840236 100644 --- a/journal-notify.h +++ b/journal-notify.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include -- cgit v1.2.3-70-g09d2