chiark / gitweb /
5f17f45cac9e4a96ad490b0f375e66828ac0616f
[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-file.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, OBJECT_DATA, &o);
66                         if (r < 0) {
67                                 log_error("Failed to move to data: %s", strerror(-r));
68                                 goto finish;
69                         }
70
71                         l = o->object.size - offsetof(Object, data.payload);
72                         printf("\t[%.*s]\n", (int) l, o->data.payload);
73
74                         r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
75                         if (r < 0) {
76                                 log_error("Failed to move back to entry: %s", strerror(-r));
77                                 goto finish;
78                         }
79                 }
80         }
81
82 finish:
83         journal_file_close(f);
84
85         return 0;
86 }