degal/log.py
author Tero Marttila <terom@fixme.fi>
Wed, 03 Jun 2009 19:23:10 +0300
branchuse-distutils
changeset 44 533b7e8b5d3b
parent 41 3b1579a7bffb
permissions -rw-r--r--
strip copyright/license boilerplate from modules, except dexif and formatbytes
44
533b7e8b5d3b strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
     1
import logging, sys
14
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     2
44
533b7e8b5d3b strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
     3
"""
533b7e8b5d3b strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
     4
    Fancy tree-structured logging output
533b7e8b5d3b strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
     5
"""
12
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
     6
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
     7
log_level = logging.INFO
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
     8
stack = []
12
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
     9
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    10
class g :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    11
    out_depth = 0
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    12
    node = None
12
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    13
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    14
def title (title, *args) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    15
    stack.append(title)
15
0fe851a29bc6 detool.py, meant to be a collection of utility operations, currently lets you move images and their html/thumb/preview files at the same time
terom
parents: 14
diff changeset
    16
29
990300aa8010 some small tweaks to log output
terom
parents: 28
diff changeset
    17
    print "%s - %s" % (" "*g.out_depth, title % args)
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    18
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    19
    g.out_depth += 1
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    20
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    21
def down (dir_name, *args) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    22
    stack.append(dir_name % args)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    23
    g.node = None
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    24
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    25
def next (fname, *args) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    26
    g.node = fname % args
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    27
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    28
def up () :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    29
    stack.pop(-1)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    30
    g.node = None
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    31
    g.out_depth = min(g.out_depth, len(stack))
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    32
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    33
def done () :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    34
    print "done"
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    35
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    36
def log (level, message, *args, **kwargs) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    37
    wait = kwargs.get("wait", False)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    38
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    39
    if level >= log_level :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    40
        if g.out_depth != len(stack) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    41
            for segment in stack[g.out_depth:] :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    42
                print "%sd %s" % (" "*g.out_depth, segment)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    43
                g.out_depth += 1
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    44
30
b1d5c32ab771 moar tweaks/small bugfixes
terom
parents: 29
diff changeset
    45
        if g.node :
b1d5c32ab771 moar tweaks/small bugfixes
terom
parents: 29
diff changeset
    46
            print "%sf %s" % (" "*g.out_depth, g.node)
b1d5c32ab771 moar tweaks/small bugfixes
terom
parents: 29
diff changeset
    47
            g.node = None
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    48
        
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    49
        if wait :
29
990300aa8010 some small tweaks to log output
terom
parents: 28
diff changeset
    50
            print "%s - %s..." % (" "*g.out_depth, message % args),
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    51
            sys.stdout.flush()
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    52
        else :
29
990300aa8010 some small tweaks to log output
terom
parents: 28
diff changeset
    53
            print "%s - %s" % (" "*g.out_depth, message % args)
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    54
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    55
def _level (level) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    56
    def _log_func (message, *args, **kwargs) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    57
        log(level, message, *args, **kwargs)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    58
    
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    59
    return _log_func
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    60
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    61
debug       = _level(logging.DEBUG)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    62
info        = _level(logging.INFO)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    63
warning     = _level(logging.WARNING)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    64
error       = _level(logging.ERROR)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    65