btrfs-progs: consolidate the btrfs message helpers

These helpers all do variations on the same thing, so add a helper to
just do the printf part, and a macro to handle the special prefix and
postfix, and then make the helpers just use the macro and new helper.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Josef Bacik 2023-04-19 17:13:49 -04:00 committed by David Sterba
parent 49581d0556
commit 6636d9a6e2
2 changed files with 24 additions and 38 deletions

View File

@ -19,9 +19,6 @@
#include "common/messages.h" #include "common/messages.h"
#include "common/utils.h" #include "common/utils.h"
#define PREFIX_ERROR "ERROR: "
#define PREFIX_WARNING "WARNING: "
static const char *common_error_string[] = { static const char *common_error_string[] = {
[ERROR_MSG_MEMORY] = "not enough memory", [ERROR_MSG_MEMORY] = "not enough memory",
[ERROR_MSG_START_TRANS] = "failed to start transaction", [ERROR_MSG_START_TRANS] = "failed to start transaction",
@ -29,40 +26,13 @@ static const char *common_error_string[] = {
}; };
__attribute__ ((format (printf, 1, 2))) __attribute__ ((format (printf, 1, 2)))
void __btrfs_warning(const char *fmt, ...) void __btrfs_printf(const char *fmt, ...)
{ {
va_list args; va_list args;
fputs(PREFIX_WARNING, stderr);
va_start(args, fmt); va_start(args, fmt);
vfprintf(stderr, fmt, args); vfprintf(stderr, fmt, args);
va_end(args); va_end(args);
fputc('\n', stderr);
}
__attribute__ ((format (printf, 1, 2)))
void __btrfs_error(const char *fmt, ...)
{
va_list args;
fputs(PREFIX_ERROR, stderr);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fputc('\n', stderr);
}
__attribute__ ((format (printf, 1, 2)))
void internal_error(const char *fmt, ...)
{
va_list vargs;
va_start(vargs, fmt);
fputs("INTERNAL " PREFIX_ERROR, stderr);
vfprintf(stderr, fmt, vargs);
va_end(vargs);
fputc('\n', stderr);
print_trace();
} }
static bool should_print(int level) static bool should_print(int level)

View File

@ -46,6 +46,28 @@
#define UASSERT(c) assert(c) #define UASSERT(c) assert(c)
#endif #endif
#define PREFIX_ERROR "ERROR: "
#define PREFIX_WARNING "WARNING: "
#define __btrfs_msg(prefix, fmt, ...) \
do { \
fputs((prefix), stderr); \
__btrfs_printf((fmt), ##__VA_ARGS__); \
fputc('\n', stderr); \
} while (0)
#define __btrfs_warning(fmt, ...) \
__btrfs_msg(PREFIX_WARNING, fmt, ##__VA_ARGS__)
#define __btrfs_error(fmt, ...) \
__btrfs_msg(PREFIX_ERROR, fmt, ##__VA_ARGS__)
#define internal_error(fmt, ...) \
do { \
__btrfs_msg("INTERNAL " PREFIX_ERROR, fmt, ##__VA_ARGS__); \
print_trace(); \
} while (0)
#define error(fmt, ...) \ #define error(fmt, ...) \
do { \ do { \
PRINT_TRACE_ON_ERROR; \ PRINT_TRACE_ON_ERROR; \
@ -94,13 +116,7 @@
} while (0) } while (0)
__attribute__ ((format (printf, 1, 2))) __attribute__ ((format (printf, 1, 2)))
void __btrfs_warning(const char *fmt, ...); void __btrfs_printf(const char *fmt, ...);
__attribute__ ((format (printf, 1, 2)))
void __btrfs_error(const char *fmt, ...);
__attribute__ ((format (printf, 1, 2)))
void internal_error(const char *fmt, ...);
/* /*
* Level of messages that must be printed by default (in case the verbosity * Level of messages that must be printed by default (in case the verbosity