From f7e37804586beee663d63b504510fefb28a2953e Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 3 Jul 2014 14:45:12 +0200 Subject: add version and verbose output --- Makefile | 7 ++++++- README.md | 1 + journal-notify.c | 23 +++++++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 4601c7d..8b8b2e6 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,12 @@ VERSION := 0.0.2 all: journal-notify README.html -journal-notify: journal-notify.c +version.h: $(wildcard .git/HEAD .git/index .git/refs/tags/*) Makefile + echo "#ifndef VERSION" > $@ + echo "#define VERSION \"$(shell git describe --tags --long 2>/dev/null || echo ${VERSION})\"" >> $@ + echo "#endif" >> $@ + +journal-notify: journal-notify.c version.h $(CC) $(CFLAGS) -o journal-notify journal-notify.c README.html: README.md diff --git a/README.md b/README.md index d9a8022..d560bb5 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Be warned: This can flood your disktop with notifications. to systemd journal fields. (see `man 7 systemd.journal-fields`) * *-n*: no case sensitive regular expressions * *-r REGEX*: This add a regular expression match for the message field. +* *-v* verbose output, can be specified multiple times The screenshot shown above resulted from this command: diff --git a/journal-notify.c b/journal-notify.c index 413f6d8..0f54090 100644 --- a/journal-notify.c +++ b/journal-notify.c @@ -14,9 +14,11 @@ #include +#include "version.h" + const char * program = NULL; -#define OPTSTRING "ehi:m:nr:" +#define OPTSTRING "ehi:m:nr:v" #define DEFAULTICON "dialog-information" int notify(const char * summary, const char * body, const char * icon) { @@ -46,6 +48,7 @@ out: int main(int argc, char **argv) { int i, rc = EXIT_FAILURE; + uint8_t verbose = 0; uint8_t have_regex = 0; regex_t regex; @@ -68,14 +71,21 @@ int main(int argc, char **argv) { regex_flags |= REG_EXTENDED; break; case 'h': - fprintf(stderr, "usage: %s [-e] [-h] [-m MATCH] [-n] [-r REGEX]\n", program); + fprintf(stderr, "usage: %s [-e] [-h] [-i ICON] [-m MATCH] [-n] [-r REGEX] [-vv]\n", program); return EXIT_SUCCESS; case 'n': regex_flags |= REG_ICASE; break; + case 'v': + verbose++; + break; } } + /* say hello */ + if (verbose > 0) + printf("%s v%s (compiled: " __DATE__ ", " __TIME__ ")\n", program, VERSION); + /* reinitialize getopt() by resetting optind to 0 */ optind = 0; @@ -92,6 +102,9 @@ int main(int argc, char **argv) { icon = optarg; break; case 'm': + if (verbose > 1) + printf("Adding match '%s'...\n", optarg); + if ((rc = sd_journal_add_match(journal, optarg, 0)) < 0) { fprintf(stderr, "Failed to add match '%s': %s\n", optarg, strerror(-rc)); goto out20; @@ -99,6 +112,9 @@ int main(int argc, char **argv) { break; case 'r': + if (verbose > 1) + printf("Adding regular expression '%s'...\n", optarg); + if (have_regex > 0) { fprintf(stderr, "Only one regex allowed!\n"); rc = EXIT_FAILURE; @@ -162,6 +178,9 @@ int main(int argc, char **argv) { /* show notification */ if (have_regex == 0 || regexec(®ex, message, 0, NULL, 0) == 0) { for (i = 0; i < 3; i++) { + if (verbose > 0) + printf("Showing notification: %s: %s\n", summary, message); + if ((rc = notify(summary, message, icon)) == 0) break; -- cgit v1.2.3-54-g00ecf