aboutsummaryrefslogtreecommitdiff
blob: f236af2fe3edf509d928d3b85d806023fb7ffdf1 (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
;;; elogt.el --- Portage Emerge log browser -*- lexical-binding: t -*-



;; Copyright 2023 Gentoo Authors


;; This file is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 2 of the License, or
;; (at your option) any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.


;; Authors: Maciej Barć <xgqt@gentoo.org>
;; Maintainer: Gentoo Emacs project <emacs@gentoo.org>
;; Created: 03 Feb 2023
;; Version: 0.0.0
;; Keywords: convenience
;; Homepage: https://gitweb.gentoo.org/proj/emacs-elogt.git
;; Package-Requires: ((emacs "25"))
;; SPDX-License-Identifier: GPL-2.0-or-later



;;; Commentary:

;; Portage Emerge log browser for GNU Emacs.

;; ElogT displays the Portage logs in a convenient table and allows log file
;; browsing and deletion.



;;; Code:


(defconst elogt-version "0.0.0"
  "ElogT version.")

(defconst elogt--portage-log-stars
  '((" \e[31;01m*\e[0m" . error)
    (" \e[32m*\e[0m"    . info)
    (" \e[33;01m*\e[0m" . warn)))


;; Customization

(defgroup elogt nil
  "Customization for ElogT, Portage Emerge log browser."
  :group 'ebuild)

(defcustom elogt-portage-log-dir "/var/log/portage"
  "Portage log directory location."
  :safe 'stringp
  :type 'file
  :group 'elogt)

(defcustom elogt-check-priority t
  "Check priority of each logfile.

If set to nil entries will have a phony Info level."
  :type 'boolean
  :group 'elogt)


;; Log file processing

(defun elogt--gather-portage-logs ()
  "Gather logs from ‘elogt-log-dir’."
  (directory-files elogt-portage-log-dir t "\\.log"))

(defun elogt--logfile-find-stars (file-name)
  "Find special info indicators in FILE-NAME."
  (let ((buffer (find-file-noselect file-name 'nowarn 'rawfile))
        (found-stars nil))
    (with-current-buffer buffer
      (dolist (star elogt--portage-log-stars)
        (save-excursion
          (goto-char (point-min))
          (let ((found (search-forward (car star) nil 'noerror)))
            (when found
              (push (cdr star) found-stars))))))
    (kill-buffer buffer)
    found-stars))

(defun elogt--logfile-priority-level (file-name)
  "Return a FILE-NAME priority level.

A logfile priority level is one of: None, Info, Warn, Error."
  (let ((found-stars (elogt--logfile-find-stars file-name)))
    (cond
     ((not elogt-check-priority)  "Info")
     ((null          found-stars) "None")
     ((member 'error found-stars) "Error")
     ((member 'warn  found-stars) "Warn")
     (t                           "Info"))))

(defun elogt--logfile-properties (file-name)
  "Extract the properties form given FILE-NAME."
  (apply #'vector (cons (elogt--logfile-priority-level file-name)
                        (split-string (file-name-base file-name) ":"))))

(defun elogt--make-log-table-contents ()
  "Make ElogT table contents."
  (let ((index 0))
    (mapcar (lambda (log-file-path)
              (let ((entry (list index
                                 (elogt--logfile-properties log-file-path))))
                (setq index (+ index 1))
                entry))
            (elogt--gather-portage-logs))))


;; Table interaction

(defun elogt--entry-logfile-path (table-entry)
  "Return a logfile path of a given ElogT TABLE-ENTRY."
  (format "%s/%s:%s:%s.log"
          elogt-portage-log-dir
          (aref table-entry 1)
          (aref table-entry 2)
          (aref table-entry 3)))

(defun elogt--get-table-entry-logfile-path ()
  "Return a logfile path of current ElogT table entry."
  (elogt--entry-logfile-path (tabulated-list-get-entry)))

(defun elogt--open-entry-file ()
  "Open a ElogT table entry at point.

Return opened buffer (done via `find-file')."
  (find-file (elogt--get-table-entry-logfile-path)))

(defun elogt-open-entry ()
  "Open specified ElogT table entry and put it in a mode for viewing only."
  (interactive)
  (let ((buffer (elogt--open-entry-file)))
    (with-current-buffer buffer
      (fundamental-mode)
      (view-mode))))

(defun elogt--delete-entry-file ()
  "Delete a logfile of a entry at point."
  (delete-file (elogt--get-table-entry-logfile-path)))

(defun elogt-delete-entry ()
  "Delete specified ElogT table entry."
  (interactive)
  (elogt--delete-entry-file)
  (tabulated-list-delete-entry))

(defun elogt-refresh-table ()
  "Refresh the ElogT table."
  (interactive)
  (message "Refreshing the ElogT table, please wait...")
  (setq tabulated-list-entries (elogt--make-log-table-contents))
  (tabulated-list-init-header)
  (tabulated-list-print t)
  (message "The ElogT table is ready."))


;; Major mode

(defvar elogt-mode-hook nil
  "Hook for `elogt' major mode.")

(defvar elogt-mode-map
  (let ((elogt-mode-map (make-keymap)))
    (define-key elogt-mode-map (kbd "/") #'isearch-forward)
    (define-key elogt-mode-map (kbd "o") #'elogt-open-entry)
    (define-key elogt-mode-map (kbd "d") #'elogt-delete-entry)
    (define-key elogt-mode-map (kbd "g") #'elogt-refresh-table)
    elogt-mode-map)
  "Key map for `elogt' major mode.")

(define-derived-mode elogt-mode tabulated-list-mode
  "ElogT"
  "Major mode for browsing Portage log files."
  (setq tabulated-list-format
        [("Priority"  10 t)
         ("Category"  20 t)
         ("Package"   30 t)
         ("Time"      20 t)])
  (setq tabulated-list-sort-key (cons "Time" t))
  (run-hooks 'elogt-mode-hook)
  (use-local-map elogt-mode-map))


;; Main provided features

;;;###autoload
(defun elogt ()
  "Browse Portage log files."
  (interactive)
  (let ((buffer (get-buffer-create "*ElogT*")))
    (with-current-buffer buffer
      (elogt-mode)
      (elogt-refresh-table))
    (display-buffer buffer)))


(provide 'elogt)



;;; elogt.el ends here