comparison src/core/log.py @ 1008:d70d4fe5c5f8

core (log): added magic %(profile)s key to log_fmt: if %(profile)s is used in log_fmt, SàT try to find profile value using introspection. This is for debugging purpose only, *DO NOT* use in production. This key don't work (yet ?) with standard backend, and will not work in every frontend.
author Goffi <goffi@goffi.org>
date Mon, 05 May 2014 18:58:34 +0200
parents a7d33c7a8277
children 73a0b7f94674
comparison
equal deleted inserted replaced
1007:a7d33c7a8277 1008:d70d4fe5c5f8
92 if not self.filter_name.dictFilter(record): 92 if not self.filter_name.dictFilter(record):
93 raise Filtered 93 raise Filtered
94 except AttributeError: 94 except AttributeError:
95 if self.filter_name is not None: 95 if self.filter_name is not None:
96 raise ValueError("Bad filter: filters must have a .filter method") 96 raise ValueError("Bad filter: filters must have a .filter method")
97 return self.fmt % record if self.fmt is not None else message 97 try:
98 return self.fmt % record
99 except TypeError:
100 return message
101 except KeyError as e:
102 if e.args[0] == 'profile':
103 # XXX: %(profile)s use some magic with introspection, for debugging purpose only *DO NOT* use in production
104 record['profile'] = _getProfile()
105 return self.fmt % record
106 else:
107 raise e
108
98 109
99 def debug(self, msg): 110 def debug(self, msg):
100 self.log(C.LOG_LVL_DEBUG, msg) 111 self.log(C.LOG_LVL_DEBUG, msg)
101 112
102 def info(self, msg): 113 def info(self, msg):
156 pass 167 pass
157 log_record = LogRecord() 168 log_record = LogRecord()
158 log_record.name = dict_record['name'] 169 log_record.name = dict_record['name']
159 return self.filter(log_record) == 1 170 return self.filter(log_record) == 1
160 171
172
173 def _getProfile():
174 """Try to find profile value using introspection"""
175 import inspect
176 stack = inspect.stack()
177 current_path = stack[0][1]
178 for frame_data in stack[:-1]:
179 if frame_data[1] != current_path:
180 break
181
182 frame = frame_data[0]
183 args = inspect.getargvalues(frame)
184 try:
185 profile = args.locals.get('profile') or args.locals['profile_key']
186 except (TypeError, KeyError):
187 try:
188 try:
189 profile = args.locals['self'].profile
190 except AttributeError:
191 profile = args.locals['self'].parent.profile
192 except Exception:
193 # we can't find profile, we return an empty value
194 profile = ''
195 return profile
161 196
162 def _ansiColors(level, message): 197 def _ansiColors(level, message):
163 """Colorise message depending on level for terminals 198 """Colorise message depending on level for terminals
164 199
165 @param level: one of C.LOG_LEVELS 200 @param level: one of C.LOG_LEVELS