journalctl.c revision cf244689e9d1ab50082c9ddd0f3c4d1eb982badc
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 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 <fcntl.h>
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <getopt.h>
#include "sd-journal.h"
#include "log.h"
#include "util.h"
#include "build.h"
#include "pager.h"
#define PRINT_THRESHOLD 128
static enum {
} arg_output = OUTPUT_SHORT;
static bool arg_follow = false;
static bool arg_show_all = false;
static bool arg_no_pager = false;
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;
}
int r;
time_t t;
char buf[64];
const void *data;
size_t n = 0;
assert(j);
r = sd_journal_get_realtime_usec(j, &realtime);
if (r < 0) {
return r;
}
log_error("Failed to format time.");
return -EINVAL;
}
length < PRINT_THRESHOLD))) {
}
if (arg_show_all)
else if (n < columns()) {
char *e;
if (!e)
else
printf(" %s", e);
free(e);
}
}
return 0;
}
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,
};
static int help(void) {
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
"Send control commands to or query the login manager.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --no-pager Do not pipe output into a pager\n"
" -a --all Show all properties, including long and unprintable\n"
" -f --follow Follow journal\n"
" -o --output=STRING Change output mode (short, verbose, export, json)\n",
return 0;
}
enum {
ARG_VERSION = 0x100,
};
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return 0;
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case 'f':
arg_follow = true;
break;
case 'o':
else {
return -EINVAL;
}
break;
case 'a':
arg_show_all = true;
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
return 1;
}
int r, i, fd;
sd_journal *j = NULL;
unsigned line = 0;
log_open();
if (r <= 0)
goto finish;
r = sd_journal_open(&j, 0);
if (r < 0) {
goto finish;
}
if (r < 0) {
goto finish;
}
}
fd = sd_journal_get_fd(j);
if (fd < 0) {
goto finish;
}
r = sd_journal_seek_head(j);
if (r < 0) {
goto finish;
}
if (!arg_no_pager && !arg_follow) {
columns();
pager_open();
}
if (arg_output == OUTPUT_JSON) {
}
for (;;) {
for (;;) {
r = sd_journal_next(j);
if (r < 0) {
goto finish;
}
if (r == 0)
break;
line ++;
if (r < 0)
goto finish;
}
if (!arg_follow)
break;
break;
log_error("poll(): %m");
r = -errno;
goto finish;
}
r = sd_journal_process(j);
if (r < 0) {
goto finish;
}
}
if (arg_output == OUTPUT_JSON)
if (j)
sd_journal_close(j);
pager_close();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}