/usr/share/pyshared/PyTango/log4tango.py is in python-pytango 7.2.2-1.
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 | ################################################################################
##
## This file is part of PyTango, a python binding for Tango
##
## http://www.tango-controls.org/static/PyTango/latest/doc/html/index.html
##
## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
##
## PyTango is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## PyTango is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with PyTango. If not, see <http://www.gnu.org/licenses/>.
##
################################################################################
"""
This is an internal PyTango module. It provides tango log classes that can
be used as decorators in any method of :class:`PyTango.DeviceImpl`.
To access these members use directly :mod:`PyTango` module and NOT PyTango.log4tango.
Example::
import PyTango
class MyDev(PyTango.Device_4Impl):
PyTango.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:`PyTango.DeviceImpl` subclass. The idea is to log the entrance and
exit of any decorated method.
Example::
class MyDevice(PyTango.Device_4Impl):
@PyTango.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:`PyTango.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(self, v, maxlen=25):
v = repr(v)
if len(v) > maxlen:
v = v[:maxlen-6] + " [...]"
return v
def __compact_dict(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 is_enabled(self, d):
return d.get_logger().is_debug_enabled()
def get_log_func(self, d):
return d.debug_stream
def __call__(self, f):
@functools.wraps(f)
def log_stream(*args, **kwargs):
d = args[0]
if not self.is_enabled(d):
return f(*args, **kwargs)
in_msg = "-> %s(" % f.func_name
if self._show_args:
in_msg += ", ".join(map(self.__compact, args[1:]))
if self._show_kwargs:
kwargs_str = ( self.__compact_dict(k,v) for k,v in kwargs.items() )
in_msg += ", ".join(kwargs_str)
in_msg += ")"
self.get_log_func(d)(in_msg)
ret = f(*args, **kwargs)
out_msg = ""
if self._show_ret:
out_msg += self.__compact(ret) + " "
out_msg += "<- %s()" % f.func_name
self.get_log_func(d)(out_msg)
return ret
return log_stream
class DebugIt(LogIt):
"""A class designed to be a decorator of any method of a
:class:`PyTango.DeviceImpl` subclass. The idea is to log the entrance and
exit of any decorated method as DEBUG level records.
Example::
class MyDevice(PyTango.Device_4Impl):
@PyTango.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:`PyTango.DeviceImpl` subclass. The idea is to log the entrance and
exit of any decorated method as INFO level records.
Example::
class MyDevice(PyTango.Device_4Impl):
@PyTango.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:`PyTango.DeviceImpl` subclass. The idea is to log the entrance and
exit of any decorated method as WARN level records.
Example::
class MyDevice(PyTango.Device_4Impl):
@PyTango.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:`PyTango.DeviceImpl` subclass. The idea is to log the entrance and
exit of any decorated method as ERROR level records.
Example::
class MyDevice(PyTango.Device_4Impl):
@PyTango.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:`PyTango.DeviceImpl` subclass. The idea is to log the entrance and
exit of any decorated method as FATAL level records.
Example::
class MyDevice(PyTango.Device_4Impl):
@PyTango.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
|