logs-show.c revision 886a64fe6903bc1ccde5c7af0eac6786918c8e49
/*-*- 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"
#include "hashmap.h"
#define PRINT_THRESHOLD 128
#define JSON_THRESHOLD 4096
int r;
r = sd_journal_get_catalog(j, &t);
if (r < 0)
return r;
if (!z)
return log_oom();
fputs("-- ", f);
fputs(z, f);
fputc('\n', f);
return 0;
}
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)
return log_oom();
*target_size = nl;
return 1;
}
assert(p);
if (flags & OUTPUT_SHOW_ALL)
return true;
if (l >= PRINT_THRESHOLD)
return false;
if (!utf8_is_printable_n(p, l))
return false;
return true;
}
static int output_short(
FILE *f,
sd_journal *j,
unsigned n_columns,
OutputFlags flags) {
int r;
const void *data;
size_t n = 0;
_cleanup_free_ char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, *message = NULL, *realtime = NULL, *monotonic = NULL, *priority = 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, priority_len = 0;
int p = LOG_INFO;
assert(f);
assert(j);
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
else if (r > 0)
continue;
if (r < 0)
return r;
}
if (!message)
return 0;
if (!(flags & OUTPUT_SHOW_ALL))
p = *priority - '0';
if (mode == OUTPUT_SHORT_MONOTONIC) {
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) {
return r;
}
fprintf(f, "[%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) {
return r;
}
t = (time_t) (x / USEC_PER_SEC);
log_error("Failed to format time.");
return r;
}
}
n += hostname_len + 1;
}
n += identifier_len + 1;
n += comm_len + 1;
} else
fputc(' ', f);
n += pid_len + 2;
n += fake_pid_len + 2;
}
if (flags & OUTPUT_COLOR) {
if (p <= LOG_ERR) {
} else if (p <= LOG_NOTICE) {
}
}
if (flags & OUTPUT_SHOW_ALL)
char bytes[FORMAT_BYTES_MAX];
char _cleanup_free_ *e;
if (!e)
else
} else
fputs("\n", f);
if (flags & OUTPUT_CATALOG)
print_catalog(f, j);
return 0;
}
static int output_verbose(
FILE *f,
sd_journal *j,
unsigned n_columns,
OutputFlags flags) {
const void *data;
char ts[FORMAT_TIMESTAMP_MAX];
int r;
assert(f);
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;
}
fprintf(f, "%s [%s]\n",
cursor);
const char *c;
char bytes[FORMAT_BYTES_MAX];
if (!c) {
log_error("Invalid field.");
return -EINVAL;
}
fprintf(f, "\t%.*s=[%s blob data]\n",
(int) (c - (const char*) data),
(const char*) data,
} else
}
if (flags & OUTPUT_CATALOG)
print_catalog(f, j);
return 0;
}
static int output_export(
FILE *f,
sd_journal *j,
unsigned n_columns,
OutputFlags flags) {
char sid[33];
int r;
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;
}
fprintf(f,
"__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;
}
fputc('\n', f);
} else
fputc('\n', f);
}
fputc('\n', f);
return 0;
}
void json_escape(
FILE *f,
const char* p,
size_t l,
OutputFlags flags) {
assert(f);
assert(p);
fputs("null", f);
else if (!utf8_is_printable_n(p, l)) {
bool not_first = false;
fputs("[ ", f);
while (l > 0) {
if (not_first)
else {
not_first = true;
}
p++;
l--;
}
fputs(" ]", f);
} else {
fputc('\"', f);
while (l > 0) {
if (*p == '"' || *p == '\\') {
fputc('\\', f);
fputc(*p, f);
} else if (*p < ' ')
fprintf(f, "\\u%04x", *p);
else
fputc(*p, f);
p++;
l--;
}
fputc('\"', f);
}
}
static int output_json(
FILE *f,
sd_journal *j,
unsigned n_columns,
OutputFlags flags) {
const void *data;
char sid[33], *k;
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 (mode == OUTPUT_JSON_PRETTY)
fprintf(f,
"{\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,
else {
if (mode == OUTPUT_JSON_SSE)
fputs("data: ", f);
fprintf(f,
"{ \"__CURSOR\" : \"%s\", "
"\"__REALTIME_TIMESTAMP\" : \"%llu\", "
"\"__MONOTONIC_TIMESTAMP\" : \"%llu\", "
"\"_BOOT_ID\" : \"%s\"",
(unsigned long long) realtime,
(unsigned long long) monotonic,
}
if (!h)
return -ENOMEM;
/* First round, iterate through the entry and count how often each field appears */
const char *eq;
char *n;
unsigned u;
if (length >= 9 &&
continue;
if (!eq)
continue;
if (!n) {
r = -ENOMEM;
goto finish;
}
u = PTR_TO_UINT(hashmap_get(h, n));
if (u == 0) {
if (r < 0) {
free(n);
goto finish;
}
} else {
free(n);
if (r < 0)
goto finish;
}
}
separator = true;
do {
done = true;
const char *eq;
char *kk, *n;
size_t m;
unsigned u;
/* We already printed the boot id, from the data in
* the header, hence let's suppress it here */
if (length >= 9 &&
continue;
if (!eq)
continue;
if (separator) {
if (mode == OUTPUT_JSON_PRETTY)
fputs(",\n\t", f);
else
fputs(", ", f);
}
if (!n) {
r = -ENOMEM;
goto finish;
}
if (u == 0) {
/* We already printed this, let's jump to the next */
free(n);
separator = false;
continue;
} else if (u == 1) {
/* Field only appears once, output it directly */
fputs(" : ", f);
hashmap_remove(h, n);
free(n);
separator = true;
continue;
} else {
/* Field appears multiple times, output it as array */
fputs(" : [ ", f);
/* Iterate through the end of the list */
if (length < m + 1)
continue;
continue;
if (((const char*) data)[m] != '=')
continue;
fputs(", ", f);
}
fputs(" ]", f);
hashmap_remove(h, n);
free(n);
/* Iterate data fields form the beginning */
done = false;
separator = true;
break;
}
}
} while (!done);
if (mode == OUTPUT_JSON_PRETTY)
fputs("\n}\n", f);
else if (mode == OUTPUT_JSON_SSE)
fputs("}\n\n", f);
else
fputs(" }\n", f);
r = 0;
while ((k = hashmap_steal_first_key(h)))
free(k);
hashmap_free(h);
return r;
}
static int output_cat(
FILE *f,
sd_journal *j,
unsigned n_columns,
OutputFlags flags) {
const void *data;
size_t l;
int r;
assert(j);
assert(f);
if (r < 0) {
/* An entry without MESSAGE=? */
if (r == -ENOENT)
return 0;
return r;
}
assert(l >= 8);
fputc('\n', f);
return 0;
}
static int (*output_funcs[_OUTPUT_MODE_MAX])(
FILE *f,
sd_journal*j,
unsigned n_columns,
OutputFlags flags) = {
[OUTPUT_SHORT] = output_short,
[OUTPUT_JSON] = output_json,
[OUTPUT_CAT] = output_cat
};
int output_journal(
FILE *f,
sd_journal *j,
unsigned n_columns,
OutputFlags flags) {
int ret;
if (n_columns <= 0)
return ret;
}
static int show_journal(FILE *f,
sd_journal *j,
unsigned n_columns,
unsigned how_many,
OutputFlags flags) {
int r;
unsigned line = 0;
bool need_seek = false;
assert(j);
/* Seek to end */
r = sd_journal_seek_tail(j);
if (r < 0)
goto finish;
r = sd_journal_previous_skip(j, how_many);
if (r < 0)
goto finish;
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)
fprintf(f, "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;
}
return r;
}
int r;
assert(j);
return -ENOMEM;
(void)(
/* Look for messages from the service itself */
(r = sd_journal_add_match(j, m1, 0)) ||
/* Look for coredumps of the service */
(r = sd_journal_add_disjunction(j)) ||
(r = sd_journal_add_match(j,
"MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
(r = sd_journal_add_match(j, m2, 0)) ||
/* Look for messages from PID 1 about this service */
(r = sd_journal_add_disjunction(j)) ||
(r = sd_journal_add_match(j, "_PID=1", 0)) ||
(r = sd_journal_add_match(j, m3, 0))
);
return r;
}
int r;
assert(j);
return -ENOMEM;
(void) (
/* Look for messages from the user service itself */
(r = sd_journal_add_match(j, m1, 0)) ||
(r = sd_journal_add_match(j, m4, 0)) ||
/* Look for messages from systemd about this service */
(r = sd_journal_add_disjunction(j)) ||
(r = sd_journal_add_match(j, m2, 0)) ||
(r = sd_journal_add_match(j, m4, 0)) ||
/* Look for coredumps of the service */
(r = sd_journal_add_disjunction(j)) ||
(r = sd_journal_add_match(j, m3, 0)) ||
(r = sd_journal_add_match(j, m4, 0))
);
return r;
}
int show_journal_by_unit(
FILE *f,
const char *unit,
unsigned n_columns,
unsigned how_many,
bool system) {
sd_journal *j = NULL;
int r;
if (how_many <= 0)
return 0;
r = sd_journal_open(&j, jflags);
if (r < 0)
goto finish;
if (system)
r = add_matches_for_unit(j, unit);
else
if (r < 0)
goto finish;
if (r < 0)
goto finish;
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_JSON_PRETTY] = "json-pretty",
[OUTPUT_JSON_SSE] = "json-sse",
[OUTPUT_CAT] = "cat"
};