10

I have a convinience defun in my init.el to do some logging

(defvar log4me::loglevel 5
  "Global loglevel.")

(defun log4me (level logmsg)
  "Log message."
  (interactive)
  (when (>= level log4elisp::loglevel)
    (message logmsg))))

It kind of works but somehow i frequently do

  (log4me somelevel (format "mymessage with %d" 1))

So i found the emacs lisp &rest parameters which i thought i might use like this:

(defun log4me (level logmsg &rest formatparams)
  "Log message."
  (interactive)
  (when (<= level log4elisp::loglevel)
    (message (format logmsg formatparams))))

(log4me 3 "Hello %ust!" 1)

which resuslts into "Format specifier doesn't match argument type" error since formatparams is actually (1) and not 1.

Is there a nice way to include format parameters into the log4elisp defun and make them arrive in the format function call as "normal" parameters (not a single list)?

2
  • 3
    see the function APPLY Commented Nov 1, 2014 at 18:20
  • @Rainer Joswig Thanks! I had the feeling that all i needed was a pointer to the right point in the documentation. apply worked well. Do you like to provide a short answer so that i can accept it? Commented Nov 1, 2014 at 18:25

2 Answers 2

10

What you need is apply:

(defun log4me (level logmsg &rest formatparams)
  "Log message."
  (interactive)
  (when (<= level log4elisp::loglevel)
    (apply #'message logmsg formatparams)))
Sign up to request clarification or add additional context in comments.

Comments

0

apply is perfect for functions, but is invalid for function-objects like macros. The following solution is applicable for both functions and macros.

(defun log4me (level logmsg &rest formatparams)
  "Log message."
  (interactive)
  (when (<= level log4elisp::loglevel)
    (eval (append '(message logmsg) formatparams))))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.