Linux-EROFS Archive mirror
 help / color / mirror / Atom feed
From: Gao Xiang <hsiangkao@linux.alibaba.com>
To: linux-erofs@lists.ozlabs.org
Cc: Gao Xiang <hsiangkao@linux.alibaba.com>,
	Daan De Meyer <daan.j.demeyer@gmail.com>
Subject: [PATCH] erofs-utils: avoid noisy prints if stdout is not a tty
Date: Tue, 16 Jan 2024 12:26:42 +0800	[thread overview]
Message-ID: <20240116042642.3124559-1-hsiangkao@linux.alibaba.com> (raw)
In-Reply-To: <CAO8sHcmnq+foWo7AZYbkxJXHfSeZkd73Dq+1dQSZYBE6QxL8JQ@mail.gmail.com>

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 <daan.j.demeyer@gmail.com>
Closes: https://lore.kernel.org/r/CAO8sHcmnq+foWo7AZYbkxJXHfSeZkd73Dq+1dQSZYBE6QxL8JQ@mail.gmail.com
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
---
 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 <string.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <unistd.h>
 #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


      parent reply	other threads:[~2024-01-16  4:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-09 10:12 mkfs.erofs is noisy when run with a pipe attached to stdout/stderr Daan De Meyer
2024-01-09 13:10 ` Gao Xiang
2024-01-16  4:26 ` Gao Xiang [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240116042642.3124559-1-hsiangkao@linux.alibaba.com \
    --to=hsiangkao@linux.alibaba.com \
    --cc=daan.j.demeyer@gmail.com \
    --cc=linux-erofs@lists.ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).