#
# Kiwi: a Framework and Enhanced Widgets for Python
#
# Copyright (C) 2005-2006 Async Open Source
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
# USA
#
# Author(s): Adriano Monteiro <adriano@globalret.com.br>
#            Johan Dahlin     <jdahlin@async.com.br>
##

import logging
import os
import sys

class LogError(Exception):
    pass

class Formatter(logging.Formatter):
    def format(self, record):
        frame = sys._getframe(9)
        filename = os.path.basename(frame.f_code.co_filename)
        record.msg = '%s:%d %s' % (filename, frame.f_lineno, record.msg)
        return logging.Formatter.format(self, record)

class Logger(logging.Logger):
    log_domain = 'default'
    def __init__(self, name=None, level=logging.NOTSET):
        """Initializes Log module, creating log handler and defining log
        level. level attribute is not mandatory. It defines from which level
        messages should be logged. Logs with lower level are ignored.

        logging default levels table:

        Level
          - logging.NOTSET
          - logging.DEBUG
          - logging.INFO
          - logging.WARNING
          - logging.ERROR
          - logging.CRITICAL
        """
        if not name:
            name = Logger.log_name
        logging.Logger.__init__(self, name, get_log_level(name))

        stream_handler = logging.StreamHandler(sys.stdout)

        # Formater class define a format for the log messages been
        # logged with this handler
        # The following format string
        #   ("%(asctime)s (%(levelname)s) - %(message)s") will result
        # in a log message like this:
        #   2005-09-07 18:15:12,636 (WARNING) - (message!)
        format_string = ("%(asctime)s %(message)s")
        stream_handler.setFormatter(Formatter(format_string,
                                              datefmt='%T'))
        self.addHandler(stream_handler)

    def __call__(self, message, *args, **kwargs):
        self.info(message, *args, **kwargs)

_log_levels = {}

def set_log_level(name, level):
    """
    @param name: logging category
    @param level: level
    """
    global _log_levels
    _log_levels[name] = level

def get_log_level(name):
    """
    @param name: logging category
    @returns: the level
    """
    global _log_levels
    return _log_levels.get(name, logging.WARNING)

def _read_log_level():
    log_levels = {}
    # bootstrap issue, cannot depend on environ
    log_level = os.environ.get('KIWI_LOG')
    if not log_level:
        return log_levels

    for part in log_level.split(','):
        if ':' in part:
            if part.count(':') > 1:
                raise LogError("too many : in part %s" % part)
            name, level = part.split(':')
            try:
                level = int(level)
            except ValueError:
                raise LogError("invalid level: %s" % level)

            if level < 0 or level > 5:
                raise LogError("level must be between 0 and 5")
        log_levels[name] = level

    return log_levels

_log_levels = _read_log_level()

kiwi_log = Logger('kiwi')

@@ -932,18 +932,32 @@
 om.br%3E%0A#
-#
 %0A
+%0Aimport fnmatch
 %0Aimport 
@@ -2688,16 +2688,49 @@
 els = %7B%7D
+%0A_default_level = logging.WARNING
 %0A%0Adef se
@@ -3008,82 +3008,299 @@
 vels
-%0A    return _log_levels.get(name, logging.WARNING)%0A%0Adef _read_log_level():
+, _default_level%0A%0A    for category in _log_levels:%0A        if fnmatch.fnmatch(name, category):%0A            level = _log_levels%5Bcategory%5D%0A            break%0A    else:%0A        level = _default_level%0A    print name, '-%3E', level%0A    return level%0A%0Adef _read_log_level():%0A    global _default_level%0A
 %0A   
@@ -3505,16 +3505,20 @@
      if 
+not 
 ':' in p
@@ -3522,24 +3522,42 @@
 n part:%0A    
+        continue%0A%0A
         if p
@@ -3576,20 +3576,16 @@
 ') %3E 1:%0A
-    
         
@@ -3639,28 +3639,24 @@
 rt)%0A        
-  
-  
 name, level 
@@ -3681,20 +3681,16 @@
         
-    
 try:%0A   
@@ -3690,36 +3690,32 @@
 ry:%0A            
- 
-   
 level = int(leve
@@ -3725,20 +3725,16 @@
         
-   
- 
 except V
@@ -3744,20 +3744,16 @@
 eError:%0A
-    
         
@@ -3809,20 +3809,16 @@
         
-    
 if level
@@ -3836,20 +3836,16 @@
 el %3E 5:%0A
-    
         
@@ -3895,16 +3895,132 @@
  and 5%22)
+%0A%0A        level = 50 - (level * 10)%0A%0A        if name == '*':%0A            _default_level = level%0A            continue
 %0A       

Refactor and add proper support for wildcards