shared: add OUTPUT_SKIP_UNPRINTABLE to log-show

This commit adds a new OUTPUT_SKIP_UNPRINTABLE to the OutputFlags
and adds code in `update_json_data` and `json_escape` to honor it.

When set all json fields that have unprintable data will be skipped
and `null` is send instead.
This commit is contained in:
Michael Vogt
2026-06-23 12:41:38 +02:00
parent 6b395fd44b
commit f05336401a
2 changed files with 9 additions and 5 deletions
+4 -1
View File
@@ -1039,7 +1039,8 @@ void json_escape(
assert(f);
assert(p);
if (!(flags & OUTPUT_SHOW_ALL) && l >= JSON_THRESHOLD)
if (((flags & OUTPUT_SKIP_UNPRINTABLE) && !utf8_is_printable(p, l)) ||
(!(flags & OUTPUT_SHOW_ALL) && l >= JSON_THRESHOLD))
fputs("null", f);
else if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(p, l)) {
@@ -1124,6 +1125,8 @@ static int update_json_data(
r = sd_json_variant_new_null(&v);
else if (utf8_is_printable(value, size))
r = sd_json_variant_new_stringn(&v, value, size);
else if (flags & OUTPUT_SKIP_UNPRINTABLE)
r = sd_json_variant_new_null(&v);
else
r = sd_json_variant_new_array_bytes(&v, value, size);
if (r < 0)
+5 -4
View File
@@ -43,12 +43,13 @@ typedef enum OutputFlags {
OUTPUT_UTC = 1 << 6,
OUTPUT_NO_HOSTNAME = 1 << 7,
OUTPUT_TRUNCATE_NEWLINE = 1 << 8,
OUTPUT_SKIP_UNPRINTABLE = 1 << 9,
/* Specific to process tree output */
OUTPUT_KERNEL_THREADS = 1 << 9,
OUTPUT_CGROUP_XATTRS = 1 << 10,
OUTPUT_CGROUP_ID = 1 << 11,
OUTPUT_HIDE_EXTRA = 1 << 12,
OUTPUT_KERNEL_THREADS = 1 << 10,
OUTPUT_CGROUP_XATTRS = 1 << 11,
OUTPUT_CGROUP_ID = 1 << 12,
OUTPUT_HIDE_EXTRA = 1 << 13,
} OutputFlags;
sd_json_format_flags_t output_mode_to_json_format_flags(OutputMode m);