summaryrefslogtreecommitdiff
path: root/utils.h
diff options
context:
space:
mode:
authorZhao Lei <zhaolei@cn.fujitsu.com>2015-09-28 21:58:13 +0800
committerDavid Sterba <dsterba@suse.com>2015-10-21 14:29:26 +0200
commita2a7d2bb57fae51c424e43fe9de3944ed2061af8 (patch)
treefe15c9900848ecd0175accea4fe714c8d0195819 /utils.h
parent90b63ba242e393f2fac4a29fabd1e8736059303b (diff)
btrfs-progs: Introduce warning and error for common use
Current code use fprintf(stderr, "...") to output warnning and error information. The error message have different style, as: # grep fprintf *.c fprintf(stderr, "Open ctree failed\n"); fprintf(stderr, "%s: open ctree failed\n", __func__); fprintf(stderr, "ERROR: cannot open ctree\n"); ... And sometimes, we forgot add tailed '\n', or use printf instead, as in current code: printf("warning, device %llu is missing\n", This patch introduce warning() and error() as common function, to make: 1: Each warning and error information have same format 2: Easy to search/change all error message 3: Easy to modify function's internal for debug or other requirement, for example: print function/linenumber in error() dumpstack in error() add some trace for some style of message add support for -v, -vv, ... support for locales custom output functions support some special device/tty Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com> [print newline after the message] Signed-off-by: David Sterba <dsterba@suse.com>
Diffstat (limited to 'utils.h')
-rw-r--r--utils.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/utils.h b/utils.h
index 192f3d1c..044ea154 100644
--- a/utils.h
+++ b/utils.h
@@ -22,6 +22,7 @@
#include <sys/stat.h>
#include "ctree.h"
#include <dirent.h>
+#include <stdarg.h>
#define BTRFS_MKFS_SYSTEM_GROUP_SIZE (4 * 1024 * 1024)
#define BTRFS_MKFS_SMALL_VOLUME_SIZE (1024 * 1024 * 1024)
@@ -270,4 +271,58 @@ const char *get_argv0_buf(void);
unsigned int get_unit_mode_from_arg(int *argc, char *argv[], int df_mode);
+static inline void warning(const char *fmt, ...)
+{
+ va_list args;
+
+ fputs("WARNING: ", stderr);
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
+ fputc('\n', stderr);
+}
+
+static inline void error(const char *fmt, ...)
+{
+ va_list args;
+
+ fputs("ERROR: ", stderr);
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
+ fputc('\n', stderr);
+}
+
+static inline int warning_on(int condition, const char *fmt, ...)
+{
+ va_list args;
+
+ if (!condition)
+ return 0;
+
+ fputs("WARNING: ", stderr);
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
+ fputc('\n', stderr);
+
+ return 1;
+}
+
+static inline int error_on(int condition, const char *fmt, ...)
+{
+ va_list args;
+
+ if (!condition)
+ return 0;
+
+ fputs("ERROR: ", stderr);
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
+ fputc('\n', stderr);
+
+ return 1;
+}
+
#endif