@cindex @code{context} in trace functions
Optional @var{context} expressions are also evaluated, both when the
associated function is called and again when it returns, with the
-value logged alongside the call-time arguments or return value
-respectively. This could be used to track the current buffer or
-position of point, for instance. When trace functions are called from
-Lisp rather than interactively, @var{context} should be a function of
-no arguments which returns the value to be inserted into the trace
-output buffer.
+value logged within square brackets alongside the call-time arguments
+or return value respectively. This could be used to track the current
+buffer or position of point, for instance. If @var{context} is a
+function, it will be called (with no arguments) to obtain the value to
+be inserted into the trace output buffer.
Finally, you may add explicit calls to @code{trace-values} to your
code, to log arbitrary values to the trace buffer at any time.
;; FIXME: Make it so we can click the function name to jump to its
;; definition and/or untrace it.
(cons function args)
- context)))
+ (if context (format " [%s]" context) ""))))
(defun trace-exit-message (function level value context)
"Generate a string that describes that FUNCTION has exited.
function
;; Do this so we'll see strings:
value
- context)))
+ (if context (format " [%s]" context) ""))))
(defvar trace--timer nil)
FUNCTION is the name of the traced function.
BUFFER is the buffer where the trace should be printed.
BACKGROUND if nil means to display BUFFER.
-CONTEXT if non-nil should be a function that returns extra info that should
-be printed along with the arguments in the trace."
+CONTEXT, if non-nil, should be either a function or an expression
+that returns extra info, which will be printed after the
+arguments or return value in the trace."
+ (setq context (if context
+ (if (functionp context)
+ context
+ (trace-make-context context))
+ (lambda () "")))
(lambda (body &rest args)
(let ((trace-level (1+ trace-level))
(trace-buffer (get-buffer-create buffer))
"Add trace advice for FUNCTION."
(advice-add
function :around
- (trace-make-advice function (or buffer trace-buffer) background
- (or context (lambda () "")))
+ (trace-make-advice function (or buffer trace-buffer) background context)
`((name . ,trace-advice-name) (depth . -100))))
(defun trace-is-traceable-p (sym)
Return (BUFFER CONTEXT)."
(list
(read-buffer "Output to buffer" trace-buffer)
- (let ((exp
- (read-from-minibuffer "Context expression: "
- nil read-expression-map t
- 'read-expression-history "nil")))
- (and exp
- (lambda ()
- (let ((print-circle t)
- (print-escape-newlines t))
- (concat " [" (prin1-to-string (eval exp t)) "]")))))))
+ (when-let ((exp (read-from-minibuffer
+ "Context expression: "
+ nil read-expression-map t
+ 'read-expression-history "nil")))
+ (trace-make-context exp))))
+
+(defun trace-make-context (exp)
+ "Return a context function for expression EXP."
+ (lambda ()
+ (let ((print-circle t)
+ (print-escape-newlines t))
+ (prin1-to-string (eval exp t)))))
;;;###autoload
(defun trace-function-foreground (function &optional buffer context)