Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7F001C4706C for ; Tue, 16 Jan 2024 04:27:05 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4TDbZH5yhtz3bvX for ; Tue, 16 Jan 2024 15:27:03 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=linux.alibaba.com (client-ip=115.124.30.132; helo=out30-132.freemail.mail.aliyun.com; envelope-from=hsiangkao@linux.alibaba.com; receiver=lists.ozlabs.org) Received: from out30-132.freemail.mail.aliyun.com (out30-132.freemail.mail.aliyun.com [115.124.30.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4TDbZ72zqfz3bX9 for ; Tue, 16 Jan 2024 15:26:53 +1100 (AEDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R141e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046059;MF=hsiangkao@linux.alibaba.com;NM=1;PH=DS;RN=3;SR=0;TI=SMTPD_---0W-kkCpQ_1705379203; Received: from e69b19392.et15sqa.tbsite.net(mailfrom:hsiangkao@linux.alibaba.com fp:SMTPD_---0W-kkCpQ_1705379203) by smtp.aliyun-inc.com; Tue, 16 Jan 2024 12:26:47 +0800 From: Gao Xiang To: linux-erofs@lists.ozlabs.org Subject: [PATCH] erofs-utils: avoid noisy prints if stdout is not a tty Date: Tue, 16 Jan 2024 12:26:42 +0800 Message-Id: <20240116042642.3124559-1-hsiangkao@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: linux-erofs@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development of Linux EROFS file system List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Gao Xiang , Daan De Meyer Errors-To: linux-erofs-bounces+linux-erofs=archiver.kernel.org@lists.ozlabs.org Sender: "Linux-erofs" As Daan reported, "mkfs.erofs is super verbose as \r doesn't go to the beginning of the line". Don't print messages like this if stdout is not a tty. Reported-by: Daan De Meyer Closes: https://lore.kernel.org/r/CAO8sHcmnq+foWo7AZYbkxJXHfSeZkd73Dq+1dQSZYBE6QxL8JQ@mail.gmail.com Signed-off-by: Gao Xiang --- lib/config.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/config.c b/lib/config.c index 0cddc95..aa3dd1f 100644 --- a/lib/config.c +++ b/lib/config.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "erofs/print.h" #include "erofs/internal.h" #include "liberofs_private.h" @@ -16,6 +17,7 @@ struct erofs_configure cfg; struct erofs_sb_info sbi; +bool erofs_stdout_tty; void erofs_init_configure(void) { @@ -33,6 +35,8 @@ void erofs_init_configure(void) cfg.c_pclusterblks_max = 1; cfg.c_pclusterblks_def = 1; cfg.c_max_decompressed_extent_bytes = -1; + + erofs_stdout_tty = isatty(STDOUT_FILENO); } void erofs_show_config(void) @@ -107,15 +111,19 @@ char *erofs_trim_for_progressinfo(const char *str, int placeholder) { int col, len; + if (!erofs_stdout_tty) { + return strdup(str); + } else { #ifdef GWINSZ_IN_SYS_IOCTL - struct winsize winsize; + struct winsize winsize; - if(ioctl(1, TIOCGWINSZ, &winsize) >= 0 && - winsize.ws_col > 0) - col = winsize.ws_col; - else + if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) >= 0 && + winsize.ws_col > 0) + col = winsize.ws_col; + else #endif - col = 80; + col = 80; + } if (col <= placeholder) return strdup(""); @@ -140,7 +148,7 @@ void erofs_msg(int dbglv, const char *fmt, ...) FILE *f = dbglv >= EROFS_ERR ? stderr : stdout; if (__erofs_is_progressmsg) { - fputc('\n', f); + fputc('\n', stdout); __erofs_is_progressmsg = false; } va_start(ap, fmt); @@ -160,7 +168,12 @@ void erofs_update_progressinfo(const char *fmt, ...) vsprintf(msg, fmt, ap); va_end(ap); - printf("\r\033[K%s", msg); - __erofs_is_progressmsg = true; - fflush(stdout); + if (erofs_stdout_tty) { + printf("\r\033[K%s", msg); + __erofs_is_progressmsg = true; + fflush(stdout); + return; + } + fputs(msg, stdout); + fputc('\n', stdout); } -- 2.39.3