"""$URL: svn://svn/repos/trunk/sitereaper/lib/verbose.py $
$Id: verbose.py 20952 2003-03-05 16:51:11Z dbinger $

The provided Verbose class can be used as a mixin to provide
a uniform method for sending extra output to stderr.

"""

import sys
from time import time, localtime, strftime

class Verbose:
    """
    Instance attributes:
      __verbose : int
        Higher values produce more output.
      __file : file
        file to write log messages to

    Use this class as a mixin.  Use set_verbose() to establish a verbose
    level, and then call note() to write output at different verbose
    levels.
    """

    def __init__ (self, verbose=1, file=None):
        self.__verbose = verbose
        self.__file = file or sys.stdout

    def set_file (self, file):
        self.__file = file

    def get_verbose (self):
        return self.__verbose

    def set_verbose (self,val):
        self.__verbose=val

    def note(self, level, *args):
        """note(level : int, args : [ any ] )

        When the verbose level is greater than or equal to level,
        write all args to stdout

        this prints a message including level and
        all args to stderr."""
        write = self.__file.write
        flush = self.__file.flush

        if self.__verbose >= level:
            now = time()
            timestamp = (strftime("%H:%M:%S", localtime(now)) +
                         ("%.3f" % (now % 1))[1:])
            write("[%s] " % timestamp)
            for a in args:
                write(str(a))
            write('\n')
            flush()
