logUtil.py (10127B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\script\util\logUtil.py 2 import logging 3 import logmodule 4 import sys 5 from stringutil import strx 6 from stdlogutils import LineWrap 7 from eveprefs import prefs, boot 8 logger = logging.getLogger(__name__) 9 logger.addHandler(logging.NullHandler()) 10 11 def supersafestr(o): 12 try: 13 return strx(o) 14 except Exception as ex: 15 default = '<UNFORMATABLE>' 16 logger.exception('Error formatting an argument to strx2. Obviously it cannot be printed but you should be able to figure it out from the stacktrace. Returning %s', default) 17 return default 18 19 20 class LogMixin(): 21 22 def __init__(self, logChannel = None, bindObject = None): 23 logChannelName = self.GetLogChannelName(logChannel, bindObject) 24 self.__logname__ = self.GetLogName(logChannelName) 25 self.logChannel = logmodule.GetChannel(logChannelName) 26 self.LoadPrefs() 27 self.logContexts = {} 28 for each in ('Info', 'Notice', 'Warn', 'Error'): 29 self.logContexts[each] = 'Logging::' + each 30 31 def GetLogChannelName(self, logChannelName = None, bindObject = None): 32 if type(logChannelName) not in [str, type(None)]: 33 raise Exception('logChannelName must be a string!') 34 if logChannelName and bindObject: 35 raise Exception('Conflicting log channel, provide logChannelName or bindObject, not both') 36 bindguid = getattr(bindObject, '__guid__', None) 37 bindLogName = getattr(bindObject, '__logname__', None) 38 selfguid = getattr(self, '__guid__', None) 39 return logChannelName or bindguid or bindLogName or selfguid or 'nonsvc.General' 40 41 def GetLogName(self, logChannelName): 42 tokens = logChannelName.split('.') 43 if len(tokens) == 1: 44 return tokens[0] 45 else: 46 return tokens[1] 47 48 def ArrangeArguments(self, *args, **keywords): 49 self.DeprecateKeywords(**keywords) 50 argsList = [] 51 prefix = self.GetLogPrefix() 52 if prefix: 53 argsList.append(prefix) 54 for item in args: 55 argsList.append(item) 56 57 return argsList 58 59 def DeprecateKeywords(self, **keywords): 60 if len(keywords): 61 self.LogError('ERROR: keyword arguements passed into a log function') 62 logmodule.LogTraceback() 63 64 def GetLogPrefix(self): 65 return None 66 67 def DudLogger(self, *args, **keywords): 68 pass 69 70 def ShouldLogMethodCalls(self): 71 if not getattr(self, 'isLogInfo', 0): 72 return False 73 if not prefs.GetValue('logMethodCalls', boot.role != 'client'): 74 return False 75 return self.logChannel.IsLogChannelOpen(logmodule.LGINFO) 76 77 def LogMethodCall(self, *args, **keywords): 78 argsList = self.ArrangeArguments(*args, **keywords) 79 logChannel = logmodule.methodcalls 80 try: 81 if len(argsList) == 1: 82 s = supersafestr(argsList[0]) 83 else: 84 s = ' '.join(map(supersafestr, argsList)) 85 logChannel.Log(s, logmodule.LGINFO, 1, force=True) 86 except TypeError: 87 logChannel.Log('[X]'.join(map(supersafestr, argsList)).replace('\x00', '\\0'), logmodule.LGINFO, 1, force=True) 88 sys.exc_clear() 89 except UnicodeEncodeError: 90 logChannel.Log('[U]'.join(map(lambda x: x.encode('ascii', 'replace'), map(unicode, argsList))), logmodule.LGINFO, 1, force=True) 91 sys.exc_clear() 92 93 def LogInfo(self, *args, **keywords): 94 argsList = self.ArrangeArguments(*args, **keywords) 95 if getattr(self, 'isLogInfo', 0) and self.logChannel.IsLogChannelOpen(logmodule.LGINFO): 96 try: 97 if len(argsList) == 1: 98 s = supersafestr(argsList[0]) 99 else: 100 s = ' '.join(map(supersafestr, argsList)) 101 self.logChannel.Log(s, logmodule.LGINFO, 1, force=True) 102 except TypeError: 103 self.logChannel.Log('[X]'.join(map(supersafestr, argsList)).replace('\x00', '\\0'), logmodule.LGINFO, 1, force=True) 104 sys.exc_clear() 105 except UnicodeEncodeError: 106 self.logChannel.Log('[U]'.join(map(lambda x: x.encode('ascii', 'replace'), map(unicode, argsList))), logmodule.LGINFO, 1, force=True) 107 sys.exc_clear() 108 109 def LogWarn(self, *args, **keywords): 110 argsList = self.ArrangeArguments(*args, **keywords) 111 if self.isLogWarning and self.logChannel.IsLogChannelOpen(logmodule.LGWARN) or charsession and not boot.role == 'client': 112 try: 113 if len(argsList) == 1: 114 s = supersafestr(argsList[0]) 115 else: 116 s = ' '.join(map(supersafestr, argsList)) 117 if self.logChannel.IsOpen(logmodule.LGWARN): 118 self.logChannel.Log(s, logmodule.LGWARN, 1, force=True) 119 for x in LineWrap(s, 10): 120 if charsession and not boot.role == 'client': 121 charsession.LogSessionHistory(x, None, 1) 122 123 except TypeError: 124 sys.exc_clear() 125 x = '[X]'.join(map(supersafestr, argsList)).replace('\x00', '\\0') 126 if self.logChannel.IsOpen(logmodule.LGWARN): 127 self.logChannel.Log(x, logmodule.LGWARN, 1, force=True) 128 if charsession and not boot.role == 'client': 129 charsession.LogSessionHistory(x, None, 1) 130 except UnicodeEncodeError: 131 sys.exc_clear() 132 x = '[U]'.join(map(lambda x: x.encode('ascii', 'replace'), map(unicode, argsList))) 133 if self.logChannel.IsOpen(logmodule.LGWARN): 134 self.logChannel.Log(x, logmodule.LGWARN, 1, force=True) 135 if charsession and not boot.role == 'client': 136 charsession.LogSessionHistory(x, None, 1) 137 138 def LogError(self, *args, **keywords): 139 argsList = self.ArrangeArguments(*args, **keywords) 140 if self.logChannel.IsOpen(logmodule.LGERR) or charsession: 141 try: 142 if len(argsList) == 1: 143 s = supersafestr(argsList[0]) 144 else: 145 s = ' '.join(map(supersafestr, argsList)) 146 if self.logChannel.IsOpen(logmodule.LGERR): 147 self.logChannel.Log(s, logmodule.LGERR, 1) 148 for x in LineWrap(s, 40): 149 if charsession: 150 charsession.LogSessionHistory(x, None, 1) 151 152 except TypeError: 153 sys.exc_clear() 154 x = '[X]'.join(map(supersafestr, argsList)).replace('\x00', '\\0') 155 if self.logChannel.IsOpen(logmodule.LGERR): 156 self.logChannel.Log(x, logmodule.LGERR, 1) 157 if charsession: 158 charsession.LogSessionHistory(x, None, 1) 159 except UnicodeEncodeError: 160 sys.exc_clear() 161 x = '[U]'.join(map(lambda x: x.encode('ascii', 'replace'), map(unicode, argsList))) 162 if self.logChannel.IsOpen(logmodule.LGERR): 163 self.logChannel.Log(x, logmodule.LGERR, 1) 164 if charsession and not boot.role == 'client': 165 charsession.LogSessionHistory(x, None, 1) 166 167 def LogNotice(self, *args, **keywords): 168 argsList = self.ArrangeArguments(*args, **keywords) 169 if getattr(self, 'isLogNotice', 0) and self.logChannel.IsLogChannelOpen(logmodule.LGNOTICE): 170 try: 171 if len(argsList) == 1: 172 s = supersafestr(argsList[0]) 173 else: 174 s = ' '.join(map(supersafestr, argsList)) 175 self.logChannel.Log(s, logmodule.LGNOTICE, 1, force=True) 176 except TypeError: 177 self.logChannel.Log('[X]'.join(map(supersafestr, argsList)).replace('\x00', '\\0'), logmodule.LGNOTICE, 1, force=True) 178 sys.exc_clear() 179 except UnicodeEncodeError: 180 self.logChannel.Log('[U]'.join(map(lambda x: x.encode('ascii', 'replace'), map(unicode, argsList))), logmodule.LGNOTICE, 1, force=True) 181 sys.exc_clear() 182 183 def LoadPrefs(self): 184 self.isLogInfo = bool(prefs.GetValue('logInfo', 1)) 185 self.isLogWarning = bool(prefs.GetValue('logWarning', 1)) 186 self.isLogNotice = bool(prefs.GetValue('logNotice', 1)) 187 188 def SetLogInfo(self, b): 189 if not b and self.isLogInfo: 190 self.LogInfo('*** LogInfo stopped for ', self.__guid__) 191 old = self.isLogInfo 192 self.isLogInfo = b 193 if b and not old: 194 self.LogInfo('*** LogInfo started for ', self.__guid__) 195 196 def SetLogNotice(self, b): 197 if not b and self.isLogNotice: 198 self.LogInfo('*** LogNotice stopped for ', self.__guid__) 199 old = self.isLogNotice 200 self.isLogNotice = b 201 if b and not old: 202 self.LogInfo('*** LogNotice started for ', self.__guid__) 203 204 def SetLogWarning(self, b): 205 if not b and self.isLogWarning: 206 self.LogWarn('*** LogWarn stopped for ', self.__guid__) 207 old = self.isLogWarning 208 self.isLogWarning = b 209 if b and not old: 210 self.LogWarn('*** LogWarn started for ', self.__guid__) 211 212 213 def _Log(severity, what): 214 try: 215 s = ' '.join(map(str, what)) 216 except UnicodeEncodeError: 217 218 def conv(what): 219 if isinstance(what, unicode): 220 return what.encode('ascii', 'replace') 221 return str(what) 222 223 s = ' '.join(map(conv, what)) 224 225 logmodule.general.Log(s.replace('\x00', '\\0'), severity) 226 227 228 def LogInfo(*what): 229 _Log(logmodule.LGINFO, what) 230 231 232 def LogNotice(*what): 233 _Log(logmodule.LGNOTICE, what) 234 235 236 def LogWarn(*what): 237 _Log(logmodule.LGWARN, what) 238 239 240 def LogError(*what): 241 _Log(logmodule.LGERR, what) 242 243 244 LogException = logmodule.LogException 245 LogTraceback = logmodule.LogTraceback 246 import carbon.common.script.util.autoexport as autoexport 247 exports = autoexport.AutoExports('log', locals())