summaryrefslogtreecommitdiff
path: root/src/journal/journalctl.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/journal/journalctl.c')
-rw-r--r--src/journal/journalctl.c125
1 files changed, 121 insertions, 4 deletions
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 6f4342e597..5a1cb6e88a 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -28,10 +28,13 @@
#include <stdlib.h>
#include <sys/poll.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
@@ -41,10 +44,11 @@ static enum {
OUTPUT_EXPORT,
OUTPUT_JSON,
_OUTPUT_MAX
-} arg_output = OUTPUT_JSON;
+} 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;
@@ -345,6 +349,96 @@ static int (*output_funcs[_OUTPUT_MAX])(sd_journal*j, unsigned line) = {
[OUTPUT_JSON] = output_json
};
+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",
+ program_invocation_short_name);
+
+ return 0;
+}
+
+static int parse_argv(int argc, char *argv[]) {
+
+ enum {
+ ARG_VERSION = 0x100,
+ ARG_NO_PAGER
+ };
+
+ static const struct option options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "version" , no_argument, NULL, ARG_VERSION },
+ { "no-pager", no_argument, NULL, ARG_NO_PAGER },
+ { "follow", no_argument, NULL, 'f' },
+ { "output", required_argument, NULL, 'o' },
+ { "all", no_argument, NULL, 'a' },
+ { NULL, 0, NULL, 0 }
+ };
+
+ int c;
+
+ assert(argc >= 0);
+ assert(argv);
+
+ while ((c = getopt_long(argc, argv, "hfo:a", options, NULL)) >= 0) {
+
+ switch (c) {
+
+ case 'h':
+ help();
+ return 0;
+
+ case ARG_VERSION:
+ puts(PACKAGE_STRING);
+ puts(DISTRIBUTION);
+ puts(SYSTEMD_FEATURES);
+ return 0;
+
+ case ARG_NO_PAGER:
+ arg_no_pager = true;
+ break;
+
+ case 'f':
+ arg_follow = true;
+ break;
+
+ case 'o':
+ if (streq(optarg, "short"))
+ arg_output = OUTPUT_SHORT;
+ else if (streq(optarg, "verbose"))
+ arg_output = OUTPUT_VERBOSE;
+ else if (streq(optarg, "export"))
+ arg_output = OUTPUT_EXPORT;
+ else if (streq(optarg, "json"))
+ arg_output = OUTPUT_JSON;
+ else {
+ log_error("Unknown output '%s'.", optarg);
+ 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 main(int argc, char *argv[]) {
int r, i, fd;
sd_journal *j = NULL;
@@ -356,13 +450,17 @@ int main(int argc, char *argv[]) {
log_parse_environment();
log_open();
+ r = parse_argv(argc, argv);
+ if (r <= 0)
+ goto finish;
+
r = sd_journal_open(&j);
if (r < 0) {
log_error("Failed to open journal: %s", strerror(-r));
goto finish;
}
- for (i = 1; i < argc; i++) {
+ for (i = optind; i < argc; i++) {
r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
if (r < 0) {
log_error("Failed to add match: %s", strerror(-r));
@@ -382,13 +480,30 @@ int main(int argc, char *argv[]) {
goto finish;
}
- if (arg_output == OUTPUT_JSON)
+ if (!arg_no_pager && !arg_follow) {
+ columns();
+ pager_open();
+ }
+
+ if (arg_output == OUTPUT_JSON) {
fputc('[', stdout);
+ fflush(stdout);
+ }
for (;;) {
struct pollfd pollfd;
- while (sd_journal_next(j) > 0) {
+ for (;;) {
+ r = sd_journal_next(j);
+
+ if (r < 0) {
+ log_error("Failed to iterate through journal: %s", strerror(-r));
+ goto finish;
+ }
+
+ if (r == 0)
+ break;
+
line ++;
r = output_funcs[arg_output](j, line);
@@ -426,5 +541,7 @@ finish:
if (j)
sd_journal_close(j);
+ pager_close();
+
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}