diff options
Diffstat (limited to 'elisp/journalctl.el')
-rw-r--r-- | elisp/journalctl.el | 111 |
1 files changed, 62 insertions, 49 deletions
diff --git a/elisp/journalctl.el b/elisp/journalctl.el index 2d16240..84ec1e1 100644 --- a/elisp/journalctl.el +++ b/elisp/journalctl.el @@ -7,8 +7,8 @@ ;; Created: September 07, 2022 ;; Modified: September 07, 2022 ;; Version: 0.0.1 -;; Homepage: https://github.com/titan/journalctl -;; Package-Requires: ((emacs "27.1")) +;; Homepage: https://git.oscarnajera.com/dotfiles/tree/elisp/journalctl.el +;; Package-Requires: ((emacs "28.1")) ;; ;; This file is not part of GNU Emacs. ;; @@ -19,18 +19,13 @@ ;;; Code: (require 'org) (require 'cl-extra) +(require 'transient) (defvar-local journalctl-current-host nil - "Keeps the optetes of the last call of journalctl.") + "Keeps the optetes of the last call to 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" "FATAL") - "Keywords that mark errors in journalctl output." - :group 'journalctl - :type 'string) + "Keeps the optetes of the last call to journalctl.") (defcustom journalctl-warn-keywords '("Warning" "warn" "debug") @@ -38,6 +33,12 @@ :group 'journalctl :type 'string) +(defcustom journalctl-error-keywords + '("Failed" "failed" "Error" "error" "critical" "couldn't" "Can't" "not" "Not" "unreachable" "FATAL") + "Keywords that mark errors 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." @@ -52,16 +53,16 @@ :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-error-face + '((t :inherit error)) + "Face to mark errors in journalctl's output." + :group 'journalctl) + (defface journalctl-starting-face '((t :inherit success)) "Face to mark starting units in journalctl's output." @@ -88,30 +89,24 @@ :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:]+\\) \\(\\(?:[[:alnum:]]\\|\\.\\)+\\) \\(.*?:\\)" - (1 'journalctl-timestamp-face) - (2 'journalctl-host-face) - (3 'journalctl-process-face)) - - ;; note: order above matters, because once colored, that part won't change. - ;; in general, put longer words first - ))) + ;; note: order matters, because once colored, that part won't change. + ;; in general, put longer words first + `((,(regexp-opt journalctl-warn-keywords 'words) . 'journalctl-warning-face) + (,(regexp-opt journalctl-error-keywords 'words) . 'journalctl-error-face) + (,(regexp-opt journalctl-starting-keywords 'words) . 'journalctl-starting-face) + (,(regexp-opt journalctl-finished-keywords 'words) . 'journalctl-finished-face) + (,(rx bol (group (= 3 alpha) " " (= 2 digit) " " (1+ (in digit ":"))) " " ; timestamp + (group (+ (in alphanumeric ?.))) " " ; host + (group (+? not-newline)) "[" (group (+ digit)) "]:") ; service[PID] + (1 'journalctl-timestamp-face) + (2 'journalctl-host-face) + (3 'journalctl-process-face) + (4 'font-lock-comment-face)))) (defcustom journalctl-hosts - '("/ssh:alina|sudo::" - "/ssh:nina|sudo::" - "/sudo::") + '("/sudo::" + "/ssh:ingrid|sudo::" + "/ssh:sarah|sudo::") "Valid hosts to connect for journal data." :type (list 'string) :group 'file) @@ -121,6 +116,8 @@ "--follow" "--lines" "--reverse" + "--pager-end" + "--catalog" "--grep" "--boot" "--since" @@ -129,6 +126,18 @@ "--priority") "List of possible options to be given to journalctl." ) +(transient-define-prefix journalct-opts () + "Prefix for opts." + ["infixes" + ("f" "follow" "--follow") + ("h" "host" "--host=" :choices journalctl-hosts :prompt "hus: ") + ("u" "unit" "--unit=" + :prompt "a unit: " + :reader journalctl-read-system-units + ;; :always-read t + )]) +;; (journalct-opts) + (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." @@ -138,17 +147,21 @@ It seems I must kill the buffer for tramp to behave correctly on the new calls." (kill-buffer buffer)) (get-buffer-create name-buffer))) -(defun journalctl-system-units (host-location) +(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: "))))) + (delq nil))))) + +(defun journalctl-read-system-units (_prompt host-location history) + "Query HOST-LOCATION (a tramp path) for its systemd units, given command HISTORY." + (completing-read "Unit: " + (journalctl--system-units host-location) + nil nil nil history)) (defun journalctl-remove-opt (opt) "Remove an OPT flag from the journal query." @@ -167,8 +180,8 @@ If OPT is set, remove this option." (thread-last (pcase opt ((or "--since" "--until") (org-read-date t)) - ((or "--follow" "--reverse" "--user")) - ("--unit" (journalctl-system-units host)) + ((or "--follow" "--reverse" "--user" "--pager-end" "--catalog")) + ("--unit" (journalctl-read-system-units nil host nil)) (_ (read-string (concat opt "= ")))) (list opt) (list) @@ -176,7 +189,7 @@ If OPT is set, remove this option." (journalctl host)))) (defun journalctl-edit-opts () - "Edit the value of 'journalctl-current-opts'." + "Edit the value of `journalctl-current-opts'." (interactive) (let* ((host journalctl-current-host) (opts journalctl-current-opts) @@ -188,8 +201,7 @@ If OPT is set, remove this option." (lambda () (interactive) (goto-char (point-min)) - (let ((standard-input (current-buffer))) - (journalctl host (read))) + (journalctl host (read (current-buffer))) (kill-buffer edit-buff))) (switch-to-buffer (current-buffer))))) @@ -209,9 +221,10 @@ If OPT is set, remove this option." (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 ((picked-host (completing-read "Tramp host: " journalctl-hosts))) + (list picked-host + `(("--unit" ,(journalctl-read-system-units nil picked-host nil)) ("--follow") + ("--lines" "100"))))) (let ((buffer (journalctl--clean-buffer)) (default-directory host)) (apply #'start-file-process "Journal" buffer "journalctl" (flatten-tree options)) |