I build a package with code to solve specific equations. The code has become quite complex and so to convince myself (and others) that it works correctly I would like to add some code to write messages to a log file. The messages would depend on the specific function for which I want information and I want to be able to turn logging on or off for certain functions (not all functions need to write to a log file though, only the 'big' ones).
So basically how could one alter the code in a package such that:
- the user of the package has the option to turn on logging for certain functions, and
- the code itself needs as little altering as possible. In particular I want to be able to add functions and test them without having to rewrite other functions their logging behaviour.
Some of the solutions I thought about are the following
OptionsPatternin every function to start logging. Problem is that I have to alter the code of each function and put
Ifstatements everywhere which messes up readability.
Implement a function
PrintLog[file_], that takes a function and tells it to evaluate and write info to log file. Then there would be some construction in the functions themselves that get turned on by the
PrintLogfunction. At the moment I wouldn't know how to implement this solution though...