Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH 1/2] btrfs-progs: common: introduce fmt_print_start_object
@ 2020-11-11 16:14 Sidong Yang
  2020-11-11 16:14 ` [PATCH 2/2] btrfs-progs: device stats: add json output format Sidong Yang
  0 siblings, 1 reply; 2+ messages in thread
From: Sidong Yang @ 2020-11-11 16:14 UTC (permalink / raw)
  To: dsterba, linux-btrfs; +Cc: Sidong Yang

Introduce a new function that can be used when you need to print an json
object in command like "device stats".

Signed-off-by: Sidong Yang <realwakka@gmail.com>
---
 common/format-output.c | 20 ++++++++++++++++++++
 common/format-output.h |  3 +++
 2 files changed, 23 insertions(+)

diff --git a/common/format-output.c b/common/format-output.c
index 8df93ecb..f31e7259 100644
--- a/common/format-output.c
+++ b/common/format-output.c
@@ -213,6 +213,26 @@ void fmt_print_end_group(struct format_ctx *fctx, const char *name)
 	}
 }
 
+void fmt_print_start_object(struct format_ctx *fctx)
+{
+	if (bconf.output_format == CMD_FORMAT_JSON) {
+		fmt_separator(fctx);
+		fmt_inc_depth(fctx);
+		fctx->memb[fctx->depth] = 0;
+		putchar('{');
+	}
+}
+
+void fmt_print_end_object(struct format_ctx *fctx)
+{
+	if (bconf.output_format == CMD_FORMAT_JSON) {
+		fmt_dec_depth(fctx);
+		putchar('\n');
+		fmt_indent2(fctx->depth);
+		putchar('}');
+	}
+}
+
 /* Use rowspec to print according to currently set output format */
 void fmt_print(struct format_ctx *fctx, const char* key, ...)
 {
diff --git a/common/format-output.h b/common/format-output.h
index bcc2d74d..9d606482 100644
--- a/common/format-output.h
+++ b/common/format-output.h
@@ -87,4 +87,7 @@ void fmt_print_start_group(struct format_ctx *fctx, const char *name,
 		enum json_type jtype);
 void fmt_print_end_group(struct format_ctx *fctx, const char *name);
 
+void fmt_print_start_object(struct format_ctx *fctx);
+void fmt_print_end_object(struct format_ctx *fctx);
+
 #endif
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-11-11 16:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-11-11 16:14 [PATCH 1/2] btrfs-progs: common: introduce fmt_print_start_object Sidong Yang
2020-11-11 16:14 ` [PATCH 2/2] btrfs-progs: device stats: add json output format Sidong Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox