summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2012-09-05 15:25:32 -0700
committerLennart Poettering <lennart@poettering.net>2012-09-05 15:25:32 -0700
commita6e87e90ede66815989ba2db92a07102a69906fe (patch)
treefb0a5304c033842264760a3efe2b7c869f757131 /src/shared
parent04bc4a3f47074d22035831965e97b0990fcf6f63 (diff)
journalctl: rework JSON output mode
This splits the JSON output mode into different modes: json and json-pretty. The former printing one entry per line, the latter showing JSON objects nicely indented and in multiple lines to make it easier to read for humans.
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/logs-show.c75
-rw-r--r--src/shared/logs-show.h10
2 files changed, 44 insertions, 41 deletions
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index 67b20563df..d60e5e5308 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -76,7 +76,7 @@ static bool shall_print(bool show_all, char *p, size_t l) {
return true;
}
-static int output_short(sd_journal *j, unsigned line, unsigned n_columns,
+static int output_short(sd_journal *j, OutputMode mode, unsigned line, unsigned n_columns,
OutputFlags flags) {
int r;
const void *data;
@@ -152,7 +152,7 @@ static int output_short(sd_journal *j, unsigned line, unsigned n_columns,
if (priority_len == 1 && *priority >= '0' && *priority <= '7')
p = *priority - '0';
- if (flags & OUTPUT_MONOTONIC_MODE) {
+ if (mode == OUTPUT_SHORT_MONOTONIC) {
uint64_t t;
sd_id128_t boot_id;
@@ -278,17 +278,7 @@ finish:
return r;
}
-static int output_short_realtime(sd_journal *j, unsigned line,
- unsigned n_columns, OutputFlags flags) {
- return output_short(j, line, n_columns, flags & ~OUTPUT_MONOTONIC_MODE);
-}
-
-static int output_short_monotonic(sd_journal *j, unsigned line,
- unsigned n_columns, OutputFlags flags) {
- return output_short(j, line, n_columns, flags | OUTPUT_MONOTONIC_MODE);
-}
-
-static int output_verbose(sd_journal *j, unsigned line,
+static int output_verbose(sd_journal *j, OutputMode mode, unsigned line,
unsigned n_columns, OutputFlags flags) {
const void *data;
size_t length;
@@ -340,7 +330,7 @@ static int output_verbose(sd_journal *j, unsigned line,
return 0;
}
-static int output_export(sd_journal *j, unsigned line,
+static int output_export(sd_journal *j, OutputMode mode, unsigned line,
unsigned n_columns, OutputFlags flags) {
sd_id128_t boot_id;
char sid[33];
@@ -452,7 +442,7 @@ static void json_escape(const char* p, size_t l) {
}
}
-static int output_json(sd_journal *j, unsigned line,
+static int output_json(sd_journal *j, OutputMode mode, unsigned line,
unsigned n_columns, OutputFlags flags) {
uint64_t realtime, monotonic;
char *cursor;
@@ -482,21 +472,25 @@ static int output_json(sd_journal *j, unsigned line,
return r;
}
- if (line == 1)
- fputc('\n', stdout);
+ if (mode == OUTPUT_JSON_PRETTY)
+ printf("{\n"
+ "\t\"__CURSOR\" : \"%s\",\n"
+ "\t\"__REALTIME_TIMESTAMP\" : \"%llu\",\n"
+ "\t\"__MONOTONIC_TIMESTAMP\" : \"%llu\",\n"
+ "\t\"_BOOT_ID\" : \"%s\"",
+ cursor,
+ (unsigned long long) realtime,
+ (unsigned long long) monotonic,
+ sd_id128_to_string(boot_id, sid));
else
- fputs(",\n", stdout);
-
- printf("{\n"
- "\t\"__CURSOR\" : \"%s\",\n"
- "\t\"__REALTIME_TIMESTAMP\" : \"%llu\",\n"
- "\t\"__MONOTONIC_TIMESTAMP\" : \"%llu\",\n"
- "\t\"_BOOT_ID\" : \"%s\"",
- cursor,
- (unsigned long long) realtime,
- (unsigned long long) monotonic,
- sd_id128_to_string(boot_id, sid));
-
+ printf("{ \"__CURSOR\" : \"%s\", "
+ "\"__REALTIME_TIMESTAMP\" : \"%llu\", "
+ "\"__MONOTONIC_TIMESTAMP\" : \"%llu\", "
+ "\"_BOOT_ID\" : \"%s\"",
+ cursor,
+ (unsigned long long) realtime,
+ (unsigned long long) monotonic,
+ sd_id128_to_string(boot_id, sid));
free(cursor);
SD_JOURNAL_FOREACH_DATA(j, data, length) {
@@ -514,18 +508,25 @@ static int output_json(sd_journal *j, unsigned line,
return -EINVAL;
}
- fputs(",\n\t", stdout);
+ if (mode == OUTPUT_JSON_PRETTY)
+ fputs(",\n\t", stdout);
+ else
+ fputs(", ", stdout);
+
json_escape(data, c - (const char*) data);
fputs(" : ", stdout);
json_escape(c + 1, length - (c - (const char*) data) - 1);
}
- fputs("\n}", stdout);
+ if (mode == OUTPUT_JSON_PRETTY)
+ fputs("\n}\n", stdout);
+ else
+ fputs(" }\n", stdout);
return 0;
}
-static int output_cat(sd_journal *j, unsigned line,
+static int output_cat(sd_journal *j, OutputMode mode, unsigned line,
unsigned n_columns, OutputFlags flags) {
const void *data;
size_t l;
@@ -547,13 +548,14 @@ static int output_cat(sd_journal *j, unsigned line,
return 0;
}
-static int (*output_funcs[_OUTPUT_MODE_MAX])(sd_journal*j, unsigned line,
+static int (*output_funcs[_OUTPUT_MODE_MAX])(sd_journal*j, OutputMode mode, unsigned line,
unsigned n_columns, OutputFlags flags) = {
- [OUTPUT_SHORT] = output_short_realtime,
- [OUTPUT_SHORT_MONOTONIC] = output_short_monotonic,
+ [OUTPUT_SHORT] = output_short,
+ [OUTPUT_SHORT_MONOTONIC] = output_short,
[OUTPUT_VERBOSE] = output_verbose,
[OUTPUT_EXPORT] = output_export,
[OUTPUT_JSON] = output_json,
+ [OUTPUT_JSON_PRETTY] = output_json,
[OUTPUT_CAT] = output_cat
};
@@ -566,7 +568,7 @@ int output_journal(sd_journal *j, OutputMode mode, unsigned line,
if (n_columns <= 0)
n_columns = columns();
- ret = output_funcs[mode](j, line, n_columns, flags);
+ ret = output_funcs[mode](j, mode, line, n_columns, flags);
fflush(stdout);
return ret;
}
@@ -736,6 +738,7 @@ static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
[OUTPUT_VERBOSE] = "verbose",
[OUTPUT_EXPORT] = "export",
[OUTPUT_JSON] = "json",
+ [OUTPUT_JSON_PRETTY] = "json-pretty",
[OUTPUT_CAT] = "cat"
};
diff --git a/src/shared/logs-show.h b/src/shared/logs-show.h
index 58ff9e5760..3e6b6e0f64 100644
--- a/src/shared/logs-show.h
+++ b/src/shared/logs-show.h
@@ -33,6 +33,7 @@ typedef enum OutputMode {
OUTPUT_VERBOSE,
OUTPUT_EXPORT,
OUTPUT_JSON,
+ OUTPUT_JSON_PRETTY,
OUTPUT_CAT,
_OUTPUT_MODE_MAX,
_OUTPUT_MODE_INVALID = -1
@@ -40,11 +41,10 @@ typedef enum OutputMode {
typedef enum OutputFlags {
OUTPUT_SHOW_ALL = 1 << 0,
- OUTPUT_MONOTONIC_MODE = 1 << 1,
- OUTPUT_FOLLOW = 1 << 2,
- OUTPUT_WARN_CUTOFF = 1 << 3,
- OUTPUT_FULL_WIDTH = 1 << 4,
- OUTPUT_COLOR = 1 << 5
+ OUTPUT_FOLLOW = 1 << 1,
+ OUTPUT_WARN_CUTOFF = 1 << 2,
+ OUTPUT_FULL_WIDTH = 1 << 3,
+ OUTPUT_COLOR = 1 << 4
} OutputFlags;
int output_journal(sd_journal *j, OutputMode mode, unsigned line,