/usr/lib/python3/dist-packages/tango/log4tango.py is in python3-tango 9.2.2-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | # ------------------------------------------------------------------------------
# This file is part of PyTango (http://pytango.rtfd.io)
#
# Copyright 2006-2012 CELLS / ALBA Synchrotron, Bellaterra, Spain
# Copyright 2013-2014 European Synchrotron Radiation Facility, Grenoble, France
#
# Distributed under the terms of the GNU Lesser General Public License,
# either version 3 of the License, or (at your option) any later version.
# See LICENSE.txt for more info.
# ------------------------------------------------------------------------------
"""
This is an internal PyTango module. It provides tango log classes that can
be used as decorators in any method of :class:`tango.DeviceImpl`.
To access these members use directly :mod:`tango` module and NOT tango.log4tango.
Example::
import tango
class MyDev(tango.Device_4Impl):
tango.InfoIt()
def read_Current(self, attr):
attr.set_value(self._current)
"""
__all__ = ["TangoStream", "LogIt", "DebugIt", "InfoIt", "WarnIt",
"ErrorIt", "FatalIt"]
__docformat__ = "restructuredtext"
import functools
class TangoStream:
def __init__(self, fn):
self._fn = fn
self._accum = ""
def write(self, s):
self._accum += s
# while there is no new line, just accumulate the buffer
try:
if s[-1] == '\n' or s.index('\n') >= 0:
self.flush()
except ValueError:
pass
def flush(self):
b = self._accum
if b is None or len(self._accum) == 0:
return
# take the '\n' because the log adds it
if b[-1] == '\n':
b = b[:-1]
self._fn(b)
self._accum = ""
class LogIt(object):
"""A class designed to be a decorator of any method of a
:class:`tango.DeviceImpl` subclass. The idea is to log the entrance and
exit of any decorated method.
Example::
class MyDevice(tango.Device_4Impl):
@tango.LogIt()
def read_Current(self, attr):
attr.set_value(self._current, 1)
All log messages generated by this class have DEBUG level. If you whish
to have different log level messages, you should implement subclasses that
log to those levels. See, for example, :class:`tango.InfoIt`.
The constructor receives three optional arguments:
* show_args - shows method arguments in log message (defaults to False)
* show_kwargs - shows keyword method arguments in log message (defaults to False)
* show_ret - shows return value in log message (defaults to False)
"""
def __init__(self, show_args=False, show_kwargs=False, show_ret=False):
"""Initializes de LogIt object.
:param show_args: (bool) show arguments in log message (default is False)
:param show_kwargs: (bool) show keyword arguments in log message (default is False)
:param show_ret: (bool) show return in log message (default is False)
"""
self._show_args = show_args
self._show_kwargs = show_kwargs
self._show_ret = show_ret
def __compact_elem(self, v, maxlen=25):
v = repr(v)
if len(v) > maxlen:
v = v[:maxlen - 6] + " [...]"
return v
def __compact_elems(self, elems):
return map(self.__compact_elem, elems)
def __compact_elems_str(self, elems):
return ", ".join(self.__compact_elems(elems))
def __compact_item(self, k, v, maxlen=None):
if maxlen is None:
return "%s=%s" % (k, self.__compact(v))
return "%s=%s" % (k, self.__compact(v, maxlen=maxlen))
def __compact_dict(self, d, maxlen=None):
return (self.__compact_item(k, v) for k, v in d.items())
def __compact_dict_str(self, d, maxlen=None):
return ", ".join(self.__compact_dict(d, maxlen=maxlen))
def is_enabled(self, obj):
return obj.get_logger().is_debug_enabled()
def get_log_func(self, obj):
return obj.debug_stream
def __call__(self, f):
@functools.wraps(f)
def log_stream(*args, **kwargs):
dev = args[0]
if not self.is_enabled(dev):
return f(*args, **kwargs)
log = self.get_log_func(dev)
f_name = dev.__class__.__name__ + "." + f.__name__
sargs = ""
if self._show_args:
sargs = self.__compact_elems_str(args[1:])
if self._show_kwargs:
sargs += self.__compact_dict_str(kwargs)
log("-> {0}({1})".format(f_name, sargs))
with_exc = True
try:
ret = f(*args, **kwargs)
with_exc = False
return ret
finally:
if with_exc:
log("<- {0}() raised exception!".format(f_name))
else:
sret = ""
if self._show_ret:
sret = self.__compact_elem(ret) + " "
log("{0}<- {1}()".format(sret, f_name))
log_stream._wrapped = f
return log_stream
class DebugIt(LogIt):
"""A class designed to be a decorator of any method of a
:class:`tango.DeviceImpl` subclass. The idea is to log the entrance and
exit of any decorated method as DEBUG level records.
Example::
class MyDevice(tango.Device_4Impl):
@tango.DebugIt()
def read_Current(self, attr):
attr.set_value(self._current, 1)
All log messages generated by this class have DEBUG level.
The constructor receives three optional arguments:
* show_args - shows method arguments in log message (defaults to False)
* show_kwargs - shows keyword method arguments in log message (defaults to False)
* show_ret - shows return value in log message (defaults to False)
"""
def is_enabled(self, d):
return d.get_logger().is_debug_enabled()
def get_log_func(self, d):
return d.debug_stream
class InfoIt(LogIt):
"""A class designed to be a decorator of any method of a
:class:`tango.DeviceImpl` subclass. The idea is to log the entrance and
exit of any decorated method as INFO level records.
Example::
class MyDevice(tango.Device_4Impl):
@tango.InfoIt()
def read_Current(self, attr):
attr.set_value(self._current, 1)
All log messages generated by this class have INFO level.
The constructor receives three optional arguments:
* show_args - shows method arguments in log message (defaults to False)
* show_kwargs - shows keyword method arguments in log message (defaults to False)
* show_ret - shows return value in log message (defaults to False)
"""
def is_enabled(self, d):
return d.get_logger().is_info_enabled()
def get_log_func(self, d):
return d.info_stream
class WarnIt(LogIt):
"""A class designed to be a decorator of any method of a
:class:`tango.DeviceImpl` subclass. The idea is to log the entrance and
exit of any decorated method as WARN level records.
Example::
class MyDevice(tango.Device_4Impl):
@tango.WarnIt()
def read_Current(self, attr):
attr.set_value(self._current, 1)
All log messages generated by this class have WARN level.
The constructor receives three optional arguments:
* show_args - shows method arguments in log message (defaults to False)
* show_kwargs - shows keyword method arguments in log message (defaults to False)
* show_ret - shows return value in log message (defaults to False)
"""
def is_enabled(self, d):
return d.get_logger().is_warn_enabled()
def get_log_func(self, d):
return d.warn_stream
class ErrorIt(LogIt):
"""A class designed to be a decorator of any method of a
:class:`tango.DeviceImpl` subclass. The idea is to log the entrance and
exit of any decorated method as ERROR level records.
Example::
class MyDevice(tango.Device_4Impl):
@tango.ErrorIt()
def read_Current(self, attr):
attr.set_value(self._current, 1)
All log messages generated by this class have ERROR level.
The constructor receives three optional arguments:
* show_args - shows method arguments in log message (defaults to False)
* show_kwargs - shows keyword method arguments in log message (defaults to False)
* show_ret - shows return value in log message (defaults to False)
"""
def is_enabled(self, d):
return d.get_logger().is_error_enabled()
def get_log_func(self, d):
return d.error_stream
class FatalIt(LogIt):
"""A class designed to be a decorator of any method of a
:class:`tango.DeviceImpl` subclass. The idea is to log the entrance and
exit of any decorated method as FATAL level records.
Example::
class MyDevice(tango.Device_4Impl):
@tango.FatalIt()
def read_Current(self, attr):
attr.set_value(self._current, 1)
All log messages generated by this class have FATAL level.
The constructor receives three optional arguments:
* show_args - shows method arguments in log message (defaults to False)
* show_kwargs - shows keyword method arguments in log message (defaults to False)
* show_ret - shows return value in log message (defaults to False)
"""
def is_enabled(self, d):
return d.get_logger().is_fatal_enabled()
def get_log_func(self, d):
return d.fatal_stream
|