output-mode: add generic helper to pick right JsonFormatFlags for given OutputMode

This commit is contained in:
Lennart Poettering 2018-12-03 20:40:02 +01:00
parent ab91733c7e
commit 9e964bb8e4
3 changed files with 22 additions and 4 deletions

View file

@ -946,10 +946,7 @@ static int output_json(
}
json_variant_dump(object,
(mode == OUTPUT_JSON_SSE ? JSON_FORMAT_SSE :
mode == OUTPUT_JSON_SEQ ? JSON_FORMAT_SEQ :
mode == OUTPUT_JSON_PRETTY ? JSON_FORMAT_PRETTY :
JSON_FORMAT_NEWLINE) |
output_mode_to_json_format_flags(mode) |
(FLAGS_SET(flags, OUTPUT_COLOR) ? JSON_FORMAT_COLOR : 0),
f, NULL);

View file

@ -3,6 +3,24 @@
#include "output-mode.h"
#include "string-table.h"
JsonFormatFlags output_mode_to_json_format_flags(OutputMode m) {
switch (m) {
case OUTPUT_JSON_SSE:
return JSON_FORMAT_SSE;
case OUTPUT_JSON_SEQ:
return JSON_FORMAT_SEQ;
case OUTPUT_JSON_PRETTY:
return JSON_FORMAT_PRETTY;
default:
return JSON_FORMAT_NEWLINE;
}
}
static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
[OUTPUT_SHORT] = "short",
[OUTPUT_SHORT_FULL] = "short-full",

View file

@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
#include "json.h"
#include "macro.h"
typedef enum OutputMode {
@ -39,5 +40,7 @@ typedef enum OutputFlags {
OUTPUT_NO_HOSTNAME = 1 << 9,
} OutputFlags;
JsonFormatFlags output_mode_to_json_format_flags(OutputMode m);
const char* output_mode_to_string(OutputMode m) _const_;
OutputMode output_mode_from_string(const char *s) _pure_;