diff options
author | Ian Hinder <ian.hinder@aei.mpg.de> | 2012-02-01 01:00:57 -0600 |
---|---|---|
committer | Ian Hinder <ian.hinder@aei.mpg.de> | 2012-02-01 01:01:13 -0600 |
commit | c2d8c677cb8be7c549391d8dca41a3e2ef830921 (patch) | |
tree | 33af3379e29ae3eb87c43cfad852327659bff6e5 /Tools/MathematicaMisc/Errors.m | |
parent | 33b1997e55f8e1216b4476d9761cb482cbc69b91 (diff) |
Errors.m: Add function execution tracing option
Enclose code with
Block[{TraceExecution = True},
code]
to see a trace of function calls with arguments and returned results.
Diffstat (limited to 'Tools/MathematicaMisc/Errors.m')
-rw-r--r-- | Tools/MathematicaMisc/Errors.m | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/Tools/MathematicaMisc/Errors.m b/Tools/MathematicaMisc/Errors.m index f75bb3b..53cfcd7 100644 --- a/Tools/MathematicaMisc/Errors.m +++ b/Tools/MathematicaMisc/Errors.m @@ -11,6 +11,7 @@ InfoMessage; SetDebugLevel; ErrorDefinition::usage = "ErrorDefinition[f] creates a default definition of a function f which throws an exception. This can be used to catch programming errors where f is called with incorrect arguments."; PrintError; +TraceExecution; DebugQuiet = 0; Warnings = 1 @@ -97,11 +98,23 @@ ErrorDefinition[x_] := SetAttributes[DefFn, HoldAll]; +TraceExecution = False; +TraceExecutionIndent = ""; + DefFn[def:(fn_[args___] := body_)] := Module[ - {}, + {result = Unique["result"]}, ErrorDefinition[fn]; - fn[args] := (*Profile[fn,*)body(*]*)]; + Quiet[ + fn[args] := + Module[ + {result}, + If[TraceExecution, Print[TraceExecutionIndent, fn," ", Map[InputForm[First[#]] &,{args}]]]; + TraceExecutionIndent="| "<>TraceExecutionIndent; + result2 = body; + TraceExecutionIndent=StringDrop[TraceExecutionIndent,2]; + If[TraceExecution, Print[TraceExecutionIndent, "-> ", InputForm@result2]]; + result2], RuleDelayed::rhs]]; End[]; |