| 1 | = Trace = |
| 2 | |
| 3 | Display an execution trace for |
| 4 | * a point of execution or |
| 5 | * an expression or expressionlist or |
| 6 | * all args and locals in the current method |
| 7 | or enable or disable subsequent trace output. |
| 8 | |
| 9 | Tracing an expression or expressionlist is unlike a print of the same expression in that |
| 10 | * The display format is different; |
| 11 | trace generates expression-text = expression-value for each expression |
| 12 | * Position information (filename, line number, declaring class and method) of the trace line is printed |
| 13 | * Traces can be suppressed on a build by a cobra commandline argument |
| 14 | {{{ cobra -include-traces:no ...}}} |
| 15 | |
| 16 | traces are useful for debugging (displaying interim expression values) and |
| 17 | logging a point of execution( e.g. indicating that code in a code block is being executed). |
| 18 | |
| 19 | |
| 20 | A simple '''trace'''' statement with no arguments gives just the position info |
| 21 | of the trace statement - the filename, line number, declaring class name |
| 22 | and declaring method name that the trace is located in. [[BR]] |
| 23 | If the current object's class is a subclass of the declaring class then the |
| 24 | subclass name is reported as well. |
| 25 | |
| 26 | When given one or more expressions, trace gives the same information |
| 27 | plus the source code and value of each expression. |
| 28 | |
| 29 | The '''trace all''' statement is a convenience for logging |
| 30 | * this, |
| 31 | * every method argument and |
| 32 | * every local variable. |
| 33 | |
| 34 | The '''trace off''' statement turns off the subsequent trace statements in the declaring method. |
| 35 | |
| 36 | The '''trace on''' statement turns them back on. |
| 37 | |
| 38 | == Grammar == |
| 39 | {{{ |
| 40 | trace |
| 41 | trace <expr1>, <expr2>, ... <expr3> |
| 42 | trace all |
| 43 | trace off |
| 44 | trace on |
| 45 | |
| 46 | }}} |
| 47 | |
| 48 | |
| 49 | == Examples == |
| 50 | |
| 51 | {{{ |
| 52 | class Foo |
| 53 | |
| 54 | var _z as int |
| 55 | |
| 56 | def computeStuff(x as int, y as int) |
| 57 | if x > y |
| 58 | trace |
| 59 | return |
| 60 | _z = x * y |
| 61 | trace all |
| 62 | trace _z |
| 63 | |
| 64 | trace: at Foo.cobra:7; in Foo.computeStuff |
| 65 | trace: this=Foo; x=4; y=2; at Foo.cobra:10; in Foo.computeStuff |
| 66 | trace: this=Foo; _z=8; at Foo.cobra:11; in Foo.computeStuff |
| 67 | }}} |
| 68 | |
| 69 | == Notes == |
| 70 | |
| 71 | Needs a way to display just the expression (suppress position info). |
| 72 | |
| 73 | See also [http://cobra-language.com/docs/manual/basic-statements/trace.html manual: trace statement documentation] |