#!/usr/bin/env python
"""Illustrates pyexperiment's logging features with a minimal example.

Running `minimal_logging run` will log to the console at the INFO level,
`minimal_logging run --verbosity DEBUG` will log to the console at the
DEBUG level, `minimal_logging run -o log_to_file 1 -o log_filename
log.txt -o log_file_verbosity DEBUG` will log at the INFO level in the
console and at the DEBUG level in the file log.txt (which will rotate up
to 5 backup copies).

Running `minimal_logging timed` to get timing statistics of a for loop
(change the log level to DEBUG to get more details).

Written by Peter Duerr
"""

# Sensible imports from the future (optional)
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import

# For python2.x compatibility
from six.moves import range  # pylint: disable=redefined-builtin, import-error

# Import the experiment module for its main function
from pyexperiment import experiment

# Import pyexperiment's logging module
from pyexperiment import log


def run():
    """Logs some messages. Set the level, e.g, to DEBUG with --verbosity DEBUG
    """
    log.debug("Debug Message...")
    log.info("Info Message")
    log.warning("Warning Message")
    log.error("Error Message")
    log.fatal("Fatal Message")


def timed():
    """Time the execution of a for loop
    """
    # set up a context for timed execution of the setup code
    with log.timed("setup"):
        replicates = 25
        no_loops = 10000

    # Run the experiment a couple of times to get statistics
    for _ in range(replicates):
        # Set up a context for timed execution of the for loop
        with log.timed("for"):
            # Run some code
            for _ in range(no_loops):
                # Need something more useful here
                pass

    # Print timing statistics
    log.print_timings()

if __name__ == '__main__':
    # Run pyexperiment's main function, passing the 'run' and 'timed' functions
    experiment.main(commands=[run, timed])
