;;; yekneb-debug.el --- Debug utility functions for GNU Emacs. ;; Copyright © 2021 - 2025 Ben Key ;; This file is part of the yekneb package. ;; ;; The yekneb package 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 3 of the License, or (at your option) any ;; later version. ;; ;; The yekneb package 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 ;; the yekneb package. If not, see . (require 'cl-lib) (defconst yekneb-log-always -1) (defconst yekneb-log-min 0) (defconst yekneb-log-severe 0) (defconst yekneb-log-error 1) (defconst yekneb-log-warning 2) (defconst yekneb-log-info 3) (defconst yekneb-log-entry 4) (defconst yekneb-log-param 5) (defconst yekneb-log-debug 6) (defconst yekneb-log-hidebug 7) (defconst yekneb-log-all 7) (defconst yekneb-log-max 7) (defconst yekneb-log-level-names `("severe" "error" "warning" "info" "entry" "param" "debug" "hidebug")) (defvar yekneb-debug-level yekneb-log-error "The currently enabled debug level.") (defun yekneb-get-log-file-name () "Gets the path to the file that log messages will be written to." (expand-file-name "debug.log" user-emacs-directory) ) (defun yekneb-validate-debug-level (value low high) (cond ((or (< value low) (> value high)) low ) (t value ) ) ) (defun yekneb-get-debug-level-name (level) "Gets the name for the specified debug level." (cond ((or (< level yekneb-log-min) (> level yekneb-log-max)) (format "Unknown level %d" level) ) (t (nth level yekneb-log-level-names) ) ) ) (defun yekneb-set-debug-level (level) "Sets `yekneb-debug-level', with validation." (setq yekneb-debug-level (yekneb-validate-debug-level level yekneb-log-min yekneb-log-max)) (message "yekneb-debug-level set to %s" (yekneb-get-debug-level-name yekneb-debug-level)) ) (defun yekneb-update-debug-level () "Sets `yekneb-debug-level' to one more than the current value." (interactive) (yekneb-set-debug-level (+ yekneb-debug-level 1)) ) (defun yekneb-log (level log-message &rest format-params) "Log message." (when (<= level yekneb-debug-level) (setq log-message (eval (append '(format log-message) format-params))) (message "%s" log-message) (let ((save-silently t)) (write-region (format "%s\n" log-message) nil (yekneb-get-log-file-name) t 'quite) ) ) ) (provide 'yekneb-debug)