From patchwork Wed Sep 16 09:40:46 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhaolei X-Patchwork-Id: 7193351 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0FDE79F380 for ; Wed, 16 Sep 2015 09:42:37 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1ECC0208A2 for ; Wed, 16 Sep 2015 09:42:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1B40C208A8 for ; Wed, 16 Sep 2015 09:42:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752553AbbIPJmb (ORCPT ); Wed, 16 Sep 2015 05:42:31 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:58059 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1752031AbbIPJma (ORCPT ); Wed, 16 Sep 2015 05:42:30 -0400 X-IronPort-AV: E=Sophos;i="5.15,520,1432569600"; d="scan'208";a="100777627" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 16 Sep 2015 17:45:18 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id t8G9gALr000364 for ; Wed, 16 Sep 2015 17:42:10 +0800 Received: from localhost.localdomain (10.167.226.114) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server id 14.3.181.6; Wed, 16 Sep 2015 17:42:25 +0800 From: Zhao Lei To: CC: Zhao Lei , Qu Wenruo Subject: [PATCH v2 1/2] btrfs-progs: Introduce warning and error for common use Date: Wed, 16 Sep 2015 17:40:46 +0800 Message-ID: X-Mailer: git-send-email 1.8.5.1 In-Reply-To: References: MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Signed-off-by: Zhao Lei --- utils.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/utils.h b/utils.h index dce0a47..c63e372 100644 --- a/utils.h +++ b/utils.h @@ -22,6 +22,7 @@ #include #include "ctree.h" #include +#include #define BTRFS_MKFS_SYSTEM_GROUP_SIZE (4 * 1024 * 1024) #define BTRFS_MKFS_SMALL_VOLUME_SIZE (1024 * 1024 * 1024) @@ -269,4 +270,58 @@ const char *get_argv0_buf(void); unsigned int get_unit_mode_from_arg(int *argc, char *argv[], int df_mode); +static inline void __veprintf(const char *prefix, const char *format, + va_list ap) +{ + if (prefix) + fprintf(stderr, "%s", prefix); + vfprintf(stderr, format, ap); +} + +static inline void warning(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + __veprintf("WARNING: ", fmt, args); + va_end(args); +} + +static inline void error(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + __veprintf("ERROR: ", fmt, args); + va_end(args); +} + +static inline int warning_on(int condition, const char *fmt, ...) +{ + if (!condition) + return 0; + + va_list args; + + va_start(args, fmt); + __veprintf("WARNING: ", fmt, args); + va_end(args); + + return 1; +} + +static inline int error_on(int condition, const char *fmt, ...) +{ + if (!condition) + return 0; + + va_list args; + + va_start(args, fmt); + __veprintf("ERROR: ", fmt, args); + va_end(args); + + return 1; +} + #endif