diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2024-10-04 18:08:35 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2024-10-28 09:23:07 +0100 |
commit | 23441a3d882930b854b827172343c4222a2c9935 (patch) | |
tree | 5751d9934838bfa61f31d5bf3611064afe8f660e /src/shared | |
parent | sd-id128: mark functions as const, not pure (diff) | |
download | systemd-23441a3d882930b854b827172343c4222a2c9935.tar.xz systemd-23441a3d882930b854b827172343c4222a2c9935.zip |
sd-json,tree-wide: add sd_json_format_enabled() and use it everwhere
We often used a pattern like if (!FLAGS_SET(flags, SD_JSON_FORMAT_OFF)),
which is rather verbose and also contains a double negative, which we try
to avoid. Add a little helper to avoid an explicit bit check.
This change clarifies an aditional thing: in some cases we treated
SD_JSON_FORMAT_OFF as a flag (flags & SD_JSON_FORMAT_OFF), while in other cases
we treated it as an independent enum value (flags == SD_JSON_FORMAT_OFF).
In the first form, flags like SD_JSON_FORMAT_SSE do _not_ turn the json
output on, while in the second form they do. Let's use the first form
everywhere.
No functional change intended.
Initially I wasn't sure if this helper should be made public or just internal,
but it seems such a common pattern that if we expose the flags, we might just
as well expose it too, to make life easier for any consumers.
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/bootspec.c | 2 | ||||
-rw-r--r-- | src/shared/format-table.c | 2 |
2 files changed, 2 insertions, 2 deletions
diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 87aa11ccdf..4c0195a41e 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -1936,7 +1936,7 @@ int show_boot_entries(const BootConfig *config, sd_json_format_flags_t json_form assert(config); - if (!FLAGS_SET(json_format, SD_JSON_FORMAT_OFF)) { + if (sd_json_format_enabled(json_format)) { _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; for (size_t i = 0; i < config->n_entries; i++) { diff --git a/src/shared/format-table.c b/src/shared/format-table.c index f5ef7175a3..5f05247438 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -3124,7 +3124,7 @@ int table_print_json(Table *t, FILE *f, sd_json_format_flags_t flags) { assert(t); - if (flags & SD_JSON_FORMAT_OFF) /* If JSON output is turned off, use regular output */ + if (!sd_json_format_enabled(flags)) /* If JSON output is turned off, use regular output */ return table_print(t, f); if (!f) |