Logo Search packages:      
Sourcecode: ubuntuone-dev-tools version File versions  Download package

handlers.py

# -*- coding: utf-8 -*-

# Author: Guillermo Gonzalez <guillermo.gonzalez@canonical.com>
# Author: Facundo Batista <facundo@canonical.com>
#
# Copyright 2009-2010 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Set of helpers handlers."""

import logging


00025 class MementoHandler(logging.Handler):
    """ A handler class which store logging records in a list """

00028     def __init__(self, *args, **kwargs):
        """ Create the instance, and add a records attribute. """
        logging.Handler.__init__(self, *args, **kwargs)
        self.records = []
        self.debug = False

00034     def emit(self, record):
        """ Just add the record to self.records. """
        self.format(record)
        self.records.append(record)

00039     def check(self, level, *msgs):
        """Verifies that the msgs are logged in the specified level."""
        for rec in self.records:
            if rec.levelno == level and all(m in rec.message for m in msgs):
                return True
        if self.debug:
            recorded = [(logging.getLevelName(r.levelno), r.message)
                        for r in self.records]
            print "Memento messages:", recorded
        return False

00050     def check_debug(self, *msgs):
        """Shortcut for checking in DEBUG."""
        return self.check(logging.DEBUG, *msgs)

00054     def check_info(self, *msgs):
        """Shortcut for checking in INFO."""
        return self.check(logging.INFO, *msgs)

00058     def check_warning(self, *msgs):
        """Shortcut for checking in WARNING."""
        return self.check(logging.WARNING, *msgs)

00062     def check_error(self, *msgs):
        """Shortcut for checking in ERROR."""
        return self.check(logging.ERROR, *msgs)

00066     def check_exception(self, exception_class, *msgs):
        """Shortcut for checking exceptions."""
        for rec in self.records:
            if rec.levelno == logging.ERROR and \
               all(m in rec.exc_text for m in msgs) and \
               exception_class == rec.exc_info[0]:
                return True
        return False

Generated by  Doxygen 1.6.0   Back to index