refactor(lib): doom-debug-mode: make verbosity selective

Now `doom-debug-mode` manipulates `doom-log-level` if you activate it
with a prefix arg, setting it to 1 by default, reducing its verbosity
and cutting down on noise in the logs.
This commit is contained in:
Henrik Lissner
2025-04-10 21:35:19 -04:00
parent c233aada0b
commit fb0dc4cc85
10 changed files with 96 additions and 88 deletions

View File

@ -246,7 +246,7 @@ unreadable. Returns the names of envvars that were changed."
(defun doom-run-hook (hook) (defun doom-run-hook (hook)
"Run HOOK (a hook function) with better error handling. "Run HOOK (a hook function) with better error handling.
Meant to be used with `run-hook-wrapped'." Meant to be used with `run-hook-wrapped'."
(doom-log "hook:%s: run %s" (or doom--hook '*) hook) (doom-log 2 "hook:%s: run %s" (or doom--hook '*) hook)
(condition-case-unless-debug e (condition-case-unless-debug e
(funcall hook) (funcall hook)
(error (error
@ -313,7 +313,7 @@ TRIGGER-HOOK is a list of quoted hooks and/or sharp-quoted functions."
(with-memoization (get 'doom-compile-function 'timer) (with-memoization (get 'doom-compile-function 'timer)
(run-with-idle-timer (run-with-idle-timer
1.5 t (fn! (when-let (fn (pop fns)) 1.5 t (fn! (when-let (fn (pop fns))
(doom-log "compile-functions: %s" fn) (doom-log 3 "compile-functions: %s" fn)
(or (if (featurep 'native-compile) (or (if (featurep 'native-compile)
(or (subr-native-elisp-p (indirect-function fn)) (or (subr-native-elisp-p (indirect-function fn))
(ignore-errors (native-compile fn)))) (ignore-errors (native-compile fn))))
@ -1170,7 +1170,7 @@ Never set this variable directly, use `with-doom-module'.")
(if key (if key
(doom-module-context key) (doom-module-context key)
(make-doom-module-context))))) (make-doom-module-context)))))
(doom-log ":context:module: =%s" doom-module-context) (doom-log 2 ":context:module: =%s" doom-module-context)
,@body)) ,@body))
(defun doom-module-context (key) (defun doom-module-context (key)

View File

@ -6,85 +6,120 @@
;;; Doom's debug mode ;;; Doom's debug mode
;;;###autoload ;;;###autoload
(defvar doom-debug-variables (defvar doom-debug--variables
`(;; Doom variables `(;; Doom variables
(doom-print-minimum-level . debug) (doom-print-minimum-level . debug)
(doom-inhibit-log . nil) (doom-inhibit-log . nil)
(doom-log-level . 2)
;; Emacs variables ;; Emacs variables
async-debug (async-debug t 2)
debug-on-error debug-on-error
gcmh-verbose (gcmh-verbose t 3)
init-file-debug init-file-debug
jka-compr-verbose (jka-compr-verbose t 3)
(message-log-max . 16384) (message-log-max 16384)
(native-comp-async-report-warnings-errors . silent) (native-comp-async-report-warnings-errors silent 2)
(native-comp-warning-on-missing-source . t) (native-comp-warning-on-missing-source t 2)
url-debug url-debug
use-package-verbose use-package-verbose
(warning-suppress-types . nil)) (warning-suppress-types nil)))
"A list of variable to toggle on `doom-debug-mode'.
Each entry can be a variable symbol or a cons cell whose CAR is the variable ;;;###autoload
symbol and CDR is the value to set it to when `doom-debug-mode' is activated.") (progn
(cl-defun set-debug-variable! (var &optional (debug-val t) (level 1))
"Set VAR to DEBUG-VAL (or `t') when `doom-debug-mode' is active at >=LEVEL."
(setf (alist-get var doom-debug--variables) (cons debug-val level))))
(defvar doom-debug--unbound-vars nil) (defvar doom-debug--unbound-variables nil)
(defun doom-debug--watch-vars-h (&rest _) (defun doom-debug--watch-vars-h (&rest _)
(when-let (vars (copy-sequence doom-debug--unbound-vars)) (when-let* ((vars (copy-sequence doom-debug--unbound-variables)))
(setq doom-debug--unbound-vars nil) (setq doom-debug--unbound-variables nil)
(mapc #'doom-debug--set-var vars))) (mapc #'doom-debug--set-var vars)))
(defun doom-debug--set-var (spec) (defun doom-debug--set-var (spec)
(cond ((listp spec) (cond ((listp spec)
(pcase-let ((`(,var . ,val) spec)) (pcase-let ((`(,var ,val ,level) spec))
(if (boundp var) (if (boundp var)
(set-default (set-default
var (if (not doom-debug-mode) var (if (or (not doom-debug-mode)
(> (or level 1) doom-log-level))
(prog1 (get var 'initial-value) (prog1 (get var 'initial-value)
(put var 'initial-value nil)) (put var 'initial-value nil))
(doom-log "debug:vars: %s = %S" var (default-toplevel-value var)) (doom-log 3 "debug:vars: %s = %S" var (default-toplevel-value var))
(put var 'initial-value (default-toplevel-value var)) (put var 'initial-value (default-toplevel-value var))
val)) val))
(add-to-list 'doom-debug--unbound-vars spec)))) (add-to-list 'doom-debug--unbound-variables spec))))
((boundp spec) ((boundp spec)
(doom-log "debug:vars: %s = %S" spec doom-debug-mode) (doom-log 3 "debug:vars: %s = %S" spec doom-debug-mode)
(set-default-toplevel-value spec doom-debug-mode)) (set-default-toplevel-value spec doom-debug-mode))
((add-to-list 'doom-debug--unbound-vars (cons spec t))))) ((add-to-list 'doom-debug--unbound-variables (cons spec t)))))
(defun doom-debug--timestamped-message-a (format-string &rest _args)
"Advice to run before `message' that prepends a timestamp to each message.
Activate this advice with:
(advice-add 'message :before 'doom-debug--timestamped-message-a)"
(when (and (stringp format-string)
message-log-max ; if nil, logging is disabled
(not (equal format-string "%s%s"))
(not (equal format-string "\n")))
(with-current-buffer "*Messages*"
(let ((timestamp (format-time-string "[%F %T] " (current-time)))
(deactivate-mark nil))
(with-silent-modifications
(goto-char (point-max))
(if (not (bolp))
(newline))
(insert timestamp))))
(let ((window (get-buffer-window "*Messages*")))
(when (and window (not (equal (selected-window) window)))
(with-current-buffer "*Messages*"
(goto-char (point-max))
(set-window-point window (point-max)))))))
;;;###autoload ;;;###autoload
(define-minor-mode doom-debug-mode (define-minor-mode doom-debug-mode
"Toggle `debug-on-error' and `init-file-debug' for verbose logging." "Toggle `debug-on-error' and `init-file-debug' for verbose logging."
:global t :global t
(let ((enabled doom-debug-mode)) (when (or doom-debug-mode
(doom-log "debug: enabled!") (and (integerp current-prefix-arg)
(mapc #'doom-debug--set-var doom-debug-variables) (> current-prefix-arg 0)))
;; Watch for changes in `doom-debug-variables', or when packages load (and (setq doom-debug-mode t)
;; potentially define one of `doom-debug-variables'), in case some of them (let ((level (max 1 (min 3 (or current-prefix-arg 1)))))
;; aren't defined when `doom-debug-mode' is first loaded. (put 'doom-log-level 'initial-value doom-log-level)
(cond (enabled (setq doom-log-level level)))
(unless noninteractive (doom-log "debug: enabled! (log-level=%d)" doom-log-level)
(message "Debug mode enabled! (Run 'M-x view-echo-area-messages' to open the log buffer)")) (mapc #'doom-debug--set-var doom-debug--variables)
;; Produce more helpful (and visible) error messages from errors ;; Watch for changes in `doom-debug--variables', or when packages load (and
;; emitted from hooks (particularly mode hooks), that usually go ;; potentially define one of `doom-debug--variables'), in case some of them
;; unnoticed otherwise. ;; aren't defined when `doom-debug-mode' is first loaded.
(advice-add #'run-hooks :override #'doom-run-hooks) (cond (doom-debug-mode
;; Add time stamps to lines in *Messages* (unless noninteractive
(advice-add #'message :before #'doom--timestamped-message-a) (message "Debug mode level %d enabled! (Run 'M-x view-echo-area-messages' to open the log buffer)"
;; The constant debug output from GC is mostly unhelpful. I still doom-log-level))
;; want it logged to *Messages*, just out of the echo area. ;; Produce more helpful (and visible) error messages from errors
(advice-add #'gcmh-idle-garbage-collect :around #'doom-debug-shut-up-a) ;; emitted from hooks (particularly mode hooks), that usually go
(add-variable-watcher 'doom-debug-variables #'doom-debug--watch-vars-h) ;; unnoticed otherwise.
(add-hook 'after-load-functions #'doom-debug--watch-vars-h)) (advice-add #'run-hooks :override #'doom-run-hooks)
(t ;; Add time stamps to lines in *Messages*
(advice-remove #'run-hooks #'doom-run-hooks) (advice-add #'message :before #'doom-debug--timestamped-message-a)
(advice-remove #'message #'doom--timestamped-message-a) ;; The constant debug output from GC is mostly unhelpful. I still
(advice-remove #'gcmh-idle-garbage-collect #'doom-debug-shut-up-a) ;; want it logged to *Messages*, just out of the echo area.
(remove-variable-watcher 'doom-debug-variables #'doom-debug--watch-vars-h) (advice-add #'gcmh-idle-garbage-collect :around #'doom-debug-shut-up-a)
(remove-hook 'after-load-functions #'doom-debug--watch-vars-h) (add-variable-watcher 'doom-debug--variables #'doom-debug--watch-vars-h)
(doom-log "debug: disabled") (add-hook 'after-load-functions #'doom-debug--watch-vars-h))
(message "Debug mode disabled!"))))) (t
(when-let* ((last-level (get 'doom-log-level 'initial-value)))
(put 'doom-log-level 'initial-value nil)
(setq doom-log-level last-level))
(advice-remove #'run-hooks #'doom-run-hooks)
(advice-remove #'message #'doom-debug--timestamped-message-a)
(advice-remove #'gcmh-idle-garbage-collect #'doom-debug-shut-up-a)
(remove-variable-watcher 'doom-debug--variables #'doom-debug--watch-vars-h)
(remove-hook 'after-load-functions #'doom-debug--watch-vars-h)
(doom-log "debug: disabled")
(message "Debug mode disabled!"))))
(defun doom-debug-shut-up-a (fn &rest args) (defun doom-debug-shut-up-a (fn &rest args)
"Suppress output from FN, even in debug mode." "Suppress output from FN, even in debug mode."
@ -163,33 +198,6 @@ symbol and CDR is the value to set it to when `doom-debug-mode' is activated.")
file))) file)))
;;
;;; Time-stamped *Message* logs
(defun doom--timestamped-message-a (format-string &rest _args)
"Advice to run before `message' that prepends a timestamp to each message.
Activate this advice with:
(advice-add 'message :before 'doom--timestamped-message-a)"
(when (and (stringp format-string)
message-log-max ; if nil, logging is disabled
(not (equal format-string "%s%s"))
(not (equal format-string "\n")))
(with-current-buffer "*Messages*"
(let ((timestamp (format-time-string "[%F %T] " (current-time)))
(deactivate-mark nil))
(with-silent-modifications
(goto-char (point-max))
(if (not (bolp))
(newline))
(insert timestamp))))
(let ((window (get-buffer-window "*Messages*")))
(when (and window (not (equal (selected-window) window)))
(with-current-buffer "*Messages*"
(goto-char (point-max))
(set-window-point window (point-max)))))))
;; ;;
;;; Hooks ;;; Hooks

View File

@ -42,8 +42,8 @@ This is controlled by `+format-on-save-disabled-modes'."
(add-hook 'lsp-managed-mode-hook #'+format-with-lsp-toggle-h)) (add-hook 'lsp-managed-mode-hook #'+format-with-lsp-toggle-h))
:config :config
(add-to-list 'doom-debug-variables '(apheleia-log-only-errors . nil)) (set-debug-variable! 'apheleia-log-only-errors nil)
(add-to-list 'doom-debug-variables '(apheleia-log-debug-info . t)) (set-debug-variable! 'apheleia-log-debug-info t 2)
(defadvice! +format--inhibit-reformat-on-prefix-arg-a (orig-fn &optional arg) (defadvice! +format--inhibit-reformat-on-prefix-arg-a (orig-fn &optional arg)
"Make it so \\[save-buffer] with prefix arg inhibits reformatting." "Make it so \\[save-buffer] with prefix arg inhibits reformatting."

View File

@ -31,7 +31,7 @@
(add-transient-hook! #'company-yasnippet (require 'yasnippet)) (add-transient-hook! #'company-yasnippet (require 'yasnippet))
:config :config
(add-to-list 'doom-debug-variables '(yas-verbosity . 3)) (set-debug-variable! 'yas-verbosity 3)
;; Allow private snippets in DOOMDIR/snippets ;; Allow private snippets in DOOMDIR/snippets
(add-to-list 'yas-snippet-dirs '+snippets-dir) (add-to-list 'yas-snippet-dirs '+snippets-dir)

View File

@ -21,7 +21,7 @@
(setq mu4e-maildir "~/.mail" (setq mu4e-maildir "~/.mail"
mu4e-user-mail-address-list nil)) mu4e-user-mail-address-list nil))
:config :config
(add-to-list 'doom-debug-variables 'mu4e-debug) (set-debug-variable! 'mu4e-debug)
;; mu4e now uses `display-buffer-alist' so we need to add some rules of our own ;; mu4e now uses `display-buffer-alist' so we need to add some rules of our own
(set-popup-rule! "^\\*mu4e-\\(main\\|headers\\)\\*" :ignore t) (set-popup-rule! "^\\*mu4e-\\(main\\|headers\\)\\*" :ignore t)
(set-popup-rule! "^\\*mu4e-log\\*" :select nil) (set-popup-rule! "^\\*mu4e-log\\*" :select nil)

View File

@ -1364,7 +1364,7 @@ between the two."
(run-hooks 'org-load-hook)) (run-hooks 'org-load-hook))
:config :config
(add-to-list 'doom-debug-variables 'org-export-async-debug) (set-debug-variable! 'org-export-async-debug)
(set-company-backend! 'org-mode 'company-capf) (set-company-backend! 'org-mode 'company-capf)
(set-eval-handler! 'org-mode #'+org-eval-handler) (set-eval-handler! 'org-mode #'+org-eval-handler)

View File

@ -3,7 +3,7 @@
(use-package! envrc (use-package! envrc
:hook (doom-first-file . envrc-global-mode) :hook (doom-first-file . envrc-global-mode)
:config :config
(add-to-list 'doom-debug-variables 'envrc-debug) (set-debug-variable! 'envrc-debug)
(set-popup-rule! "^\\*envrc\\*" :quit t :ttl 0) (set-popup-rule! "^\\*envrc\\*" :quit t :ttl 0)

View File

@ -32,7 +32,7 @@
;; Emacs GC is put under high pressure. ;; Emacs GC is put under high pressure.
(cl-callf plist-put eglot-events-buffer-config :size 0) (cl-callf plist-put eglot-events-buffer-config :size 0)
(add-to-list 'doom-debug-variables '(eglot-events-buffer-config :size 2000000 :format full)) (set-debug-variable! 'eglot-events-buffer-config '(:size 2000000 :format full))
(defadvice! +lsp--defer-server-shutdown-a (fn &optional server) (defadvice! +lsp--defer-server-shutdown-a (fn &optional server)
"Defer server shutdown for a few seconds. "Defer server shutdown for a few seconds.

View File

@ -59,7 +59,7 @@ Can be a list of backends; accepts any value `company-backends' accepts.")
(apply fn args)))) (apply fn args))))
:config :config
(add-to-list 'doom-debug-variables 'lsp-log-io) (set-debug-variable! 'lsp-log-io t 2)
(setq lsp-intelephense-storage-path (concat doom-data-dir "lsp-intelephense/") (setq lsp-intelephense-storage-path (concat doom-data-dir "lsp-intelephense/")
lsp-vetur-global-snippets-dir lsp-vetur-global-snippets-dir

View File

@ -48,7 +48,7 @@ FUNCTION
transient-values-file (concat doom-data-dir "transient/values") transient-values-file (concat doom-data-dir "transient/values")
transient-history-file (concat doom-data-dir "transient/history")) transient-history-file (concat doom-data-dir "transient/history"))
:config :config
(add-to-list 'doom-debug-variables 'magit-refresh-verbose) (set-debug-variable! 'magit-refresh-verbose)
(setq transient-default-level 5 (setq transient-default-level 5
magit-diff-refine-hunk t ; show granular diffs in selected hunk magit-diff-refine-hunk t ; show granular diffs in selected hunk