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 <dsterba@suse.com>
This commit is contained in:
David Sterba 2022-09-30 08:52:14 +02:00
parent f7a768d624
commit 74eb8c68cf
2 changed files with 33 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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