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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
;;; 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, files
;; Homepage: https://gitweb.gentoo.org/proj/emacs-elogt.git
;; Package-Requires: ((emacs "27.1"))
;; 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:
(require 'ansi-color)
(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)))
(defconst elogt--priority-level-indicators
`((error . ,(propertize "Error" 'font-lock-face '(:foreground "red")))
(info . ,(propertize "Info" 'font-lock-face '(:foreground "green")))
(warn . ,(propertize "Warn" 'font-lock-face '(:foreground "orange")))
(none . ,(propertize "None" 'font-lock-face '(:foreground "purple")))))
;; 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--priority-level-property (key)
"Rerun a value referenced by KEY of ‘elogt--priority-level-indicators’."
(cdr (assoc key elogt--priority-level-indicators)))
(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) (elogt--priority-level-property 'info))
((null found-stars) (elogt--priority-level-property 'none))
((member 'error found-stars) (elogt--priority-level-property 'error))
((member 'warn found-stars) (elogt--priority-level-property 'warn))
(t (elogt--priority-level-property 'info)))))
(defun elogt--file-size (file-path)
"Return size of a given file at FILE-PATH."
(file-size-human-readable-iec
(file-attribute-size (file-attributes file-path))))
(defun elogt--logfile-properties (file-name)
"Extract the properties form given FILE-NAME."
(let ((splitted-file-name (split-string (file-name-base file-name) ":")))
(cond
((= (length splitted-file-name) 3)
(let ((category (car splitted-file-name))
(package (cadr splitted-file-name))
(time (caddr splitted-file-name)))
(vector (elogt--logfile-priority-level file-name)
category
(propertize package 'face 'bold)
(propertize time 'face 'italic)
(elogt--file-size file-name))))
(t
(make-vector 5 "")))))
(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)
(substring-no-properties (aref table-entry 2))
(substring-no-properties (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)
(ansi-color-apply-on-region (point-min) (point-max) 'preserve-sequences)
(read-only-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."))
(defun elogt-sort-package ()
"Sort the ElogT table by package, ascending."
(interactive)
(tabulated-list-sort 2))
(defun elogt-sort-category ()
"Sort the ElogT table by category, ascending."
(interactive)
(tabulated-list-sort 1))
(defun elogt-sort-priority ()
"Sort the ElogT table by priority, ascending."
(interactive)
(tabulated-list-sort 0))
(defun elogt-sort-time ()
"Sort the ElogT table by time, descending."
(interactive)
(tabulated-list-sort 3))
;; Major mode
(defvar elogt-mode-hook nil
"Hook for `elogt' major mode.")
(defvar elogt-mode-map
(let ((elogt-mode-map (make-keymap)))
;; Standard key bindings.
(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)
;; Elogv key bindings.
(define-key elogt-mode-map (kbd "A") #'elogt-sort-package)
(define-key elogt-mode-map (kbd "a") #'elogt-sort-category)
(define-key elogt-mode-map (kbd "c") #'elogt-sort-priority)
(define-key elogt-mode-map (kbd "t") #'elogt-sort-time)
;; Miscellaneous key bindings.
(define-key elogt-mode-map (kbd "/") #'isearch-forward)
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)
("Size" 10 t)])
(setq tabulated-list-sort-key '("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
|