comparison orpg/tools/orpg_log.py @ 18:97265586402b ornery-orc

Traipse 'OpenRPG' {090827-00} Traipse is a distribution of OpenRPG that is designed to be easy to setup and go. Traipse also makes it easy for developers to work on code without fear of sacrifice. 'Ornery-Orc' continues the trend of 'Grumpy' and adds fixes to the code. 'Ornery-Orc''s main goal is to offer more advanced features and enhance the productivity of the user. Update Summary: Update Manager is now in version 0.8. While not every button works, users can now browse the different revisions and their different changesets. The code has been refined some with feature from Core added to it. A Crash report is now created if the users software crashes. Update Manager has been moved to the Traipse Suite menu item, and a Debug Console as been added as well.
author sirebral
date Thu, 27 Aug 2009 01:04:43 -0500
parents 5df1340bda13
children 51428d30c59e
comparison
equal deleted inserted replaced
17:265b987cce4f 18:97265586402b
24 # $Id: orpg_log.py,v 1.9 2007/05/06 16:43:02 digitalxero Exp $ 24 # $Id: orpg_log.py,v 1.9 2007/05/06 16:43:02 digitalxero Exp $
25 # 25 #
26 # Description: classes for orpg log messages 26 # Description: classes for orpg log messages
27 # 27 #
28 28
29 from orpg.orpgCore import * 29 from __future__ import with_statement
30 import sys, os, os.path, wx, time, traceback
30 31
31 class orpgLog: 32 from orpg.orpgCore import component
33 from orpg.external.terminalwriter import TerminalWriter
34 from orpg.tools.decorators import pending_deprecation
35 from orpg.dirpath import dir_struct
36
37 #########################
38 ## Error Types
39 #########################
40 ORPG_CRITICAL = 1
41 ORPG_GENERAL = 2
42 ORPG_INFO = 4
43 ORPG_NOTE = 8
44 ORPG_DEBUG = 16
45
46 def Crash(type, value, crash):
47 crash_report = open(dir_struct["home"] + 'crash-report.txt', "w")
48 traceback.print_exception(type, value, crash, file=crash_report)
49 crash_report.close()
50 msg = ''
51 crash_report = open(dir_struct["home"] + 'crash-report.txt', "r")
52 for line in crash_report: msg += line
53 logger.exception(msg)
54 crash_report.close()
55 logger.exception("Crash Report Created!!")
56 logger.info("Printed out crash-report.txt in your System folder", True)
57 wx.MessageBox('Crash Report Created!', 'System Failure')
58
59 class DebugConsole(wx.Frame):
60 def __init__(self, parent):
61 super(DebugConsole, self).__init__(parent, -1, "Debug Window")
62 icon = None
63 icon = wx.Icon(dir_struct["icon"]+'note.ico', wx.BITMAP_TYPE_ICO)
64 self.SetIcon( icon )
65 self.console = wx.TextCtrl(self, -1, style=wx.TE_MULTILINE | wx.TE_READONLY)
66 sizer = wx.BoxSizer(wx.VERTICAL)
67 sizer.Add(self.console, 1, wx.EXPAND)
68 self.SetSizer(sizer)
69 self.SetAutoLayout(True)
70 self.SetSize((300, 175))
71 self.Bind(wx.EVT_CLOSE, self.Min)
72 self.Min(None)
73 component.add('debugger', self.console)
74
75 def Min(self, evt):
76 self.Hide()
77
78 class orpgLog(object):
79 _log_level = 7
80 _log_name = None
81 _log_to_console = False
82 _io = TerminalWriter(sys.stderr)
83 _lvl_args = None
84
85 def __new__(cls, *args, **kwargs):
86 it = cls.__dict__.get("__it__")
87 if it is not None:
88 return it
89 cls.__it__ = it = object.__new__(cls)
90 return it
91
32 def __init__(self, home_dir, filename='orpgRunLog '): 92 def __init__(self, home_dir, filename='orpgRunLog '):
33 self.logToConsol = True 93 self._lvl_args = {16: {'colorizer': {'green': True},
34 self.logLevel = 7 94 'log_string': 'DEBUG'},
35 self.logName = home_dir + filename + time.strftime( '%m-%d-%Y.txt', time.localtime( time.time() ) ) 95 8: {'colorizer': {'bold': True, 'green':True},
96 'log_string':'NOTE'},
97 4: {'colorizer': {'blue': True},
98 'log_string': 'INFO'},
99 2: {'colorizer': {'red': True},
100 'log_string': 'ERROR'},
101 1: {'colorizer': {'bold': True, 'red': True},
102 'log_string': 'EXCEPTION'}}
103 if not self.log_name:
104 self.log_name = home_dir + filename + time.strftime('%m-%d-%Y.txt',
105 time.localtime(time.time()))
36 106
37 def log(self, msg, type, to_consol=False): 107 def debug(self, msg, to_console=False):
38 if self.logToConsol or to_consol or type == ORPG_CRITICAL: 108 self.log(msg, ORPG_DEBUG, to_console)
39 print msg
40 109
41 if type & self.logLevel or to_consol: 110 def note(self, msg, to_console=False):
42 logMsg = time.strftime( '[%x %X] ', time.localtime( time.time() ) ) + msg + "\n" 111 self.log(msg, ORPG_NOTE, to_console)
43 logFile = open(self.logName, "a")
44 logFile.write(logMsg)
45 logFile.close()
46 112
113 def info(self, msg, to_console=False):
114 self.log(msg, ORPG_INFO, to_console)
115
116 def general(self, msg, to_console=False):
117 self.log(msg, ORPG_GENERAL, to_console)
118
119 def exception(self, msg, to_console=True):
120 self.log(msg, ORPG_CRITICAL, to_console)
121
122 def log(self, msg, log_type, to_console=False):
123 if self.log_to_console or to_console or log_type == ORPG_CRITICAL:
124 try: self._io.line(str(msg), **self._lvl_args[log_type]['colorizer'])
125 except: pass #Fails without the Debug Console
126 try: component.get('debugger').AppendText(".. " + str(msg) +'\n')
127 except: pass
128
129 if log_type & self.log_level or to_console:
130 atr = {'msg': msg, 'level': self._lvl_args[log_type]['log_string']}
131 atr['time'] = time.strftime('[%x %X]', time.localtime(time.time()))
132 logMsg = '%(time)s (%(level)s) - %(msg)s\n' % (atr)
133
134 with open(self.log_name, 'a') as f:
135 f.write(logMsg)
136
137 @pending_deprecation("use logger.log_level = #")
47 def setLogLevel(self, log_level): 138 def setLogLevel(self, log_level):
48 self.logLevel = log_level 139 self.log_level = log_level
49 140
141 @pending_deprecation("use logger.log_level")
50 def getLogLevel(self): 142 def getLogLevel(self):
51 return self.logLevel 143 return self.log_level
52 144
145 @pending_deprecation("use logger.log_name = bla")
53 def setLogName(self, log_name): 146 def setLogName(self, log_name):
54 self.logName = log_name 147 self.log_name = log_name
55 148
149 @pending_deprecation("use logger.log_name")
56 def getLogName(self): 150 def getLogName(self):
57 return self.logName 151 return self.log_name
58 152
59 def setLogToConsol(self, bool): 153 @pending_deprecation("use logger.log_to_console = True/False")
60 self.logToConsol = bool 154 def setLogToConsol(self, true_or_false):
155 self.log_to_consol = true_or_false
61 156
157 @pending_deprecation("use logger.log_to_console")
62 def getLogToConsol(self): 158 def getLogToConsol(self):
63 return self.logToConsol 159 return self.log_to_consol
160
161 """
162 Property Methods
163 """
164 def _get_log_level(self):
165 return self._log_level
166 def _set_log_level(self, log_level):
167 if not isinstance(log_level, int) or log_level < 1 or log_level > 31:
168 raise TypeError("The loggers level must be an int between 1 and 31")
169
170 self._log_level = log_level
171
172 def _get_log_name(self):
173 return self._log_name
174 def _set_log_name(self, name):
175 if not os.access(os.path.abspath(os.path.dirname(name)), os.W_OK):
176 raise IOError("Could not write to the specified location")
177
178 self._log_name = name
179
180 def _get_log_to_console(self):
181 return self._log_to_console
182 def _set_log_to_console(self, true_or_false):
183 if not isinstance(true_or_false, bool):
184 raise TypeError("log_to_console must be a boolean value")
185
186 self._log_to_console = true_or_false
187
188 log_level = property(_get_log_level, _set_log_level)
189 log_name = property(_get_log_name, _set_log_name)
190 log_to_console = property(_get_log_to_console, _set_log_to_console)
191
192 logger = orpgLog(dir_struct.get("user") + "runlogs/")
193 crash = sys.excepthook = Crash