comparison orpg/tools/orpg_log.py @ 66:c54768cffbd4 ornery-dev

Traipse Dev 'OpenRPG' {090818-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: *Unstable* This is the first wave of Code Refinement updates. Includes new material from Core Beta; new debugger material (partially implemented), beginnings of switch to etree, TerminalWriter, and a little more. open_rpg has been renamed to component; functioning now as component.get(), component.add(), component.delete(). This version has known bugs, specifically with the gametree and nodes. I think the XML files where not removed during testing of Core and switching back.
author sirebral
date Tue, 18 Aug 2009 06:33:37 -0500
parents cab94a90f9dc
children dd4be4817377
comparison
equal deleted inserted replaced
65:4840657c23c5 66:c54768cffbd4
23 # Version: 23 # Version:
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 from __future__ import with_statement
29 import sys
30 import os, os.path
31 import time
28 32
29 from orpg.orpgCore import * 33 from orpg.external.terminalwriter import TerminalWriter
34 from orpg.tools.decorators import pending_deprecation
35 from orpg.dirpath import dir_struct
30 36
31 class orpgLog: 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 class orpgLog(object):
47 _log_level = 7
48 _log_name = None
49 _log_to_console = False
50 _io = TerminalWriter(sys.stderr)
51 _lvl_args = None
52
53 def __new__(cls, *args, **kwargs):
54 it = cls.__dict__.get("__it__")
55 if it is not None:
56 return it
57 cls.__it__ = it = object.__new__(cls)
58 return it
59
32 def __init__(self, home_dir, filename='orpgRunLog '): 60 def __init__(self, home_dir, filename='orpgRunLog '):
33 self.logToConsol = True 61 self._lvl_args = {16: {'colorizer': {'green': True},
34 self.logLevel = 7 62 'log_string': 'DEBUG'},
35 self.logName = home_dir + filename + time.strftime( '%m-%d-%Y.txt', time.localtime( time.time() ) ) 63 8: {'colorizer': {'bold': True, 'green':True},
64 'log_string':'NOTE'},
65 4: {'colorizer': {'blue': True},
66 'log_string': 'INFO'},
67 2: {'colorizer': {'red': True},
68 'log_string': 'ERROR'},
69 1: {'colorizer': {'bold': True, 'red': True},
70 'log_string': 'EXCEPTION'}}
71 if not self.log_name:
72 self.log_name = home_dir + filename + time.strftime('%m-%d-%Y.txt',
73 time.localtime(time.time()))
36 74
37 def log(self, msg, type, to_consol=False): 75 def debug(self, msg, to_console=False):
38 if self.logToConsol or to_consol or type == ORPG_CRITICAL: 76 self.log(msg, ORPG_DEBUG, to_console)
39 print msg
40 77
41 if type & self.logLevel or to_consol: 78 def note(self, msg, to_console=False):
42 logMsg = time.strftime( '[%x %X] ', time.localtime( time.time() ) ) + msg + "\n" 79 self.log(msg, ORPG_NOTE, to_console)
43 logFile = open(self.logName, "a")
44 logFile.write(logMsg)
45 logFile.close()
46 80
81 def info(self, msg, to_console=False):
82 self.log(msg, ORPG_INFO, to_console)
83
84 def general(self, msg, to_console=False):
85 self.log(msg, ORPG_GENERAL, to_console)
86
87 def exception(self, msg, to_console=True):
88 self.log(msg, ORPG_CRITICAL, to_console)
89
90 def log(self, msg, log_type, to_console=False):
91 if self.log_to_console or to_console or log_type == ORPG_CRITICAL:
92 self._io.line(str(msg), **self._lvl_args[log_type]['colorizer'])
93
94
95 if log_type & self.log_level or to_console:
96 atr = {'msg': msg, 'level': self._lvl_args[log_type]['log_string']}
97 atr['time'] = time.strftime('[%x %X]', time.localtime(time.time()))
98 logMsg = '%(time)s (%(level)s) - %(msg)s\n' % (atr)
99
100 with open(self.log_name, 'a') as f:
101 f.write(logMsg)
102
103 @pending_deprecation("use logger.log_level = #")
47 def setLogLevel(self, log_level): 104 def setLogLevel(self, log_level):
48 self.logLevel = log_level 105 self.log_level = log_level
49 106
107 @pending_deprecation("use logger.log_level")
50 def getLogLevel(self): 108 def getLogLevel(self):
51 return self.logLevel 109 return self.log_level
52 110
111 @pending_deprecation("use logger.log_name = bla")
53 def setLogName(self, log_name): 112 def setLogName(self, log_name):
54 self.logName = log_name 113 self.log_name = log_name
55 114
115 @pending_deprecation("use logger.log_name")
56 def getLogName(self): 116 def getLogName(self):
57 return self.logName 117 return self.log_name
58 118
59 def setLogToConsol(self, bool): 119 @pending_deprecation("use logger.log_to_console = True/False")
60 self.logToConsol = bool 120 def setLogToConsol(self, true_or_false):
121 self.log_to_consol = true_or_false
61 122
123 @pending_deprecation("use logger.log_to_console")
62 def getLogToConsol(self): 124 def getLogToConsol(self):
63 return self.logToConsol 125 return self.log_to_consol
126
127 """
128 Property Methods
129 """
130 def _get_log_level(self):
131 return self._log_level
132 def _set_log_level(self, log_level):
133 if not isinstance(log_level, int) or log_level < 1 or log_level > 31:
134 raise TypeError("The loggers level must be an int between 1 and 31")
135
136 self._log_level = log_level
137
138 def _get_log_name(self):
139 return self._log_name
140 def _set_log_name(self, name):
141 if not os.access(os.path.abspath(os.path.dirname(name)), os.W_OK):
142 raise IOError("Could not write to the specified location")
143
144 self._log_name = name
145
146 def _get_log_to_console(self):
147 return self._log_to_console
148 def _set_log_to_console(self, true_or_false):
149 if not isinstance(true_or_false, bool):
150 raise TypeError("log_to_console must be a boolean value")
151
152 self._log_to_console = true_or_false
153
154 log_level = property(_get_log_level, _set_log_level)
155 log_name = property(_get_log_name, _set_log_name)
156 log_to_console = property(_get_log_to_console, _set_log_to_console)
157
158 logger = orpgLog(dir_struct.get("user") + "runlogs/")