1. I've been learning Clojure, and sometimes I need to know what my functions are doing. To do this, I usually use some format:
file.clj:<lineno> <message>
Where the message could be notifying me some line is being evaluated, or printing the value of some variable.
1.1. I prefer writing <file-name>:<lineno>, in a nod to grep -n output.
2. I know, this is bad form, inelegant, whatever. But it is useful...and that's why I do it!
2.1. To pre-emptively answer criticisms, I know there are precisely one million and one different logging, debugging, stack-tracing libraries. They are all quite heavy weight and complicated.
For these reasons (too complicated and too heavy-weight), I prefer writing my own.
3. What code can do this? Well, the code I typically use is a couple of simple macros:
(defmacro dbg [x]
`(do (printf "%s:%s> %s\n"
~*source-path*
~(:line (meta &form))
~x)
(flush)))
(defmacro dbg-eval [x]
`(do (printf "%s:%s> %s \n;=> %s\n"
~*source-path*
~(:line (meta &form))
~(pr-str x)
~x)
(flush)))
3.1. So writing (dbg "foo() is starting"), I can tell when the function foo is being called.
Stylistic Problems
4. This is based on the Java stacktrace/logging format class.methodName(file.java:lineno) <message>, and namely takes advantage using method calling syntax: method().
4.1. It'd be nice if there were a LISP-y way to do likewise, but for now...it works.
Perhaps write (filename.clj:lineno/function-name <message>)? Or some appropriate variation.
No comments:
Post a Comment