From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH mini-journalreader 1/1] add '-j' flag to output json
Date: Wed, 24 Nov 2021 15:47:46 +0100 [thread overview]
Message-ID: <20211124144748.68687-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20211124144748.68687-1-d.csapak@proxmox.com>
in the format:
{"data":[... log lines ...],"success":1}
this is chosen so that we can achieve api compatibility when we stream
this output to an api client
strings are escaped by replacing '"', '\' and all values <= 0x1F by their
\uXXXX representation
invalid utf8 sequences will be returned as they are
(jq and the browser can handle that)
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
src/mini-journalreader.c | 66 ++++++++++++++++++++++++++++++++++++++--
1 file changed, 63 insertions(+), 3 deletions(-)
diff --git a/src/mini-journalreader.c b/src/mini-journalreader.c
index 92176ac..7ce7857 100644
--- a/src/mini-journalreader.c
+++ b/src/mini-journalreader.c
@@ -32,6 +32,8 @@
#define BUFSIZE 4096
static char BUF[BUFSIZE];
+bool json = false;
+bool first_line = true;
static uint64_t get_timestamp(sd_journal *j) {
uint64_t timestamp;
@@ -63,8 +65,19 @@ static void print_cursor(sd_journal *j) {
fprintf(stderr, "Failed to get cursor: %s\n", strerror(-r));
exit(1);
}
+ if (json) {
+ if (!first_line) {
+ print_to_buf(",\"", 2);
+ } else {
+ print_to_buf("\"", 1);
+ }
+ }
print_to_buf(cursor, strlen(cursor));
+ if (json) {
+ print_to_buf("\"", 1);
+ }
print_to_buf("\n", 1);
+ first_line = false;
free(cursor);
}
@@ -93,7 +106,15 @@ static void print_reboot(sd_journal *j) {
if (bootid[0] != '\0') { // we have some bootid
if (memcmp(bootid, d, l)) { // a new bootid found
memcpy(bootid, d, l);
- print_to_buf("-- Reboot --\n", 13);
+ if (json) {
+ if (!first_line) {
+ print_to_buf(",", 1);
+ }
+ print_to_buf("\"-- Reboot --\"\n", 15);
+ first_line = false;
+ } else {
+ print_to_buf("-- Reboot --\n", 13);
+ }
}
} else {
memcpy(bootid, d, l);
@@ -149,13 +170,35 @@ static bool print_field(sd_journal *j, const char *field) {
size_t fieldlen = strlen(field)+1;
d += fieldlen;
l -= fieldlen;
- print_to_buf(d, l);
+
+ if (json) {
+ char tmp[7];
+ for (size_t i = 0; i < l;i++) {
+ if (d[i] == '"' || d[i] == '\\' || (d[i] >= 0 && d[i] <= 0x1F)) {
+ sprintf(tmp, "\\u%04X", d[i]);
+ print_to_buf(tmp, 6);
+ } else {
+ print_to_buf(d+i, 1);
+ }
+ }
+ } else {
+ print_to_buf(d, l);
+ }
return true;
}
static void print_line(sd_journal *j) {
print_reboot(j);
+
+ if (json) {
+ if (!first_line) {
+ print_to_buf(",", 1);
+ }
+ print_to_buf("\"", 1);
+ first_line = false;
+ }
+
print_timestamp(j);
print_to_buf(" ", 1);
print_field(j, "_HOSTNAME");
@@ -167,6 +210,11 @@ static void print_line(sd_journal *j) {
print_pid(j);
print_to_buf(": ", 2);
print_field(j, "MESSAGE");
+
+ if (json) {
+ print_to_buf("\"", 1);
+ }
+
print_to_buf("\n", 1);
}
@@ -184,6 +232,7 @@ _Noreturn static void usage(char *error) {
" -n <integer>\t\tprint the last number entries logged\n"
" -f <cursor>\t\tprint from this cursor\n"
" -t <cursor>\t\tprint to this cursor\n"
+ " -j \t\t\tprint as json"
" -h \t\t\tthis help\n"
"\n"
"Passing no range option will dump all the available journal\n"
@@ -217,7 +266,7 @@ int main(int argc, char *argv[]) {
progname = argv[0];
- while ((c = (char)getopt (argc, argv, "b:e:d:n:f:t:h")) != -1) {
+ while ((c = (char)getopt (argc, argv, "b:e:d:n:f:t:jh")) != -1) {
switch (c) {
case 'b':
begin = arg_to_uint64(optarg);
@@ -239,6 +288,9 @@ int main(int argc, char *argv[]) {
case 't':
endcursor = optarg;
break;
+ case 'j':
+ json = true;
+ break;
case 'h':
usage(NULL);
case '?':
@@ -285,6 +337,10 @@ int main(int argc, char *argv[]) {
return 1;
}
+ if (json) {
+ print_to_buf("{\"data\":[", 9);
+ }
+
// if we want to print the last x entries, seek to cursor or end,
// then x entries back, print the cursor and finally print the
// entries until end or cursor
@@ -350,6 +406,10 @@ int main(int argc, char *argv[]) {
print_cursor(j);
sd_journal_close(j);
+ if (json) {
+ print_to_buf("],\"success\":1}", 14);
+ }
+
// print remaining buffer
fflush_unlocked(stdout);
--
2.30.2
next prev parent reply other threads:[~2021-11-24 14:48 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-24 14:47 [pve-devel] [PATCH mini-journalreader/http-server/manager] optimize journal api cal Dominik Csapak
2021-11-24 14:47 ` Dominik Csapak [this message]
2021-11-24 17:17 ` [pve-devel] applied: [PATCH mini-journalreader 1/1] add '-j' flag to output json Thomas Lamprecht
2021-11-24 14:47 ` [pve-devel] [PATCH http-server 1/1] http-server: let the api call decide the content-encoding Dominik Csapak
2021-11-24 17:18 ` [pve-devel] applied: " Thomas Lamprecht
2021-11-24 14:47 ` [pve-devel] [PATCH manager 1/1] api: journal: stream the journal data to the client Dominik Csapak
2021-11-24 17:32 ` Stoiko Ivanov
2021-11-24 17:46 ` Thomas Lamprecht
2021-11-24 17:50 ` Stoiko Ivanov
2021-11-24 17:38 ` [pve-devel] applied: " Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211124144748.68687-2-d.csapak@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal