From 74eb8c68cf45d5679cf8ce569677dc5ca6a4b596 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Fri, 30 Sep 2022 08:52:14 +0200 Subject: [PATCH] btrfs-progs: add template for common error messages There are several generic errors that repeat the same message. Define a template for such messages, with optional text. Signed-off-by: David Sterba --- common/messages.c | 23 +++++++++++++++++++++++ common/messages.h | 10 ++++++++++ 2 files changed, 33 insertions(+) diff --git a/common/messages.c b/common/messages.c index 09716a40..0fc8c758 100644 --- a/common/messages.c +++ b/common/messages.c @@ -19,6 +19,10 @@ #include "common/messages.h" #include "common/utils.h" +static const char *common_error_string[] = { + [ERROR_MSG_MEMORY] = "not enough memory", +}; + __attribute__ ((format (printf, 1, 2))) void __btrfs_warning(const char *fmt, ...) { @@ -113,3 +117,22 @@ void pr_verbose(int level, const char *fmt, ...) vfprintf(stdout, fmt, args); va_end(args); } + +/* Print common error message with optional data, appended after the generic text */ +__attribute__ ((format (printf, 2, 3))) +void error_msg(enum common_error error, const char *msg, ...) +{ + const char *str = common_error_string[error]; + + if (msg) { + va_list args; + + va_start(args, msg); + fprintf(stderr, "ERROR: %s: ", str); + vfprintf(stderr, msg, args); + va_end(args); + fputc('\n', stderr); + } else { + fprintf(stderr, "ERROR: %s\n", str); + } +} diff --git a/common/messages.h b/common/messages.h index 915f760e..e695462b 100644 --- a/common/messages.h +++ b/common/messages.h @@ -130,4 +130,14 @@ void internal_error(const char *fmt, ...); __attribute__ ((format (printf, 2, 3))) void pr_verbose(int level, const char *fmt, ...); +/* + * Commonly used errors + */ +enum common_error { + ERROR_MSG_MEMORY, +}; + +__attribute__ ((format (printf, 2, 3))) +void error_msg(enum common_error error, const char *msg, ...); + #endif