Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 1 | """This module implements output printing. |
| 2 | |
| 3 | Output are divided into 4 levels and |
| 4 | can be configured for different verbosity |
| 5 | |
| 6 | Copyright(C) 2009, Stanford University |
| 7 | Date August 2009 |
| 8 | Created by ykk |
| 9 | """ |
| 10 | |
| 11 | ##Various output modes |
| 12 | MODE = {} |
| 13 | MODE["ERR"] = 0 |
| 14 | MODE["WARN"] = 1 |
| 15 | MODE["INFO"] = 2 |
| 16 | MODE["DBG"] = 3 |
| 17 | |
| 18 | #Global mode |
| 19 | global output_mode |
| 20 | output_mode = None |
| 21 | |
| 22 | def 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 | |
| 48 | def 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 | |
| 67 | def err(msg, who=None): |
| 68 | """Print error messages |
| 69 | """ |
| 70 | output("ERR", msg, who) |
| 71 | |
| 72 | def warn(msg, who=None): |
| 73 | """Print warning messages |
| 74 | """ |
| 75 | output("WARN", msg, who) |
| 76 | |
| 77 | def info(msg, who=None): |
| 78 | """Print informational messages |
| 79 | """ |
| 80 | output("INFO", msg, who) |
| 81 | |
| 82 | def dbg(msg, who=None): |
| 83 | """Print debug messages |
| 84 | """ |
| 85 | output("DBG", msg, who) |