]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/journalctl.c
journal: add inline compression support with XZ
[thirdparty/systemd.git] / src / journal / journalctl.c
CommitLineData
87d2c1ff
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2011 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <fcntl.h>
23#include <errno.h>
24#include <stddef.h>
3fbf9cbb
LP
25#include <string.h>
26#include <stdio.h>
27#include <unistd.h>
28#include <stdlib.h>
50f20cfd 29#include <sys/poll.h>
87d2c1ff 30
3fbf9cbb
LP
31#include "sd-journal.h"
32#include "log.h"
250d54b5 33
50f20cfd
LP
34static bool arg_follow = true;
35
87d2c1ff 36int main(int argc, char *argv[]) {
50f20cfd 37 int r, i, fd;
3fbf9cbb
LP
38 sd_journal *j = NULL;
39
40 log_set_max_level(LOG_DEBUG);
41 log_set_target(LOG_TARGET_CONSOLE);
87d2c1ff
LP
42
43 log_parse_environment();
44 log_open();
45
3fbf9cbb 46 r = sd_journal_open(&j);
87d2c1ff
LP
47 if (r < 0) {
48 log_error("Failed to open journal: %s", strerror(-r));
3fbf9cbb 49 goto finish;
87d2c1ff
LP
50 }
51
de7b95cd
LP
52 for (i = 1; i < argc; i++) {
53 r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
54 if (r < 0) {
55 log_error("Failed to add match: %s", strerror(-r));
56 goto finish;
57 }
58 }
59
50f20cfd
LP
60 fd = sd_journal_get_fd(j);
61 if (fd < 0) {
62 log_error("Failed to get wakeup fd: %s", strerror(-fd));
63 goto finish;
64 }
8725d60a 65
50f20cfd
LP
66 r = sd_journal_seek_head(j);
67 if (r < 0) {
68 log_error("Failed to seek to head: %s", strerror(-r));
69 goto finish;
70 }
87d2c1ff 71
50f20cfd
LP
72 for (;;) {
73 struct pollfd pollfd;
74
75 while (sd_journal_next(j) > 0) {
76 const void *data;
77 size_t length;
78 char *cursor;
79 uint64_t realtime = 0, monotonic = 0;
80
81 r = sd_journal_get_cursor(j, &cursor);
82 if (r < 0) {
83 log_error("Failed to get cursor: %s", strerror(-r));
84 goto finish;
85 }
86
87 printf("entry: %s\n", cursor);
88 free(cursor);
89
90 sd_journal_get_realtime_usec(j, &realtime);
91 sd_journal_get_monotonic_usec(j, &monotonic, NULL);
92 printf("realtime: %llu\n"
93 "monotonic: %llu\n",
94 (unsigned long long) realtime,
95 (unsigned long long) monotonic);
96
97 SD_JOURNAL_FOREACH_DATA(j, data, length)
98 printf("\t%.*s\n", (int) length, (const char*) data);
87d2c1ff
LP
99 }
100
50f20cfd
LP
101 if (!arg_follow)
102 break;
103
104 zero(pollfd);
105 pollfd.fd = fd;
106 pollfd.events = POLLIN;
87d2c1ff 107
50f20cfd
LP
108 if (poll(&pollfd, 1, -1) < 0) {
109 if (errno == EINTR)
110 break;
c2373f84 111
50f20cfd
LP
112 log_error("poll(): %m");
113 r = -errno;
114 goto finish;
115 }
8725d60a 116
50f20cfd
LP
117 r = sd_journal_process(j);
118 if (r < 0) {
119 log_error("Failed to process: %s", strerror(-r));
120 goto finish;
121 }
de190aef 122 }
87d2c1ff
LP
123
124finish:
3fbf9cbb
LP
125 if (j)
126 sd_journal_close(j);
87d2c1ff 127
3fbf9cbb 128 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
87d2c1ff 129}