Package madgraph :: Package interface :: Module coloring_logging
[hide private]
[frames] | no frames]

Source Code for Module madgraph.interface.coloring_logging

 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 if color_choice == 0: 46 color_choice = 30 47 else: 48 new_args.append(arg) 49 record.args = tuple(new_args) 50 color = COLOR_SEQ % (30 + color_choice) 51 message = logging.Formatter.format(self, record) 52 if not message.endswith('$RESET'): 53 message += '$RESET' 54 for k,v in COLORS.items(): 55 color_flag = COLOR_SEQ % (v+30) 56 message = message.replace("$" + k, color_flag)\ 57 .replace("$BG" + k, COLOR_SEQ % (v+40))\ 58 .replace("$BG-" + k, COLOR_SEQ % (v+40)) 59 60 if levelname == 'INFO': 61 message = message.replace("$RESET", '' if not color_specified else RESET_SEQ)\ 62 .replace("$BOLD", '')\ 63 .replace("$COLOR", color if color_specified else '') 64 return message 65 else: 66 message = message.replace("$RESET", RESET_SEQ)\ 67 .replace("$BOLD", BOLD_SEQ)\ 68 .replace("$COLOR", color) 69 70 return message
71 72 logging.ColorFormatter = ColorFormatter 73