Debug on FOO 100Where FOO is the narrative stream's name, and 100 is the level of interest we have in it. By convention, higher numbers are more detailed.Then, scatter narrative through your code thus:
Debug.FOO {this is an arbitrary expression containing $vars [and exprs]} 10If the level of interest of the narrative line is less than the currently assigned level of interest, the narrative is substed and the output sent to the stream's open file descriptor.Because the narrative string is substed in the caller's scope, one can perform arbitrarily complex computations which narrate the state of the computation, but because it's only conditionally substed according to level of interest in each narrative stream, the user can feel free to pepper their code with Debug narrative at fairly low cost when the Debugging's not needed. In my opinion, this freedom makes for a more literate programming style, more readable code, and easier debugging.Latest version may be sourced here [1] and perused here [2]
Lars H, 2008-08-26: Some remarks…
- If the message length is between 4097 and 8193 characters in length, the above "truncating" actually amounts to repeating the central part of it!
- Don't do uplevel 1 ::subst -nobackslashes [list $message], do uplevel 1 [list ::subst -nobackslashes $message] — avoids some shimmering.
- You probably need to clarify what is meant by "an arbitrary expression containing $vars [and exprs]" in the docs; subst was not an association I got from that.
- string map {\n \\n} $result is a rather inconsistent (and IMHO ugly) way of quoting newlines. string range [list $result\{] 0 end-2 is an alternative.
- To be "no-op if disabled" is a feature also of the tcllib logger package, but that doesn't do substitution on the message, so dynamic messages would still be formed.
- Why -nobackslashes in the subst?