logs-show.c revision 7ea07dcddafe573c699fc48171b57b912897e7e2
/*-*- 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"
#define PRINT_THRESHOLD 128
static bool contains_unprintable(const void *p, size_t l) {
const char *j;
for (j = p; j < (const char *) p + l; j++)
if (*j < ' ' || *j >= 127)
return true;
return false;
}
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 (contains_unprintable(p, l))
return false;
return true;
}
static int output_short(sd_journal *j, unsigned line, unsigned n_columns, bool show_all, bool monotonic_mode) {
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 (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;
}
}
n += hostname_len + 1;
}
n += identifier_len + 1;
n += comm_len + 1;
} else
putchar(' ');
n += pid_len + 2;
n += fake_pid_len + 2;
}
if (show_all)
char bytes[FORMAT_BYTES_MAX];
} else if (message_len + n < n_columns)
else if (n < n_columns) {
char *e;
if (!e)
else
printf(": %s\n", e);
free(e);
} else
r = 0;
return r;
}
}
static int output_short_monotonic(sd_journal *j, unsigned line, unsigned n_columns, bool show_all) {
}
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 (contains_unprintable(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;
}
static int (*output_funcs[_OUTPUT_MODE_MAX])(sd_journal*j, unsigned line, unsigned n_columns, bool show_all) = {
[OUTPUT_JSON] = output_json,
[OUTPUT_CAT] = output_cat
};
int output_journal(sd_journal *j, OutputMode mode, unsigned line, unsigned n_columns, bool show_all) {
if (n_columns <= 0)
}
int show_journal_by_unit(
const char *unit,
unsigned n_columns,
unsigned how_many,
bool show_all,
bool follow) {
char *m = NULL;
sd_journal *j = NULL;
int r;
int fd;
unsigned line = 0;
bool need_seek = false;
return 0;
if (how_many <= 0)
return 0;
r = -ENOMEM;
goto finish;
}
if (r < 0)
goto finish;
fd = sd_journal_get_fd(j);
if (fd < 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;
}
if (!follow)
break;
if (r < 0)
goto finish;
r = sd_journal_process(j);
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"
};