about summary refs log tree commit diff
path: root/exwm-debug.el
diff options
context:
space:
mode:
authorAdrián Medraño Calvo <adrian@medranocalvo.com>2018-08-13T12·00+0000
committerAdrián Medraño Calvo <adrian@medranocalvo.com>2018-08-13T12·00+0000
commit8d15a39c4d4928d9bc38bed63e2bb85e4536af45 (patch)
tree055156597eb21aca3a42c4190ef88373a276d8b0 /exwm-debug.el
parent4d43e3119a2d0cb002d87340cd11c1d372ad126e (diff)
Print log output to an EXWM-specific messages buffer
Using `message' to log debugging information is cumbersome, as the output
appears constantly in the minibuffer, obscuring prompts and other information.
In the case of long messages, it might resize the minibuffer, which causes EXWM
to perform additional actions due to the log output.

This change reimplements EXWM debug logging using a separate
buffer (*EXWM-DEBUG*).  Basic functionality, like scrolling when point is at the
end of the buffer is maintained.

* exwm-core.el (exwm--log): Use `exwm-debug--message' instead of
`message'.  Prefix all messages with the name of the function.
Make FORMAT-STRING argument optional.

* exwm-debug.el: New file.
(exwm-debug-buffer): New variable holding the buffer where debug
messages are output to.
(exwm-debug--message): New function printing a message to
`exwm-debug-buffer'.
(exwm-debug--backtrace): New function printing a backtrace.
Diffstat (limited to 'exwm-debug.el')
-rw-r--r--exwm-debug.el104
1 files changed, 104 insertions, 0 deletions
diff --git a/exwm-debug.el b/exwm-debug.el
new file mode 100644
index 000000000000..89421da651c5
--- /dev/null
+++ b/exwm-debug.el
@@ -0,0 +1,104 @@
+;;; exwm-debug.el --- Debugging helpers for EXWM  -*- lexical-binding: t -*-
+
+;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
+
+;; Author: Chris Feng <chris.w.feng@gmail.com>
+;;	Adrián Medraño Calvo <adrian@medranocalvo.com>
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs 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.
+
+;; GNU Emacs 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 GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; This module collects functions that help in debugging EXWM.
+
+;;; Code:
+
+(eval-and-compile
+  (defvar exwm-debug-on nil "Non-nil to turn on debug for EXWM."))
+
+(defvar exwm-debug-buffer
+  (when exwm-debug-on
+    (let ((buffer (get-buffer-create "*EXWM-DEBUG*")))
+      (buffer-disable-undo buffer)
+      buffer))
+  "Buffer to write debug messages to.")
+
+(defun exwm-debug--call-stack ()
+  "Return the current call stack frames."
+  (let (frames frame
+        ;; No need to acount for our setq, while, let, ...
+        (index 5))
+    (while (setq frame (backtrace-frame index))
+      (push frame frames)
+      (cl-incf index))
+    (cl-remove-if-not 'car frames)))
+
+(defmacro exwm-debug--compile-time-function-name ()
+  "Get the name of outermost definition at expansion time."
+  (let* ((frame (cl-find-if
+		 (lambda (frame)
+		   (ignore-errors
+		     (let ((clause (car (cl-third frame))))
+		       (or (equal clause 'defalias)
+			   (equal clause 'cl-defmethod)))))
+		 (reverse (exwm-debug--call-stack))))
+	 (defn (cl-third frame))
+	 (deftype (car defn)))
+    (cl-case deftype
+      ((defalias) (symbol-name (cl-cadadr defn)))
+      ((cl-defmethod) (symbol-name (cadr defn)))
+      (t "<unknown function>"))))
+
+(defmacro exwm-debug--with-debug-buffer (&rest forms)
+  "Evaluate FORMS making sure `exwm-debug-buffer' is correctly updated."
+  `(with-current-buffer exwm-debug-buffer
+     (let (windows-eob)
+       ;; Note windows whose point is at EOB.
+       (dolist (w (get-buffer-window-list exwm-debug-buffer t t))
+         (when (= (window-point w) (point-max))
+           (push w windows-eob)))
+       (save-excursion
+         (goto-char (point-max))
+         ,@forms)
+       ;; Restore point.
+       (dolist (w windows-eob)
+         (set-window-point w (point-max))))))
+
+(defun exwm-debug--message (format-string &rest objects)
+  "Print a message to `exwm-debug-buffer'.
+
+The FORMAT-STRING argument follows the speficies how to print each of
+the passed OBJECTS.  See `format' for details."
+  (exwm-debug--with-debug-buffer
+   (insert (apply #'format format-string objects))))
+
+(defmacro exwm-debug--backtrace ()
+  "Print a backtrace to the `exwm-debug-buffer'."
+  '(exwm-debug--with-debug-buffer
+    (let ((standard-output exwm-debug-buffer))
+      (backtrace))))
+
+(defmacro exwm-debug--backtrace-on-error (&rest forms)
+  "Evaluate FORMS.  Printing a backtrace if an error is signaled."
+  `(let ((debug-on-error t)
+         (debugger (lambda (&rest _) (exwm-debug--backtrace))))
+     ,@forms))
+
+
+
+(provide 'exwm-debug)
+
+;;; exwm-debug.el ends here