logs-show.c revision 669241a076108e0483d7d8475beaa506106d077e
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include "logs-show.h"
#include "log.h"
#include "util.h"
#include "utf8.h"
#define PRINT_THRESHOLD 128
static int parse_field(const void *data, size_t length, const char *field, char **target, size_t *target_size) {
void *buf;
return 0;
return 0;
if (!buf) {
log_error("Out of memory.");
return -ENOMEM;
}
*target_size = nl;
return 1;
}
if (show_all)
return true;
if (l > PRINT_THRESHOLD)
return false;
if (!utf8_is_printable_n(p, l))
return false;
return true;
}
OutputFlags flags) {
int r;
const void *data;
size_t n = 0;
char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, *message = NULL, *realtime = NULL, *monotonic = NULL;
size_t hostname_len = 0, identifier_len = 0, comm_len = 0, pid_len = 0, fake_pid_len = 0, message_len = 0, realtime_len = 0, monotonic_len = 0;
assert(j);
if (r < 0)
goto finish;
else if (r > 0)
continue;
if (r < 0)
goto finish;
else if (r > 0)
continue;
if (r < 0)
goto finish;
else if (r > 0)
continue;
if (r < 0)
goto finish;
else if (r > 0)
continue;
if (r < 0)
goto finish;
else if (r > 0)
continue;
if (r < 0)
goto finish;
else if (r > 0)
continue;
if (r < 0)
goto finish;
else if (r > 0)
continue;
if (r < 0)
goto finish;
}
if (!message) {
r = 0;
goto finish;
}
if (flags & OUTPUT_MONOTONIC_MODE) {
uint64_t t;
r = -ENOENT;
if (monotonic)
r = safe_atou64(monotonic, &t);
if (r < 0)
r = sd_journal_get_monotonic_usec(j, &t, &boot_id);
if (r < 0) {
goto finish;
}
printf("[%5llu.%06llu]",
(unsigned long long) (t / USEC_PER_SEC),
(unsigned long long) (t % USEC_PER_SEC));
n += 1 + 5 + 1 + 6 + 1;
} else {
char buf[64];
uint64_t x;
time_t t;
r = -ENOENT;
if (realtime)
r = safe_atou64(realtime, &x);
if (r < 0)
r = sd_journal_get_realtime_usec(j, &x);
if (r < 0) {
goto finish;
}
t = (time_t) (x / USEC_PER_SEC);
log_error("Failed to format time.");
goto finish;
}
}
hostname, hostname_len)) {
n += hostname_len + 1;
}
identifier, identifier_len)) {
n += identifier_len + 1;
n += comm_len + 1;
} else
putchar(' ');
n += pid_len + 2;
fake_pid, fake_pid_len)) {
n += fake_pid_len + 2;
}
if (flags & OUTPUT_SHOW_ALL)
char bytes[FORMAT_BYTES_MAX];
} else if ((flags & OUTPUT_FULL_WIDTH) ||
(message_len + n < n_columns))
char *e;
if (!e)
else
printf(": %s\n", e);
free(e);
} else
r = 0;
return r;
}
}
}
const void *data;
char *cursor;
char ts[FORMAT_TIMESTAMP_MAX];
int r;
assert(j);
r = sd_journal_get_realtime_usec(j, &realtime);
if (r < 0) {
return r;
}
r = sd_journal_get_cursor(j, &cursor);
if (r < 0) {
return r;
}
printf("%s [%s]\n",
cursor);
const char *c;
char bytes[FORMAT_BYTES_MAX];
if (!c) {
log_error("Invalid field.");
return -EINVAL;
}
printf("\t%.*s=[%s blob data]\n",
(int) (c - (const char*) data),
(const char*) data,
} else
}
return 0;
}
char sid[33];
int r;
char *cursor;
const void *data;
assert(j);
r = sd_journal_get_realtime_usec(j, &realtime);
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
r = sd_journal_get_cursor(j, &cursor);
if (r < 0) {
return r;
}
printf("__CURSOR=%s\n"
"__REALTIME_TIMESTAMP=%llu\n"
"__MONOTONIC_TIMESTAMP=%llu\n"
"_BOOT_ID=%s\n",
(unsigned long long) realtime,
(unsigned long long) monotonic,
/* We already printed the boot id, from the data in
* the header, hence let's suppress it here */
if (length >= 9 &&
continue;
const char *c;
if (!c) {
log_error("Invalid field.");
return -EINVAL;
}
} else
}
return 0;
}
static void json_escape(const char* p, size_t l) {
if (!utf8_is_printable_n(p, l)) {
bool not_first = false;
while (l > 0) {
if (not_first)
else {
not_first = true;
}
p++;
l--;
}
} else {
while (l > 0) {
if (*p == '"' || *p == '\\') {
} else
p++;
l--;
}
}
}
char *cursor;
const void *data;
char sid[33];
int r;
assert(j);
r = sd_journal_get_realtime_usec(j, &realtime);
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
r = sd_journal_get_cursor(j, &cursor);
if (r < 0) {
return r;
}
if (line == 1)
else
printf("{\n"
"\t\"__CURSOR\" : \"%s\",\n"
"\t\"__REALTIME_TIMESTAMP\" : \"%llu\",\n"
"\t\"__MONOTONIC_TIMESTAMP\" : \"%llu\",\n"
"\t\"_BOOT_ID\" : \"%s\"",
(unsigned long long) realtime,
(unsigned long long) monotonic,
const char *c;
/* We already printed the boot id, from the data in
* the header, hence let's suppress it here */
if (length >= 9 &&
continue;
if (!c) {
log_error("Invalid field.");
return -EINVAL;
}
}
return 0;
}
const void *data;
size_t l;
int r;
assert(j);
if (r < 0) {
return r;
}
assert(l >= 8);
putchar('\n');
return 0;
}
[OUTPUT_JSON] = output_json,
[OUTPUT_CAT] = output_cat
};
if (n_columns <= 0)
}
int show_journal_by_unit(
const char *unit,
unsigned n_columns,
unsigned how_many,
OutputFlags flags) {
char *m = NULL;
sd_journal *j = NULL;
int r;
unsigned line = 0;
bool need_seek = false;
return 0;
if (how_many <= 0)
return 0;
r = -ENOMEM;
goto finish;
}
if (r < 0)
goto finish;
r = sd_journal_add_match(j, m, strlen(m));
if (r < 0)
goto finish;
r = sd_journal_seek_tail(j);
if (r < 0)
goto finish;
r = sd_journal_previous_skip(j, how_many);
if (r < 0)
goto finish;
if (mode == OUTPUT_JSON) {
}
for (;;) {
for (;;) {
if (need_seek) {
r = sd_journal_next(j);
if (r < 0)
goto finish;
}
if (r == 0)
break;
need_seek = true;
if (not_before > 0) {
/* -ESTALE is returned if the
timestamp is not from this boot */
if (r == -ESTALE)
continue;
else if (r < 0)
goto finish;
if (usec < not_before)
continue;
}
line ++;
if (r < 0)
goto finish;
}
/* Check whether the cutoff line is too early */
r = sd_id128_get_boot(&boot_id);
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r > 0 && not_before < cutoff)
printf("Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.\n");
warn_cutoff = false;
}
if (!(flags & OUTPUT_FOLLOW))
break;
if (r < 0)
goto finish;
}
if (mode == OUTPUT_JSON)
if (m)
free(m);
if (j)
sd_journal_close(j);
return r;
}
static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
[OUTPUT_SHORT] = "short",
[OUTPUT_SHORT_MONOTONIC] = "short-monotonic",
[OUTPUT_VERBOSE] = "verbose",
[OUTPUT_EXPORT] = "export",
[OUTPUT_JSON] = "json",
[OUTPUT_CAT] = "cat"
};