Skip to content

Commit

Permalink
Add support for byte arrrays
Browse files Browse the repository at this point in the history
A log entry containing a byte encoded message would cause jq to return
an error `cannot add <string> to array`. This converts the message into
a string before adding it to the log stream.

Change-type: minor
  • Loading branch information
pipex committed Sep 14, 2023
1 parent a190c49 commit 83868cb
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion format.jq
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,37 @@ def fmt_unit($entry):
+"]"
;

# Convert a byte array into a string
# Source: https://stackoverflow.com/a/48431552

def btostring:
[foreach .[] as $item (
[0, 0]
;
if .[0] > 0 then [.[0] - 1, .[1] * 64 + ($item % 64)]
elif $item >= 240 then [3, $item % 8]
elif $item >= 224 then [2, $item % 16]
elif $item >= 192 then [1, $item % 32]
elif $item < 128 then [0, $item]
else error("Malformed UTF-8 bytes")
end
;
if .[0] == 0 then .[1] else empty end
)] | implode


def fmt_message($msg):
if $msg | type == "array" then
$msg | btostring
else
$msg
end
;

# Select fields from the journalctl output and tranform to
# a human readable format
def fmt_entry($entry):
[fmt_date($entry.__REALTIME_TIMESTAMP), fmt_unit($entry), .MESSAGE] | join(" ")
[fmt_date($entry.__REALTIME_TIMESTAMP), fmt_unit($entry), fmt_message(.MESSAGE)] | join(" ")
;

# Process the entry
Expand Down

0 comments on commit 83868cb

Please sign in to comment.