chiark / gitweb /
7bcd842f6db5db8d3a2a1c8c3b19497210a00512
[elogind.git] / src / journal / journalctl.c
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>
25
26 #include "journal-private.h"
27
28 int main(int argc, char *argv[]) {
29         int r;
30         JournalFile *f;
31         Object *o = NULL;
32
33         log_parse_environment();
34         log_open();
35
36         r = journal_file_open("/var/log/journal/system.journal", O_RDONLY, 0644, &f);
37         if (r == -ENOENT)
38                 r = journal_file_open("/run/log/journal/system.journal", O_RDONLY, 0644, &f);
39
40         if (r < 0) {
41                 log_error("Failed to open journal: %s", strerror(-r));
42                 return EXIT_FAILURE;
43         }
44
45         for (;;) {
46                 uint64_t offset;
47                 uint64_t n, i;
48
49                 r = journal_file_next_entry(f, o, &o, &offset);
50                 if (r < 0) {
51                         log_error("Failed to read journal: %s", strerror(-r));
52                         goto finish;
53                 }
54
55                 if (r == 0)
56                         break;
57
58                 printf("entry: %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
59
60                 n = journal_file_entry_n_items(o);
61                 for (i = 0; i < n; i++) {
62                         uint64_t p, l;
63
64                         p = le64toh(o->entry.items[i].object_offset);
65                         r = journal_file_move_to_object(f, p, &o);
66                         if (r < 0) {
67                                 log_error("Failed to move to data: %s", strerror(-r));
68                                 goto finish;
69                         }
70
71                         if (le64toh(o->object.type) != OBJECT_DATA) {
72                                 log_error("Invalid file");
73                                 goto finish;
74                         }
75
76                         l = o->object.size - offsetof(Object, data.payload);
77                         printf("\t[%.*s]\n", (int) l, o->data.payload);
78
79                         r = journal_file_move_to_object(f, offset, &o);
80                         if (r < 0) {
81                                 log_error("Failed to move back to entry: %s", strerror(-r));
82                                 goto finish;
83                         }
84                 }
85         }
86
87 finish:
88         journal_file_close(f);
89
90         return 0;
91 }