aboutsummaryrefslogtreecommitdiffstats
path: root/elisp/journalctl.el
blob: f36ebc1907d47c4f5393bf85d0c3a66f45d585df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
;;; journalctl.el --- Query journalctl -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022 Óscar Nájera
;;
;; Author: Óscar Nájera <hi@oscarnajera.com>
;; Maintainer: Óscar Nájera <hi@oscarnajera.com>
;; Created: September 07, 2022
;; Modified: September 07, 2022
;; Version: 0.0.1
;; Homepage: https://github.com/titan/journalctl
;; Package-Requires: ((emacs "27.1"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;;  Query journalctl
;;
;;; Code:
(require 'org)
(require 'cl-extra)

(defvar-local journalctl-current-host nil
  "Keeps the optetes of  the last call of journalctl.")

(defvar-local journalctl-current-opts nil
  "Keeps the optetes of  the last call of journalctl.")

(defcustom journalctl-error-keywords
  '("Failed" "failed" "Error" "error" "critical" "couldn't" "Can't" "not" "Not" "unreachable")
  "Keywords that mark errors in journalctl output."
  :group 'journalctl
  :type 'string)

(defcustom journalctl-warn-keywords
  '("Warning" "warn" "debug")
  "Keywords that mark warnings in journalctl output."
  :group 'journalctl
  :type 'string)

(defcustom journalctl-starting-keywords
  '("Starting" "Activating" "Listening" "Reloading" "connect")
  "Keywords that mark start of processes or steps in journalctl output."
  :group 'journalctl
  :type 'string)

(defcustom journalctl-finished-keywords
  '("Stopped" "Stopping" "Reached" "Closed" "finished" "Started" "Successfully activated"
    "Received" "opened" "success" "enabled" "removed" "active" "Created" "loaded" "detected")
  "Keywords that mark finished processes or steps in journalctl output."
  :group 'journalctl
  :type 'string)

;;; faces
(defface journalctl-error-face
  '((t :inherit error))
  "Face to mark errors in journalctl's output."
  :group 'journalctl)

(defface journalctl-warning-face
  '((t :inherit warning))
  "Face to mark warnings in journalctl's output."
  :group 'journalctl)

(defface journalctl-starting-face
  '((t :inherit success))
  "Face to mark starting units in journalctl's output."
  :group 'journalctl)

(defface journalctl-finished-face
  '((t :inherit success :bold t))
  "Face to mark finished units in journalctl's output."
  :group 'journalctl)

(defface journalctl-timestamp-face
  '((t :inherit font-lock-type-face))
  "Face for timestamps in journalctl's output."
  :group 'journalctl)

(defface journalctl-host-face
  '((t :inherit font-lock-keyword-face))
  "Face for hosts in journalctl's output."
  :group 'journalctl)

(defface journalctl-process-face
  '((t :inherit font-lock-function-name-face))
  "Face for hosts in journalctl's output."
  :group 'journalctl)

(defvar journalctl-font-lock-keywords
  (let* (
         ;; generate regex string for each category of keywords
	 (error-keywords-regexp (regexp-opt journalctl-error-keywords 'words))
	 (warn-keywords-regexp (regexp-opt journalctl-warn-keywords 'words))
	 (starting-keywords-regexp (regexp-opt journalctl-starting-keywords 'words))
	 (finished-keywords-regexp (regexp-opt journalctl-finished-keywords 'words)))
    `(
      (,warn-keywords-regexp . 'journalctl-warning-face)
      (,error-keywords-regexp . 'journalctl-error-face)
      (,starting-keywords-regexp . 'journalctl-starting-face)
      (,finished-keywords-regexp . 'journalctl-finished-face)
      ("^\\([A-Z][a-z]+ [0-9]+ [0-9:]+\\)" . (1 'journalctl-timestamp-face))
      ("^\\([A-Z][a-z]+ [0-9]+ [0-9:]+\\) \\(\\(?:[[:alnum:]]\\|\\.\\)+\\)" . (2 'journalctl-host-face))
      ("^\\([A-Z][a-z]+ [0-9]+ [0-9:]+\\) \\(\\(?:[[:alnum:]]\\|\\.\\)+\\) \\(.*?:\\)" . (3 'journalctl-process-face))

      ;; note: order above matters, because once colored, that part won't change.
      ;; in general, put longer words first
      )))
(defcustom journalctl-hosts
  '("/ssh:alina|sudo::"
    "/ssh:nina|sudo::"
    "/sudo::")
  "Valid hosts to connect for journal data."
  :type (list 'string)
  :group 'file)

(defconst journalctl-list-of-options
  '("--unit"
    "--follow"
    "--lines"
    "--reverse"
    "--grep"
    "--boot"
    "--since"
    "--until"
    "--user"
    "--priority")
  "List of possible options to be given to journalctl." )

(defun journalctl--clean-buffer ()
  "Produce a clean buffer for the log.
It seems I must kill the buffer for tramp to behave correctly on the new calls."
  (let* ((name-buffer "JOURNAL LOG")
         (buffer (get-buffer name-buffer)))
    (when (buffer-live-p buffer)
      (kill-buffer buffer))
    (get-buffer-create name-buffer)))

(defun journalctl-system-units (host-location)
  "Query HOST-LOCATION (a tramp path) for its systemd units."
  (interactive (list (completing-read "Tramp host: " journalctl-hosts)))
  (let ((default-directory host-location))
    (with-temp-buffer
      (start-file-process "units" (current-buffer) "systemctl" "list-units" "--all" "--quiet" "--full")
      (sit-for 0.1)
      (thread-last (split-string (buffer-string) "\n")
                   (mapcar (lambda (line) (car (split-string line)) ))
                   (delq nil)
                   (completing-read "Unit: ")))))

(defun journalctl (host options)
  "Query the log of HOST given OPTIONS."
  (interactive
   (let* ((picked-host (completing-read "Tramp host: " journalctl-hosts))
          (default-options `(("--unit" ,(journalctl-system-units picked-host)) ("--follow"))))
     (list picked-host default-options)))
  (let ((buffer (journalctl--clean-buffer))
        (default-directory host))
    (apply #'start-file-process "Journal" buffer "journalctl" (flatten-tree options))
    (with-current-buffer buffer
      (journalctl-mode)
      (setq-local journalctl-current-host host)
      (setq-local journalctl-current-opts options))
    (switch-to-buffer buffer)))

(defun journalctl-remove-opt (opt)
  "Remove an OPT flag from the journal query."
  (interactive (list (completing-read "remove option" (mapcar #'car journalctl-current-opts) nil t)))
  (let ((new-opts (delq (assoc opt journalctl-current-opts) journalctl-current-opts))
        (host journalctl-current-host))
    (journalctl host new-opts)))

(defun journalctl-add-opt (opt)
  "Remove option from journalctl call.

If OPT is set, remove this option."
  (interactive (list (completing-read "add option: " journalctl-list-of-options nil t)))
  (let ((opts (delq (assoc opt journalctl-current-opts) journalctl-current-opts))
        (host journalctl-current-host))
    (thread-last
      (pcase opt
        ((or "--since" "--until") (org-read-date t))
        ((or "--follow" "--reverse" "--user"))
        ("--unit" (journalctl-system-units host))
        (_ (read-string (concat opt "= "))))
      (list opt)
      (list)
      (append opts)
      (journalctl host))))

(defun  journalctl-edit-opts ()
  "Edit the value of 'journalctl-current-opts'."
  (interactive)
  (let* ((host journalctl-current-host)
         (opts journalctl-current-opts)
         (edit-buff (get-buffer-create "Edit Journalctl options")))
    (with-current-buffer edit-buff
      (emacs-lisp-mode)
      (cl-prettyprint opts)
      (local-set-key "\C-c\C-c"
                     (lambda ()
                       (interactive)
                       (goto-char (point-min))
                       (let ((standard-input (current-buffer)))
                         (journalctl host (read)))
                       (kill-buffer edit-buff)))
      (switch-to-buffer (current-buffer)))))


;;; keymap
(defvar journalctl-mode-map
  (let ((map (make-keymap "journalctl")))
    (define-key map (kbd "n") 'journalctl-next-chunk)
    (define-key map (kbd "p") 'journalctl-previous-chunk)
    (define-key map (kbd "+")  'journalctl-add-opt)
    (define-key map (kbd "-")  'journalctl-remove-opt)
    (define-key map (kbd "e") 'journalctl-edit-opts)
    (define-key map (kbd "q")  'kill-current-buffer)
    map)
  "Keymap for journalctl mode.")

;;;###autoload
(define-derived-mode journalctl-mode fundamental-mode "journalctl"
  "Major mode for viewing journalctl output."
  ;; code for syntax highlighting
  (setq font-lock-defaults '((journalctl-font-lock-keywords))))

(provide 'journalctl)
;;; journalctl.el ends here