What is the right way to perform hierarchical logging in Emacs Lisp?

As a representative example…

 "baking cake"
    "making batter"
      (scoped-message "whisking eggs" (whisk-eggs))

I would like to see something like baking cake → making batter → whisking eggs in the Messages butter and in the mode line.

Is there a library, macro, or standard idiom that makes this easy?

The library straight.el does this when building dependent libraries (e.g. building org-drill → org-contrib), but it's built in a very custom way. Is there a standard solution available?

  • I do not see a big need for such a library since the effect is easily obtained by let-binding: (let ((scoped-message "baking cake")) (let ((scoped-message (concat scoped-message " → making batter"))) (message (concat scoped-message " → whisking eggs")))) Pityingly the line ends are lost in the comment and therefore the structure information associated with the line ends is lost. Nevertheless it is more a comment instead of an answer... – Tobias Dec 22 '18 at 23:14

Noting that this is more or less along the lines of how run-mode-hooks and delay-mode-hooks interact, you could do something similar to that:

(defvar scoped-message-p nil)
(defvar scoped-messages nil)
(defmacro scoped-message (message &rest body)
  (declare (indent 1))
     (if scoped-message-p
         (push ,message scoped-messages)
       (setq scoped-messages (list ,message)))
     (let ((scoped-message-p t))
     (unless scoped-message-p
       (message "%s" (mapconcat #'identity (nreverse scoped-messages)
                                " → ")))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.