Skip to content

Commit 6427841

Browse files
committed
Add: debug-warn macro
1 parent 555a32e commit 6427841

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

README.org

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,14 @@ The primary function provided is: ~(datetime-format SYM-OR-FMT &optional TIME &r
887887
There are several other symbols provided besides ~atom~, such as ~rfc-3339~, which formats dates according to that RFC.
888888

889889
** Debugging :debugging:
890+
:PROPERTIES:
891+
:TOC: :depth 2 :include descendants
892+
:END:
893+
:CONTENTS:
894+
- [[#tools][Tools]]
895+
- [[#edebug][Edebug]]
896+
- [[#debug-warn-macro][debug-warn macro]]
897+
:END:
890898

891899
*** Tools :tools:
892900

@@ -919,6 +927,74 @@ Declaring ~debug~ forms for functions and macros that take keyword arguments can
919927

920928
Probably should first replace the ~:bindings~ part with this, which correctly matches ~let~ bindings: ~(&rest &or symbolp (gate symbolp &optional def-form))~.
921929

930+
**** =debug-warn= macro
931+
932+
This macro simplifies print-style debugging by automatically including the names of the containing function and argument forms, rather than requiring the programmer to write =format= strings manually.
933+
934+
For example, when used like:
935+
936+
#+BEGIN_SRC elisp :exports code
937+
(defun argh (var)
938+
(debug-warn (current-buffer) "This is bad!" (point) var)
939+
var)
940+
941+
(setq warning-minimum-log-level :debug)
942+
(argh 1)
943+
#+END_SRC
944+
945+
This warning would be shown in the =*Warnings*= buffer:
946+
947+
#+BEGIN_EXAMPLE
948+
Debug (argh): (CURRENT-BUFFER):*scratch* This is bad! (POINT):491845 VAR:1
949+
#+END_EXAMPLE
950+
951+
The macro is not tangled to =epdh.el=, so you may add it to your code as necessary.
952+
953+
#+BEGIN_SRC elisp :exports code
954+
(cl-defmacro debug-warn (&rest args)
955+
"Display a debug warning showing the runtime value of ARGS.
956+
The warning automatically includes the name of the containing
957+
function, and it is only displayed if `warning-minimum-log-level'
958+
is `:debug' at runtime (which avoids formatting messages that
959+
won't be shown).
960+
961+
Each of ARGS may be a string, which is displayed as-is, or a
962+
symbol, the value of which is displayed prefixed by its name, or
963+
a Lisp form, which is displayed prefixed by its first symbol.
964+
965+
Before the actual ARGS arguments, you can write keyword
966+
arguments, i.e. alternating keywords and values. The following
967+
keywords are supported:
968+
969+
:buffer BUFFER Name of buffer to pass to `display-warning'.
970+
:level LEVEL Level passed to `display-warning', which see.
971+
Default is :debug."
972+
(pcase-let* ((fn-name (with-current-buffer
973+
(or byte-compile-current-buffer (current-buffer))
974+
;; This is a hack, but a nifty one.
975+
(save-excursion
976+
(beginning-of-defun)
977+
(cl-second (read (current-buffer))))))
978+
(plist-args (cl-loop while (keywordp (car args))
979+
collect (pop args)
980+
collect (pop args)))
981+
((map (:buffer buffer) (:level level)) plist-args)
982+
(level (or level :debug))
983+
(string (cl-loop for arg in args
984+
concat (pcase arg
985+
((pred stringp) "%s ")
986+
((pred symbolp)
987+
(concat (upcase (symbol-name arg)) ":%s "))
988+
((pred listp)
989+
(concat "(" (upcase (symbol-name (car arg)))
990+
(pcase (length arg)
991+
(1 ")")
992+
(_ "...)"))
993+
":%s "))))))
994+
`(when (eq :debug warning-minimum-log-level)
995+
(display-warning ',fn-name (format ,string ,@args) ,level ,buffer))))
996+
#+END_SRC
997+
922998
** Destructuring :destructuring:
923999

9241000
See [[id:b699e1a1-e34c-4ce8-a5dd-41161d2a1cbf][Pattern matching]].

0 commit comments

Comments
 (0)