logs-show.c revision 4cd9a9d9ecf3a8835e21930f3215a5f5b74144be
/*-*- 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 General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU 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;
}
int r;
time_t t;
char buf[64];
const void *data;
size_t n = 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;
}
if (!message) {
r = 0;
goto finish;
}
r = sd_journal_get_realtime_usec(j, &realtime);
if (r < 0) {
goto finish;
}
log_error("Failed to format time.");
goto finish;
}
n += hostname_len + 1;
}
n += identifier_len + 1;
n += comm_len + 1;
}
n += pid_len + 2;
}
if (show_all)
else if (message_len + n < columns())
else if (n < columns()) {
char *e;
if (!e)
else
printf(": %s", 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;
if (!c) {
log_error("Invalid field.");
return -EINVAL;
}
printf("\t%.*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=%llu\n"
".monotonic=%llu\n"
".boot_id=%s\n",
(unsigned long long) realtime,
(unsigned long long) monotonic,
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\" : %llu,\n"
"\t\".monotonic\" : %llu,\n"
"\t\".boot_id\" : \"%s\"",
(unsigned long long) realtime,
(unsigned long long) monotonic,
const char *c;
if (!c) {
log_error("Invalid field.");
return -EINVAL;
}
}
return 0;
}
[OUTPUT_SHORT] = output_short,
};
}
int show_journal_by_unit(
const char *unit,
const char *prefix,
unsigned n_columns,
unsigned how_many,
bool show_all,
bool follow) {
char *m = NULL;
sd_journal *j;
int r;
int fd;
unsigned line = 0;
bool need_seek = false;
return 0;
if (how_many <= 0)
return 0;
if (n_columns <= 0)
if (!prefix)
prefix = "";
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_VERBOSE] = "verbose",
[OUTPUT_EXPORT] = "export",
[OUTPUT_JSON] = "json"
};