logs-show: clean up journal_entry_to_json() a bit
* Make sure ret is initialized on success return * Drop unneeded 'object' variable * No need to ref/unref json objects when constructing intermediary array
This commit is contained in:
+30
-24
@@ -1193,17 +1193,20 @@ static int update_json_data_split(
|
||||
return update_json_data(h, flags, name, eq + 1, size - fieldlen - 1);
|
||||
}
|
||||
|
||||
int journal_entry_to_json(sd_journal *j, OutputFlags flags, const Set *output_fields, sd_json_variant **ret) {
|
||||
int journal_entry_to_json(
|
||||
sd_journal *j,
|
||||
OutputFlags flags,
|
||||
const Set *output_fields,
|
||||
sd_json_variant **ret) {
|
||||
|
||||
char usecbuf[CONST_MAX(DECIMAL_STR_MAX(usec_t), DECIMAL_STR_MAX(uint64_t))];
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *object = NULL;
|
||||
_cleanup_hashmap_free_ Hashmap *h = NULL;
|
||||
sd_id128_t journal_boot_id, seqnum_id;
|
||||
_cleanup_free_ char *cursor = NULL;
|
||||
usec_t realtime, monotonic;
|
||||
sd_json_variant **array = NULL;
|
||||
JsonData *d;
|
||||
uint64_t seqnum;
|
||||
const char *corrupted_what = NULL;
|
||||
_cleanup_hashmap_free_ Hashmap *h = NULL;
|
||||
_cleanup_free_ sd_json_variant **array = NULL;
|
||||
size_t n = 0;
|
||||
int r;
|
||||
|
||||
@@ -1214,32 +1217,32 @@ int journal_entry_to_json(sd_journal *j, OutputFlags flags, const Set *output_fi
|
||||
|
||||
r = sd_journal_get_cursor(j, &cursor);
|
||||
if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) {
|
||||
log_debug_errno(r, "Unable to determine cursor of entry, assuming bad or partially written entry: %m");
|
||||
return 0;
|
||||
corrupted_what = "cursor";
|
||||
goto corrupted_skip;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to get cursor: %m");
|
||||
|
||||
r = sd_journal_get_realtime_usec(j, &realtime);
|
||||
if (r == -EBADMSG) {
|
||||
log_debug_errno(r, "Unable to read realtime timestamp of entry, assuming bad or partially written entry: %m");
|
||||
return 0;
|
||||
corrupted_what = "realtime timestamp";
|
||||
goto corrupted_skip;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to get realtime timestamp: %m");
|
||||
|
||||
r = sd_journal_get_monotonic_usec(j, &monotonic, &journal_boot_id);
|
||||
if (r == -EBADMSG) {
|
||||
log_debug_errno(r, "Unable to read monotonic timestamp of entry, assuming bad or partially written entry: %m");
|
||||
return 0;
|
||||
corrupted_what = "monotonic timestamp";
|
||||
goto corrupted_skip;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to get monotonic timestamp: %m");
|
||||
|
||||
r = sd_journal_get_seqnum(j, &seqnum, &seqnum_id);
|
||||
if (r == -EBADMSG) {
|
||||
log_debug_errno(r, "Unable to read sequence number of entry, assuming bad or partially written entry: %m");
|
||||
return 0;
|
||||
corrupted_what = "sequence number";
|
||||
goto corrupted_skip;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to get seqnum: %m");
|
||||
@@ -1294,29 +1297,32 @@ int journal_entry_to_json(sd_journal *j, OutputFlags flags, const Set *output_fi
|
||||
return r;
|
||||
}
|
||||
|
||||
array = new(sd_json_variant*, hashmap_size(h)*2);
|
||||
array = new(sd_json_variant*, hashmap_size(h) * 2);
|
||||
if (!array)
|
||||
return log_oom();
|
||||
|
||||
CLEANUP_ARRAY(array, n, sd_json_variant_unref_many);
|
||||
|
||||
JsonData *d;
|
||||
HASHMAP_FOREACH(d, h) {
|
||||
assert(sd_json_variant_elements(d->values) > 0);
|
||||
|
||||
array[n++] = sd_json_variant_ref(d->name);
|
||||
array[n++] = d->name;
|
||||
|
||||
if (sd_json_variant_elements(d->values) == 1)
|
||||
array[n++] = sd_json_variant_ref(sd_json_variant_by_index(d->values, 0));
|
||||
array[n++] = sd_json_variant_by_index(d->values, 0);
|
||||
else
|
||||
array[n++] = sd_json_variant_ref(d->values);
|
||||
array[n++] = d->values;
|
||||
}
|
||||
|
||||
r = sd_json_variant_new_object(&object, array, n);
|
||||
r = sd_json_variant_new_object(ret, array, n);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to allocate JSON object: %m");
|
||||
|
||||
*ret = TAKE_PTR(object);
|
||||
return 1;
|
||||
|
||||
corrupted_skip:
|
||||
log_debug_errno(r, "Unable to determine %s of entry, assuming bad or partially written entry: %m", ASSERT_PTR(corrupted_what));
|
||||
*ret = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int output_json(
|
||||
@@ -1338,9 +1344,9 @@ static int output_json(
|
||||
return r;
|
||||
|
||||
return sd_json_variant_dump(object,
|
||||
output_mode_to_json_format_flags(mode) |
|
||||
(FLAGS_SET(flags, OUTPUT_COLOR) ? SD_JSON_FORMAT_COLOR : 0),
|
||||
f, NULL);
|
||||
output_mode_to_json_format_flags(mode) |
|
||||
(FLAGS_SET(flags, OUTPUT_COLOR) ? SD_JSON_FORMAT_COLOR : 0),
|
||||
f, NULL);
|
||||
}
|
||||
|
||||
static int output_cat_field(
|
||||
|
||||
Reference in New Issue
Block a user