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  for i in range(0,11): 
22      COLORS['Level %i'%i] = COLORS['DEBUG'] 
23   
24  RESET_SEQ = "\033[0m" 
25  COLOR_SEQ = "\033[1;%dm" 
26  BOLD_SEQ  = "\033[1m" 
27   
28 -class ColorFormatter(logging.Formatter):
29
30 - def __init__(self, *args, **kwargs):
31 # can't do super(...) here because Formatter is an old school class 32 logging.Formatter.__init__(self, *args, **kwargs)
33
34 - def format(self, record):
35 levelname = record.levelname 36 try: 37 color_choice = COLORS[levelname] 38 except KeyError: 39 color_choice = COLORS['INFO'] 40 new_args=[] 41 # A not-so-nice but working way of passing arguments to this formatter 42 # from MadGraph. 43 color_specified = False 44 for arg in record.args: 45 if isinstance(arg,str) and arg.startswith('$MG'): 46 elems=arg.split(':') 47 if len(elems)>2: 48 if elems[1]=='color': 49 color_specified = True 50 color_choice = COLORS[elems[2]] 51 if color_choice == 0: 52 color_choice = 30 53 else: 54 new_args.append(arg) 55 record.args = tuple(new_args) 56 color = COLOR_SEQ % (30 + color_choice) 57 message = logging.Formatter.format(self, record) 58 if not message.endswith('$RESET'): 59 message += '$RESET' 60 for k,v in COLORS.items(): 61 color_flag = COLOR_SEQ % (v+30) 62 message = message.replace("$" + k, color_flag)\ 63 .replace("$BG" + k, COLOR_SEQ % (v+40))\ 64 .replace("$BG-" + k, COLOR_SEQ % (v+40)) 65 66 if levelname == 'INFO': 67 message = message.replace("$RESET", '' if not color_specified else RESET_SEQ)\ 68 .replace("$BOLD", '')\ 69 .replace("$COLOR", color if color_specified else '') 70 return message 71 else: 72 message = message.replace("$RESET", RESET_SEQ)\ 73 .replace("$BOLD", BOLD_SEQ)\ 74 .replace("$COLOR", color) 75 76 return message
77 78 logging.ColorFormatter = ColorFormatter 79