I do have an Emacs command which adds a separator line, in log files, when there is more than 3 seconds between timestamps found on 2 consecutive lines:
(defun log-mode-display-separator-line ()
"Separate blocks of log lines with an horizontal line."
(interactive)
(save-excursion
(goto-char (point-min))
(let (last ov)
(while (re-search-forward "[0-9]\\{4\\}-[01][1-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]" nil t)
(when last
(when (> (- (float-time (date-to-time (match-string 0)))
(float-time (date-to-time last)))
3)
(setq ov (make-overlay (line-beginning-position) (line-end-position)))
(overlay-put ov 'face 'log-mode-separator-face)))
(setq last (match-string 0))))))
The log-mode-separator-face
uses the overline attribute to visually put an horizontal line between "semantic" blocks of lines.
I would like to get that horizontal line automatically inserted when the buffer is reverted (every 5 seconds), but the processing may become too big on huge log files, which are constantly updated. As of now, the whole file is scanned from the beginning each time we call that command...
How can I realize this, then? (With Emacs, no doubt it is...)
First of all, there's no guarantee that the buffer is reverted every 5 seconds. Since Emacs 24.4, autorevert uses file notifications, that means the buffer is reverted whenever the file changes on disk. If you want to suppress this, set auto-revert-use-notify
to nil.
Further on, it depends how you want to revert your log file. I suppose you use auto-revert-tail-mode
. This mode calls the hook functions contained in before-revert-hook
and after-revert-hook
. As a strategy, I would write a function for after-revert-hook
, which underlines the last line in your buffer to be reverted. Something like this:
(defun my-auto-revert-tail-mode-display-separator-line ()
"Separate blocks of log lines with an horizontal line."
(save-excursion
(goto-char (point-max))
(while (and (= (line-beginning-position) (line-end-position))
(> (point) (point-min)))
(forward-line -1))
(let ((ov (make-overlay (line-beginning-position) (line-end-position))))
(overlay-put ov 'face 'underline))))
(add-hook 'after-revert-hook 'my-auto-revert-tail-mode-display-separator-line)