lib/log.py
branchuse-distutils
changeset 41 3b1579a7bffb
parent 40 373392025533
child 42 146997912efb
equal deleted inserted replaced
40:373392025533 41:3b1579a7bffb
     1 # DeGAL - A pretty simple web image gallery
       
     2 # Copyright (C) 2007 Tero Marttila
       
     3 # http://marttila.de/~terom/degal/
       
     4 #
       
     5 # This program is free software; you can redistribute it and/or modify
       
     6 # it under the terms of the GNU General Public License as published by
       
     7 # the Free Software Foundation; either version 2 of the License, or
       
     8 # (at your option) any later version.
       
     9 #
       
    10 # This program is distributed in the hope that it will be useful,
       
    11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 # GNU General Public License for more details.
       
    14 #
       
    15 # You should have received a copy of the GNU General Public License
       
    16 # along with this program; if not, write to the
       
    17 # Free Software Foundation, Inc.,
       
    18 # 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
       
    19 #
       
    20 
       
    21 import logging, sys
       
    22 
       
    23 log_level = logging.INFO
       
    24 stack = []
       
    25 
       
    26 class g :
       
    27     out_depth = 0
       
    28     node = None
       
    29 
       
    30 def title (title, *args) :
       
    31     stack.append(title)
       
    32 
       
    33     print "%s - %s" % (" "*g.out_depth, title % args)
       
    34 
       
    35     g.out_depth += 1
       
    36 
       
    37 def down (dir_name, *args) :
       
    38     stack.append(dir_name % args)
       
    39     g.node = None
       
    40 
       
    41 def next (fname, *args) :
       
    42     g.node = fname % args
       
    43 
       
    44 def up () :
       
    45     stack.pop(-1)
       
    46     g.node = None
       
    47     g.out_depth = min(g.out_depth, len(stack))
       
    48 
       
    49 def done () :
       
    50     print "done"
       
    51 
       
    52 def log (level, message, *args, **kwargs) :
       
    53     wait = kwargs.get("wait", False)
       
    54 
       
    55     if level >= log_level :
       
    56         if g.out_depth != len(stack) :
       
    57             for segment in stack[g.out_depth:] :
       
    58                 print "%sd %s" % (" "*g.out_depth, segment)
       
    59                 g.out_depth += 1
       
    60 
       
    61         if g.node :
       
    62             print "%sf %s" % (" "*g.out_depth, g.node)
       
    63             g.node = None
       
    64         
       
    65         if wait :
       
    66             print "%s - %s..." % (" "*g.out_depth, message % args),
       
    67             sys.stdout.flush()
       
    68         else :
       
    69             print "%s - %s" % (" "*g.out_depth, message % args)
       
    70 
       
    71 def _level (level) :
       
    72     def _log_func (message, *args, **kwargs) :
       
    73         log(level, message, *args, **kwargs)
       
    74     
       
    75     return _log_func
       
    76 
       
    77 debug       = _level(logging.DEBUG)
       
    78 info        = _level(logging.INFO)
       
    79 warning     = _level(logging.WARNING)
       
    80 error       = _level(logging.ERROR)
       
    81