From 3db65ff5cb3d6f58b83052760b4ce04eb77db1bc Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Mon, 10 Mar 2025 19:02:48 -0700 Subject: [PATCH] Suppress log module when inserting date stamps in ERC * lisp/erc/erc-log.el (erc-truncate-buffer-on-save): Use `make-obsolete-variable' because `erc-truncate-buffer-on-save' is an option, not a function. Already mentioned in etc/ERC-NEWS. (erc-logging-enabled): Return nil if `log' appears in the `erc--skip' msg prop. * lisp/erc/erc-stamp.el (erc-stamp--defer-date-insertion-on-post-modify): Add `log' to `erc--skip' msg prop. This tells the log module to defer advancing the `erc-last-saved-position' marker until after stamps have been inserted, thus ensuring they're included in logs when `erc-log-write-after-insert' is non-nil. * test/lisp/erc/erc-scenarios-log.el (erc-scenarios-log--save-buffer-in-logs/truncate-on-save): Suppress warning for `erc-truncate-buffer-on-save'. (erc-scenarios-log--write-after-insert): New test. (Bug#76875) --- lisp/erc/erc-log.el | 4 +- lisp/erc/erc-stamp.el | 3 + test/lisp/erc/erc-scenarios-log.el | 116 ++++++++++++++++++++++++----- 3 files changed, 103 insertions(+), 20 deletions(-) diff --git a/lisp/erc/erc-log.el b/lisp/erc/erc-log.el index b960eaeca9d..173e6724a8f 100644 --- a/lisp/erc/erc-log.el +++ b/lisp/erc/erc-log.el @@ -124,7 +124,8 @@ custom function which returns the directory part and set (defcustom erc-truncate-buffer-on-save nil "Erase the contents of any ERC (channel, query, server) buffer when it is saved." :type 'boolean) -(make-obsolete 'erc-truncate-buffer-on-save 'erc-cmd-CLEAR "30.1") +(make-obsolete-variable 'erc-truncate-buffer-on-save + "maybe see command `erc-cmd-CLEAR'" "30.1") (defcustom erc-enable-logging t "If non-nil, ERC will log IRC conversations. @@ -322,6 +323,7 @@ is writable (it will be created as necessary) and (or buffer (setq buffer (current-buffer))) (and erc-log-channels-directory (not erc-log--save-in-progress-p) + (not (erc--memq-msg-prop 'erc--skip 'log)) (or (functionp erc-log-channels-directory) (if erc-log--check-writable-nocreate-p (file-writable-p erc-log-channels-directory) diff --git a/lisp/erc/erc-stamp.el b/lisp/erc/erc-stamp.el index 5b6550c4a88..c3ab155cb24 100644 --- a/lisp/erc/erc-stamp.el +++ b/lisp/erc/erc-stamp.el @@ -732,6 +732,9 @@ non-nil." (symbol (make-symbol "erc-stamp--insert-date")) (marker (setf (erc-stamp--date-marker data) (point-min-marker)))) (setf (erc-stamp--date-fn data) symbol) + ;; Disable logging in case `erc-log-write-after-insert' is in effect. + (when erc--msg-props + (push 'log (gethash 'erc--skip erc--msg-props))) (fset symbol (lambda (&rest _) (remove-hook hook-var symbol) diff --git a/test/lisp/erc/erc-scenarios-log.el b/test/lisp/erc/erc-scenarios-log.el index 1e95e98e073..3c95018dd26 100644 --- a/test/lisp/erc/erc-scenarios-log.el +++ b/test/lisp/erc/erc-scenarios-log.el @@ -332,18 +332,78 @@ (ert-deftest erc-scenarios-log--save-buffer-in-logs/truncate-on-save () :tags '(:expensive-test) + (with-suppressed-warnings ((obsolete erc-truncate-buffer-on-save)) + (erc-scenarios-common-with-cleanup + ((erc-scenarios-common-dialog "base/assoc/bouncer-history") + (dumb-server (erc-d-run "localhost" t 'foonet)) + (tempdir (make-temp-file "erc-tests-log." t nil nil)) + (erc-log-channels-directory tempdir) + (erc-modules (cons 'log erc-modules)) + (port (process-contact dumb-server :service)) + (erc-truncate-buffer-on-save t) + (logchan (expand-file-name (format "#chan!tester@127.0.0.1:%d.txt" + port) + tempdir)) + (erc-server-flood-penalty 0.1) + (erc-insert-timestamp-function #'erc-insert-timestamp-left) + (expect (erc-d-t-make-expecter))) + + (unless noninteractive + (add-hook 'kill-emacs-hook + (lambda () (delete-directory tempdir :recursive)))) + + (ert-info ("Connect to foonet") + (with-current-buffer (erc :server "127.0.0.1" + :port port + :nick "tester" + :password "foonet:changeme" + :full-name "tester") + (should (string= (buffer-name) (format "127.0.0.1:%d" port))))) + + (with-current-buffer (erc-d-t-wait-for 5 (get-buffer "#chan")) + (funcall expect 10 " [07:04:10] hi everyone") + (should-not (file-exists-p logchan)) + ;; Simulate an M-x erc-save-buffer-in-logs RET + (cl-letf (((symbol-function 'called-interactively-p) #'always)) + (call-interactively #'erc-save-buffer-in-logs)) + (should (file-exists-p logchan)) + (funcall expect 10 " bob: As't please your lordship") + (erc-save-buffer-in-logs) + ;; Not truncated when called by lisp code. + (should (> (buffer-size) 400))) + + (ert-info ("No double entries") + (with-temp-buffer + (insert-file-contents logchan) + (funcall expect 0.1 "hi everyone") + (funcall expect -0.1 "hi everyone") + (funcall expect 0.1 "Playback Complete") + (funcall expect -0.1 "Playback Complete") + (funcall expect 10 " bob: As't"))) + + (erc-log-mode -1) + (when noninteractive (delete-directory tempdir :recursive))))) + +(ert-deftest erc-scenarios-log--write-after-insert () + :tags '(:expensive-test :unstable) + (erc-scenarios-common-with-cleanup ((erc-scenarios-common-dialog "base/assoc/bouncer-history") (dumb-server (erc-d-run "localhost" t 'foonet)) (tempdir (make-temp-file "erc-tests-log." t nil nil)) (erc-log-channels-directory tempdir) - (erc-modules (cons 'log erc-modules)) + (erc-modules `(truncate log ,@erc-modules)) + (erc-max-buffer-size 512) + (erc-log-write-after-insert t) + (erc-timestamp-format-left "\n[@@DATE__STAMP@@]\n") + (erc-truncate-padding-size 512) (port (process-contact dumb-server :service)) - (erc-truncate-buffer-on-save t) (logchan (expand-file-name (format "#chan!tester@127.0.0.1:%d.txt" port) tempdir)) + (logserv (expand-file-name + (format "127.0.0.1:%d!tester@127.0.0.1:%d.txt" port port) + tempdir)) (erc-server-flood-penalty 0.1) - (erc-insert-timestamp-function #'erc-insert-timestamp-left) (expect (erc-d-t-make-expecter))) (unless noninteractive @@ -356,30 +416,48 @@ :nick "tester" :password "foonet:changeme" :full-name "tester") - (should (string= (buffer-name) (format "127.0.0.1:%d" port))))) + (should (string= (buffer-name) (format "127.0.0.1:%d" port))) + ;; File already exists because `erc-log-write-after-insert' is + ;; non-nil. + (should (file-exists-p logserv)) + (should-not (file-exists-p logchan)) + ;; Verify that truncation actually happens where it should. + (funcall expect 10 "*** MAXLIST=beI:60") + (should (= (pos-bol) 22)) + ;; Exactly two + 1 (for date stamp) newlines preserved. + (should (string-prefix-p "\n\n\n[" (buffer-string))))) + + (ert-info ("Log file ahead of truncation point") + ;; Log contains lines still present in buffer. + (with-temp-buffer + (insert-file-contents logserv) + (funcall expect 10 "@@DATE__STAMP@@") + (funcall expect 10 "*** MAXLIST=beI:60"))) (with-current-buffer (erc-d-t-wait-for 5 (get-buffer "#chan")) - (funcall expect 10 " [07:04:10] hi everyone") - (should-not (file-exists-p logchan)) - ;; Simulate an M-x erc-save-buffer-in-logs RET - (cl-letf (((symbol-function 'called-interactively-p) #'always)) - (call-interactively #'erc-save-buffer-in-logs)) + (funcall expect 10 "@@DATE__STAMP@@") + (funcall expect 10 "please your lordship") (should (file-exists-p logchan)) - (funcall expect 10 " bob: As't please your lordship") - (erc-save-buffer-in-logs) - ;; Not truncated when called by lisp code. - (should (> (buffer-size) 400))) + (funcall expect -0.1 "[07:04:37] alice: Here," (point-min))) - (ert-info ("No double entries") + (ert-info ("Log ahead of truncation point") (with-temp-buffer (insert-file-contents logchan) - (funcall expect 0.1 "hi everyone") - (funcall expect -0.1 "hi everyone") - (funcall expect 0.1 "Playback Complete") - (funcall expect -0.1 "Playback Complete") - (funcall expect 10 " bob: As't"))) + (funcall expect 10 "@@DATE__STAMP@@") + (funcall expect 1 "You have joined") + ;; No unwanted duplicates. + (funcall expect 1 " [07:04:37] alice: Here,") + (funcall expect -0.001 " [07:04:37] alice: Here,") + (funcall expect 1 " [07:04:42] bob: By my troth") + (funcall expect -0.001 " [07:04:42] bob: By my troth") + (funcall expect 1 "I will grant it") + + ;; Writes happen instantly because `erc-log-write-after-insert' + ;; is non-nil. Compare to `erc-scenarios-log--truncate' above. + (funcall expect 1 "loathed enemy"))) (erc-log-mode -1) + (erc-truncate-mode -1) (when noninteractive (delete-directory tempdir :recursive)))) ;;; erc-scenarios-log.el ends here