The short answer to this is that a simple Print[]
is not a good way to debug a large package. It is not possible to turn it off (in a robust way) only for your package, but not in other places (it can be disabled globally, the simplest way being Print; Unprotect[Print]; Print = Null &
, but this will severely annoy your users)
For much better ways to emit debug messages, and turn them off, see here:
https://stackoverflow.com/questions/8698754/message-generation-in-mathematica
I'll summarize Brett Champion and my own answers from there, as personally (and subjectively) I consider those the best solutions, but please read the full thread there.
Brett Champion's answer:
Use a custom symbol for debug messages(just like your package functions, you may want to put it in a separate context to avoid collisions), like this:
... debugPrint[expr] ...
Then define a PrintDebug
function like this:
Attributes[PrintDebug]= PrintDebug[expr_] := Block[, expr]
Any code wrapped in PrintDebug
will print messages. If there's no PrintDebug
wrapper, message printing is disabled.
MyFunction[1,2,3] // PrintDebug
Alternately you may set a value to debugPrint
globally to enable message printing for everything or just do $Pre = PrintDebug
.
My answer:
We can use the built-in Message
functionality for debugging too:
debugPrint::msg = "Debug message: `1`"; debugPrint[msg_] := Message[debugPrint::msg, msg]
Use the function like this:
debugPrint["hello"]
Turn off or on messages like this:
Off[debugPrint::msg] On[debugPrint::msg]