import functools
import time


def stopwatch(fn):
    """
    Log (INFO) how much time is spent in decorated fn.
    """

    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        start = time.perf_counter()
        result = fn(*args, **kwargs)
        end = time.perf_counter()
        msec = (end - start) * 1000
        # log results
        logger = fn.__globals__.get("logger", None)
        if logger:
            logger.info(
                "{elapsed:4d} msec elapsed in '{name}'".format(
                    elapsed=int(round(msec, 1)), name=fn.__qualname__
                )
            )
        return result

    return wrapper