journalctl.c revision d3f2bdbfee94a4cd124b66e610f4b926b749e192
/*-*- 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 <systemd/sd-journal.h>
#include "log.h"
#include "util.h"
#include "build.h"
#include "pager.h"
#include "logs-show.h"
static bool arg_follow = false;
static bool arg_show_all = false;
static bool arg_no_pager = false;
static int arg_lines = -1;
static bool arg_no_tail = false;
static bool arg_new_id128 = false;
static int help(void) {
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
"Send control commands to or query the journal.\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"
" -n --lines=INTEGER Journal entries to show\n"
" --no-tail Show all lines, even in follow mode\n"
" -o --output=STRING Change journal output mode (short, short-monotonic,\n"
" verbose, export, json, cat)\n"
" --new-id128 Generate a new 128 Bit id\n",
return 0;
}
enum {
ARG_VERSION = 0x100,
};
};
int c, r;
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':
if (arg_output < 0) {
return -EINVAL;
}
break;
case 'a':
arg_show_all = true;
break;
case 'n':
if (r < 0 || arg_lines < 0) {
return -EINVAL;
}
break;
case ARG_NO_TAIL:
arg_no_tail = true;
break;
case ARG_NEW_ID128:
arg_new_id128 = true;
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
if (arg_follow && !arg_no_tail)
arg_lines = 10;
return 1;
}
static int generate_new_id128(void) {
int r;
unsigned i;
r = sd_id128_randomize(&id);
if (r < 0) {
return r;
}
printf("As string:\n"
SD_ID128_FORMAT_STR "\n\n"
"As UUID:\n"
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n\n"
"As macro:\n"
"#define MESSAGE_XYZ SD_ID128_MAKE(",
for (i = 0; i < 16; i++)
return 0;
}
int r, i, fd;
sd_journal *j = NULL;
unsigned line = 0;
bool need_seek = false;
log_open();
if (r <= 0)
goto finish;
if (arg_new_id128) {
r = generate_new_id128();
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;
}
if (arg_lines >= 0) {
r = sd_journal_seek_tail(j);
if (r < 0) {
goto finish;
}
r = sd_journal_previous_skip(j, arg_lines);
} else {
r = sd_journal_seek_head(j);
if (r < 0) {
goto finish;
}
r = sd_journal_next(j);
}
if (r < 0) {
goto finish;
}
if (!arg_no_pager && !arg_follow) {
columns();
pager_open();
}
if (arg_output == OUTPUT_JSON) {
}
for (;;) {
for (;;) {
if (need_seek) {
r = sd_journal_next(j);
if (r < 0) {
goto finish;
}
}
if (r == 0)
break;
line ++;
if (r < 0)
goto finish;
need_seek = true;
}
if (!arg_follow)
break;
if (r < 0) {
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;
}