Print debugging statement: Difference between revisions

m (→‎{{header|Phix}}: added syntax colouring the hard way)
Line 456:
9
</pre>
 
=={{header|Nim}}==
In Nim, there are many ways to implement print debugging statements.
 
The first one consists to insert conditional printing statements. Printing statements may write on <code>stdout</code>, <code>stderror</code> or any other file. In their simplest form, they can be an <code>echo</code> statement or a <code>debugEcho</code> statement, the latter pretending to be side effects free.
 
The conditional activation is done using a “when” statement. Here is an example:
<lang Nim>when defined(debug):
echo "Debugging info: ", x, " ", y, " ", z</lang>
When compiling, if nothing is specified, no code is generated to display the debugging information. If we want to generate the debug statements, we have to specify <code>-d:debug</code> when compiling, for instance if the file is named “example.nim”: <code>nim c -d:debug example.nim</code>. Note that the name “debug” has been chosen as an example but maybe any valid Nim identifier not already used. This allows to use different flags according to what we want to debug.
 
If is also possible to use existing flags instead of defining new ones:
<lang Nim>when not defined(release):
echo "Debugging info: ", x, " ", y, " ", z</lang>
In this case, by default (debug build), the code for the statement is generated. But if we produce a release build (<code>-d:release</code> option), the debugging code will not be generated.
 
Note that it is possible embed a conditional print statement in a template or even to use macros to create more complicated forms of debugging statements.
 
A second way to insert debugging print statements consists to use “assert” statements. This is not really debugging statements, but rather sanity checks. Nim provides an “assert” statement which is deactivated if checks are off and a “doAssert” statement which cannot be deactivated. Even if the assert statement is simple, it allows to display a message which useful debugging information in case of failure.
 
Last, but not least, Nim provides a simple and efficient log module. Using logger objects, this module allows to write log data to the console or into a file. As expected of a logging module, it provides several levels for debugging:
 
<lang Nim>Level = enum
lvlAll, ## All levels active
lvlDebug, ## Debug level and above are active
lvlInfo, ## Info level and above are active
lvlNotice, ## Notice level and above are active
lvlWarn, ## Warn level and above are active
lvlError, ## Error level and above are active
lvlFatal, ## Fatal level and above are active
lvlNone ## No levels active; nothing is logged</lang>
 
These levels are mapped to the following names: "DEBUG", "DEBUG", "INFO", "NOTICE", "WARN","ERROR", "FATAL", "NONE".
 
Changing the logging level for a logger or globally maybe done at runtime if the application has implemented some mechanism for that, or at compile time, for instance by using some flag with a value: <code> -d:logLevel=DEBUG</code>.
 
Logging debugging data is then a simple as writing:
<lang Nim>debug "Debugging info: $1 $2 $3".format(x, y, z)</lang>
 
=={{header|Perl}}==
Anonymous user