lib/log.py
author terom
Thu, 31 Jan 2008 19:13:00 +0000
changeset 28 70b6c13d084f
parent 27 301d738b1181
child 29 990300aa8010
permissions -rw-r--r--
fancy new log format
# DeGAL - A pretty simple web image gallery
# Copyright (C) 2007 Tero Marttila
# http://marttila.de/~terom/degal/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

import logging, sys

log_level = logging.INFO
stack = []

class g :
    out_depth = 0
    node = None

def title (title, *args) :
    stack.append(title)

    print "%s- %s" % (" "*g.out_depth, title % args)

    g.out_depth += 1

def down (dir_name, *args) :
    stack.append(dir_name % args)
    g.node = None

def next (fname, *args) :
    g.node = fname % args

def up () :
    stack.pop(-1)
    g.node = None
    g.out_depth = min(g.out_depth, len(stack))

def done () :
    print "done"

def log (level, message, *args, **kwargs) :
    wait = kwargs.get("wait", False)

    if level >= log_level :
        if g.out_depth != len(stack) :
            for segment in stack[g.out_depth:] :
                print "%sd %s" % (" "*g.out_depth, segment)
                g.out_depth += 1

            if g.node :
                print "%sf %s" % (" "*g.out_depth, g.node)
                g.node = None
        
        if wait :
            print "%s- %s..." % (" "*g.out_depth, message % args),
            sys.stdout.flush()
        else :
            print "%s- %s" % (" "*g.out_depth, message % args)

def _level (level) :
    def _log_func (message, *args, **kwargs) :
        log(level, message, *args, **kwargs)
    
    return _log_func

debug       = _level(logging.DEBUG)
info        = _level(logging.INFO)
warning     = _level(logging.WARNING)
error       = _level(logging.ERROR)