diff --git a/zdev/include/misc.h b/zdev/include/misc.h index 81034339..43deb6cc 100644 --- a/zdev/include/misc.h +++ b/zdev/include/misc.h @@ -136,6 +136,12 @@ void _warn(const char *, ...); fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); \ _warn(__VA_ARGS__); \ } while (0) +void _warn_once(const char *, ...); +#define warn_once(...) do { \ + if (debug_enabled) \ + fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); \ + _warn_once(__VA_ARGS__); \ + } while (0) #define info(...) do { if (!quiet) fprintf(stdout_data ? stderr : stdout, \ __VA_ARGS__); } while (0) #define verb(...) do { if (verbose) fprintf(stdout_data ? stderr : stdout, \ diff --git a/zdev/src/misc.c b/zdev/src/misc.c index da4999f3..2f5cbc2b 100644 --- a/zdev/src/misc.c +++ b/zdev/src/misc.c @@ -57,6 +57,7 @@ unsigned long longrun_total; unsigned long longrun_current; static struct util_list *delayed_messages; +static struct util_list *warn_once_messages; static FILE *dryrun_file; static FILE *dryrun_get_file(void) @@ -245,6 +246,7 @@ static void dryrun_print(void) void misc_exit(void) { strlist_free(delayed_messages); + strlist_free(warn_once_messages); if (dryrun_file) { if (verbose) dryrun_print(); @@ -362,6 +364,29 @@ void _warn(const char *format, ...) va_end(args); } +/* Report a warning: print an error message to standard error. Only print each + * warning once. */ +void _warn_once(const char *format, ...) +{ + va_list args; + char *str; + + va_start(args, format); + if (vasprintf(&str, format, args) == -1) + oom(); + va_end(args); + + /* Check if message was printed before. */ + if (!warn_once_messages) + warn_once_messages = strlist_new(); + if (!strlist_find(warn_once_messages, str)) { + fprintf(stderr, "%s", str); + strlist_add(warn_once_messages, "%s", str); + } + + free(str); +} + /* Print an error message indicating an out-of-memory situation and exit. */ void oom(void) {