~maddevelopers/mg5amcnlo/WWW5_caching

« back to all changes in this revision

Viewing changes to users/mardelcourt/PROC_970226/PROC_970226/bin/internal/coloring_logging.py

  • Committer: John Doe
  • Date: 2013-03-25 20:27:02 UTC
  • Revision ID: john.doe@gmail.com-20130325202702-5sk3t1r8h33ca4p4
first clean version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import logging
 
2
 
 
3
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
 
4
 
 
5
COLORS = {
 
6
    'WARNING'  : BLUE,
 
7
    'INFO'     : BLACK,
 
8
    'DEBUG'    : GREEN,
 
9
    'CRITICAL' : RED,
 
10
    'ERROR'    : RED,
 
11
    'BLACK'    : BLACK,
 
12
    'RED'      : RED,
 
13
    'GREEN'    : GREEN,
 
14
    'YELLOW'   : YELLOW,
 
15
    'BLUE'     : BLUE,
 
16
    'MAGENTA'  : MAGENTA,
 
17
    'CYAN'     : CYAN,
 
18
    'WHITE'    : WHITE,
 
19
}
 
20
 
 
21
RESET_SEQ = "\033[0m"
 
22
COLOR_SEQ = "\033[1;%dm"
 
23
BOLD_SEQ  = "\033[1m"
 
24
 
 
25
class ColorFormatter(logging.Formatter):
 
26
 
 
27
    def __init__(self, *args, **kwargs):
 
28
        # can't do super(...) here because Formatter is an old school class
 
29
        logging.Formatter.__init__(self, *args, **kwargs)
 
30
 
 
31
    def format(self, record):
 
32
        levelname = record.levelname
 
33
        color_choice = COLORS[levelname]
 
34
        new_args=[]
 
35
        # A not-so-nice but working way of passing arguments to this formatter
 
36
        # from MadGraph.
 
37
        color_specified = False
 
38
        for arg in record.args:
 
39
            if isinstance(arg,str) and arg.startswith('$MG'):
 
40
                elems=arg.split(':')
 
41
                if len(elems)>2:
 
42
                    if elems[1]=='color':
 
43
                        color_specified = True                            
 
44
                        color_choice = COLORS[elems[2]]
 
45
            else:
 
46
                new_args.append(arg)
 
47
        record.args = tuple(new_args)
 
48
        color     = COLOR_SEQ % (30 + color_choice)
 
49
        message   = logging.Formatter.format(self, record) + RESET_SEQ
 
50
        for k,v in COLORS.items():
 
51
            message = message.replace("$" + k,    COLOR_SEQ % (v+30))\
 
52
                         .replace("$BG" + k,  COLOR_SEQ % (v+40))\
 
53
                         .replace("$BG-" + k, COLOR_SEQ % (v+40))        
 
54
        
 
55
        
 
56
        if levelname == 'INFO':
 
57
            message   = message.replace("$RESET", '')\
 
58
                           .replace("$BOLD",  '')\
 
59
                           .replace("$COLOR", color if color_specified else '')
 
60
            return message
 
61
        else:    
 
62
            message   = message.replace("$RESET", RESET_SEQ)\
 
63
                           .replace("$BOLD",  BOLD_SEQ)\
 
64
                           .replace("$COLOR", color)
 
65
 
 
66
        return message 
 
67
 
 
68
logging.ColorFormatter = ColorFormatter
 
 
b'\\ No newline at end of file'