blob: 64df4f5236b2eb381053f064d3c585a1e89ab320 [file] [log] [blame]
Dan Talaycof75360a2010-02-05 22:22:54 -08001"""This module implements output printing.
2
3Output are divided into 4 levels and
4can be configured for different verbosity
5
6Copyright(C) 2009, Stanford University
7Date August 2009
8Created by ykk
9"""
10
11##Various output modes
12MODE = {}
13MODE["ERR"] = 0
14MODE["WARN"] = 1
15MODE["INFO"] = 2
16MODE["DBG"] = 3
17
18#Global mode
19global output_mode
20output_mode = None
21
22def set_mode(msg_mode, who=None):
23 """Set the message mode for who
24 If who is None, set global mode
25 """
26 global output_mode
27 if (output_mode == None):
28 output_mode = {}
29 output_mode["global"] = MODE["WARN"]
30 output_mode["DBG"] = []
31 output_mode["INFO"] = []
32 output_mode["WARN"] = []
33
34 #Set global mode
35 if (who == None):
36 output_mode["global"] = MODE[msg_mode]
37 return
38
39 #Individual mode
40 if (msg_mode == "ERR"):
41 return
42 for mode in ["WARN","INFO","DBG"]:
43 if (not (who in mode[mode])):
44 mode[mode].append(who)
45 if (msg_mode == mode):
46 return
47
48def output(msg_mode, msg, who=None):
49 """Print message
50 """
51 global output_mode
52 if (output_mode == None):
53 raise RuntimeException("Output mode is not set")
54
55 #Indicate who string
56 if (who == None):
57 whostr = ""
58 else:
59 whostr = who+":"
60
61 #Print output
62 if (MODE[msg_mode] <= output_mode["global"]):
63 print msg_mode.ljust(4, ' ')+"|"+whostr+msg
64 elif (who in output_mode[msg_mode]):
65 print msg_mode.ljust(4, ' ')+"|"+whostr+msg
66
67def err(msg, who=None):
68 """Print error messages
69 """
70 output("ERR", msg, who)
71
72def warn(msg, who=None):
73 """Print warning messages
74 """
75 output("WARN", msg, who)
76
77def info(msg, who=None):
78 """Print informational messages
79 """
80 output("INFO", msg, who)
81
82def dbg(msg, who=None):
83 """Print debug messages
84 """
85 output("DBG", msg, who)