Simple Cases

Adjustable Trace Level via Environment Variable

By default, icecream-truck does not produce any output. You, as an application developer, will need to determine how much output you want from it and whether you will let your users adjust that knob. Having a default trace depth, which can be overridden by environment variable is one simple way to achieve this.

#!/usr/bin/env python

# ruff: noqa: F821


import functools
import operator

import ictruck


def main( ):
    # Add 'ictr' to Python builtins.
    ictruck.install( )

    # Indicate that tracing has been enabled.
    # If we had simply done 'ictruck.install( )', it would be inactive,
    # since the default trace depth is -1.
    ictr( 0 )( "Icecream tracing active." )

    some_work( )


def some_work( ):
    answer = deeper_work( ( 2, 3, 7 ), operator.mul )
    # Can use a deeper trace level to indicate depth in call stack.
    ictr( 1 )( answer )


def deeper_work( data, operator ):
    ictr( 2 )( operator )
    for datum in data:
        # Can use a deeper trace level to indicate loop bodies.
        ictr( 3 )( datum )
    return functools.reduce( operator, data )


if '__main__' == __name__: main( )

Running this will result in:

TRACE0| 'Icecream tracing active.'
TRACE2| operator: <built-in function mul>
TRACE1| answer: 42

Running this with ICTRUCK_TRACE_LEVELS=3 in the environment will result in:

TRACE0| 'Icecream tracing active.'
TRACE2| operator: <built-in function mul>
TRACE3| datum: 2
TRACE3| datum: 3
TRACE3| datum: 7
TRACE1| answer: 42