Commit 83dabe8f authored by Piergiorgio Pancino's avatar Piergiorgio Pancino
Browse files

* Update of log interface on controllers/modules

* some bugfixes
parent 2fa96ed3
......@@ -7,7 +7,7 @@
from warnings import warn
from .embl import ExporterClient
from bliss.common.logtools import LogMixin
from bliss.common.logtools import *
from bliss.common import session
import gevent
......@@ -27,7 +27,7 @@ def start_exporter(address, port, timeout=3, retries=1):
return exporter_clients[(address, port)]
class Exporter(ExporterClient.ExporterClient, LogMixin):
class Exporter(ExporterClient.ExporterClient):
STATE_EVENT = "State"
STATUS_EVENT = "Status"
VALUE_EVENT = "Value"
......@@ -145,8 +145,9 @@ class Exporter(ExporterClient.ExporterClient, LogMixin):
try:
cb(self._to_python_value(value))
except:
self._logger.exception(
"Exception while executing callback %s for event %s", cb, name
log_exception(
self,
f"Exception while executing callback {cb} for event {name}",
)
continue
......
......@@ -32,7 +32,7 @@ from bliss.comm.util import HexMsg
from bliss.common.tango import DeviceProxy
from bliss.common import session
from bliss.common.logtools import LogMixin
from bliss.common.logtools import *
__TMO_TUPLE = (
0.0,
......@@ -126,7 +126,7 @@ class PrologixError(GpibError):
pass
class Prologix(LogMixin):
class Prologix:
def __init__(self, cnt, **keys):
url = keys.pop("url")
url_parse = re.compile(r"^(prologix://)?([^:/]+):?([0-9]*)$")
......@@ -137,51 +137,52 @@ class Prologix(LogMixin):
port = match.group(3) and int(match.group(3)) or 1234
self._sock = Socket(hostname, port, timeout=keys.get("timeout"))
session.get_current().map.register(self, children_list=["comms", self._sock])
self._logger.debug(f"Prologix::__init__() host = {hostname} port = {port}")
log_debug(self, f"Prologix::__init__() host = {hostname} port = {port}")
self._gpib_kwargs = keys
def init(self):
self._logger.debug("Prologix::init()")
log_debug(self, "Prologix::init()")
if not self._sock._connected:
# the Prologix must be a controller (mode 1)
self._logger.debug("Prologix::init(): set to mode 1 (Controller) ")
log_debug(self, "Prologix::init(): set to mode 1 (Controller) ")
self._sock.write(b"++mode 1\n")
self._sock.write(b"++clr\n")
self._logger.debug("Prologix::init() save the configuration set to 0")
log_debug(self, "Prologix::init() save the configuration set to 0")
self._sock.write(b"++savecfg 0\n")
self._logger.debug("Prologix::init() auto (read_after_write) set to 0")
log_debug(self, "Prologix::init() auto (read_after_write) set to 0")
self._sock.write(b"++auto 0\n")
self._eol = self._gpib_kwargs["eol"]
if self._eol == "\r\n":
self._logger.debug_data("Prologix::init() eos set to 0 (%s)", self._eol)
log_debug_data(self, "Prologix::init() eos set to 0 (%s)", self._eol)
self._sock.write(b"++eos 0\n")
elif self._eol == "\r":
self._logger.debug("Prologix::init() eos set to 1 (%s)" % self._eol)
log_debug(self, "Prologix::init() eos set to 1 (%s)" % self._eol)
self._sock.write(b"++eos 1\n")
elif self._eol == "\n":
self._logger.debug("Prologix::init() eos set to 2 (%s)" % self._eol)
log_debug(self, "Prologix::init() eos set to 2 (%s)" % self._eol)
self._sock.write(b"++eos 2\n")
else:
self._logger.debug("Prologix::init() eos set to 3 (%s)" % self._eol)
log_debug(self, "Prologix::init() eos set to 3 (%s)" % self._eol)
self._sock.write(b"++eos 3\n")
self._logger.debug("Prologix::init() eoi set to 1")
log_debug(self, "Prologix::init() eoi set to 1")
self._sock.write(b"++eoi 1\n")
self._logger.debug("Prologix::init() read_tmo_ms set to 13")
log_debug(self, "Prologix::init() read_tmo_ms set to 13")
self._sock.write(b"++read_tmo_ms 13\n")
# the gpib address
self._sad = self._gpib_kwargs.get("sad", 0)
self._pad = self._gpib_kwargs["pad"]
if self._sad == 0:
self._logger.debug(
"Prologix::init() gpib primary address set to %d" % self._pad
log_debug(
self, "Prologix::init() gpib primary address set to %d" % self._pad
)
self._sock.write(b"++addr %d\n" % self._pad)
else:
self._logger.debug(
log_debug(
self,
"Prologix::init() gpib primary & secondary address' set to %d:%d"
% (self._pad, self._sad)
% (self._pad, self._sad),
)
self._sock.write(b"++addr %d %d\n" % (self._pad, self._sad))
......@@ -198,7 +199,7 @@ class Prologix(LogMixin):
"""
def ibwrt(self, cmd):
self._logger.debug("Sent: %s" % cmd)
log_debug(self, "Sent: %s" % cmd)
cmd = (
cmd.replace(b"\33", b"\33" + b"\33")
.replace(b"+", b"\33" + b"+")
......@@ -223,7 +224,7 @@ def TangoGpib(cnt, **keys):
return Object(keys.pop("url"), green_mode=GreenMode.Gevent)
class TangoDeviceServer(LogMixin):
class TangoDeviceServer:
def __init__(self, cnt, **keys):
url = keys.pop("url")
url_tocken = "tango_gpib_device_server://"
......@@ -239,7 +240,7 @@ class TangoDeviceServer(LogMixin):
session.get_current().map.register(self)
def init(self):
self._logger.debug("TangoDeviceServer::init()")
log_debug(self, "TangoDeviceServer::init()")
if self._proxy is None:
self._proxy = DeviceProxy(self._tango_url)
......@@ -247,7 +248,7 @@ class TangoDeviceServer(LogMixin):
self._proxy = None
def ibwrt(self, cmd):
self._logger.debug("Sent: %s" % cmd)
log_debug(self, "Sent: %s" % cmd)
ncmd = numpy.zeros(4 + len(cmd), dtype=numpy.uint8)
ncmd[3] = self._pad
ncmd[2] = self._sad
......@@ -257,7 +258,7 @@ class TangoDeviceServer(LogMixin):
def ibrd(self, length):
self._proxy.SetTimeout([self._pad_sad, self._gpib_kwargs.get("tmo", 12)])
msg = self._proxy.ReceiveBinData([self._pad_sad, length])
self._logger.debug("Received: %s" % msg)
log_debug(self, "Received: %s" % msg)
return msg.tostring()
def _raw(self, length):
......@@ -268,7 +269,7 @@ class LocalGpibError(GpibError):
pass
class LocalGpib(LogMixin):
class LocalGpib:
URL_RE = re.compile(r"^(local://)?([0-9]{1,2})$")
......@@ -288,19 +289,19 @@ class LocalGpib(LogMixin):
return "{0}(board={1})".format(type(self).__name__, self.board_index)
def init(self):
self._logger.debug("init()")
log_debug(self, "init()")
opts = self._gpib_kwargs
from . import libgpib
self.gpib = libgpib
self._logger.debug("libgpib version %s", self.gpib.ibvers())
log_debug(self, "libgpib version %s", self.gpib.ibvers())
self.gpib.GPIBError = LocalGpibError
self.ud = self.gpib.ibdev(
self.board_index, pad=opts["pad"], sad=opts["sad"], tmo=opts["tmo"]
)
def ibwrt(self, cmd):
self._logger.debug("Sent: %r" % cmd)
log_debug(self, "Sent: %r" % cmd)
tp = gevent.get_hub().threadpool
return tp.spawn(self.gpib.ibwrt, self.ud, cmd).get()
......@@ -338,7 +339,7 @@ def try_open(fu):
return rfunc
class Gpib(LogMixin):
class Gpib:
"""Gpib object
from bliss.comm.gpib import Gpib
......@@ -388,8 +389,8 @@ class Gpib(LogMixin):
def open(self):
if self._raw_handler is None:
self._logger.debug(f"opening {self.gpib_type} gpib")
self._logger.debug(self._logger.log_format_dict(self._gpib_kwargs))
log_debug(self, f"opening {self.gpib_type} gpib")
log_debug_data(self, "kwargs", self._gpib_kwargs)
if self.gpib_type == Gpib.GpibType.ENET:
self._raw_handler = Enet(self, **self._gpib_kwargs)
self._raw_handler.init()
......@@ -409,13 +410,13 @@ class Gpib(LogMixin):
if self._raw_handler is not None:
self._raw_handler.close()
self._raw_handler = None
self._logger.debug("close")
log_debug(self, "close")
@try_open
def raw_read(self, maxsize=None, timeout=None):
size_to_read = maxsize or self.READ_BLOCK_SIZE
msg = self._raw_handler.ibrd(size_to_read)
self._logger.debug_data("raw_read", msg)
log_debug_data(self, "raw_read", msg)
return msg
def read(self, size=1, timeout=None):
......@@ -425,7 +426,7 @@ class Gpib(LogMixin):
@try_open
def _read(self, size=1):
msg = self._raw_handler.ibrd(size)
self._logger.debug_data("read", msg)
log_debug_data(self, "read", msg)
return msg
def readline(self, eol=None, timeout=None):
......@@ -447,7 +448,7 @@ class Gpib(LogMixin):
eol_pos = self._data.find(local_eol)
msg = self._data[:eol_pos]
self._data = self._data[eol_pos + len(local_eol) :]
self._logger.debug_data("readline", msg)
log_debug_data(self, "readline", msg)
return msg
def write(self, msg, timeout=None):
......@@ -456,7 +457,7 @@ class Gpib(LogMixin):
@try_open
def _write(self, msg):
self._logger.debug_data("write", msg)
log_debug_data(self, "write", msg)
return self._raw_handler.ibwrt(msg)
@protect_from_kill
......@@ -489,7 +490,7 @@ class Gpib(LogMixin):
return r_lines
def flush(self):
self._logger.debug("flush")
log_debug(self, "flush")
self._raw_handler = None
def _check_type(self):
......
......@@ -36,7 +36,7 @@
import socket, sys
from struct import *
from bliss.common import session
from bliss.common.logtools import LogMixin
from bliss.common.logtools import *
# debug = ["io", "ignore_not_impl"] # "dummy_io", "rw"
debug = ["ignore_not_impl"]
......@@ -66,7 +66,7 @@ def _not_impl(name):
return _dbg(wrap, name)
class EnetSocket(LogMixin):
class EnetSocket:
def __init__(self, host, port=5000):
self._host = host
self._port = port
......
......@@ -20,7 +20,7 @@ from ..common.greenlet_utils import KillMask, protect_from_kill
from . import serial
from bliss.common import session
from bliss.common.logtools import LogMixin
from bliss.common.logtools import *
class ModbusError(CommunicationError):
......@@ -90,7 +90,7 @@ class Modbus_ASCII:
return lrc & 0xff
class Modbus_RTU(LogMixin):
class Modbus_RTU:
def __init__(self, node, *args, **kwargs):
self._serial = serial.Serial(*args, **kwargs)
self.node = node
......
......@@ -43,7 +43,7 @@ import numpy
from .util import get_interface
from .exceptions import CommunicationError, CommunicationTimeout
from bliss.common import session
from bliss.common.logtools import LogMixin
from bliss.common.logtools import *
def decode_IDN(s):
......@@ -381,7 +381,7 @@ def sanitize_msgs(*msgs, **opts):
return commands, queries, eol.join(result) + eol
class SCPI(LogMixin):
class SCPI:
"""
:term:`SCPI` language helper.
......@@ -576,10 +576,10 @@ class SCPI(LogMixin):
eol = kwargs.setdefault("eol", self._eol)
strict_query = kwargs.setdefault("strict_query", self._strict_query)
cmds, queries, msg = sanitize_msgs(*msgs, **kwargs)
self._logger.debug("[start] read %r", msg)
log_debug(self, f"[start] read {msg}")
raw_results = self.interface.write_readlines(msg.encode(), len(queries))
raw_results = [r.decode() for r in raw_results]
self._logger.debug("[ end ] read %r=%r", msg, raw_results)
log_debug(self, f"[ end ] read {msg}={raw_results}")
if raw:
return raw_results
if len(queries) != len(raw_results):
......@@ -595,9 +595,7 @@ class SCPI(LogMixin):
try:
result = getf(result)
except:
self._logger.debug(
"Failed to convert result. Details:", exc_info=1
)
log_exception(self, "Failed to convert result.")
results.append((query, result))
return results
......@@ -628,9 +626,9 @@ class SCPI(LogMixin):
context = self._contexts[-1]["commands"].extend(msgs)
return
msg = self.__to_write_commands(*msgs, **kwargs)
self._logger.debug("[start] write %r", msg)
log_debug(self, f"[start] write {msg}")
self.interface.write(msg.encode())
self._logger.debug("[ end ] write %r", msg)
log_debug(self, f"[ end ] write {msg}")
_MAX_ERR_STACK_SIZE = 20
......@@ -659,7 +657,7 @@ class SCPI(LogMixin):
return stack or None
class BaseSCPIDevice(LogMixin):
class BaseSCPIDevice:
"""Base SCPI device class"""
def __init__(self, *args, **kwargs):
......
......@@ -20,7 +20,7 @@ from ..common.greenlet_utils import KillMask
from bliss.common.cleanup import capture_exceptions
from bliss.common import session
from bliss.common.logtools import LogMixin
from bliss.common.logtools import *
import serial
......@@ -152,7 +152,7 @@ class _BaseSerial:
msg = self._data[:eol_pos]
self._data = self._data[eol_pos + len(eol) :]
self._cnt()._logger.debug_data("Rx:", msg)
log_debug_data(self._cnt(), "Rx:", msg)
return msg
def read(self, size, timeout):
......@@ -178,7 +178,7 @@ class _BaseSerial:
break
msg = self._data[:size]
self._data = self._data[size:]
self._cnt()._logger.debug_data("Rx:", msg)
log_debug_data(self._cnt(), "Rx:", msg)
return msg
def write(self, msg, timeout):
......@@ -186,7 +186,7 @@ class _BaseSerial:
return self._write(msg)
def _write(self, msg):
self._cnt()._logger.debug_data("Tx:", msg)
log_debug_data(self._cnt(), "Tx:", msg)
while msg:
_, ready, _ = select.select([], [self.fd], [])
size_send = os.write(self.fd, msg)
......@@ -207,7 +207,7 @@ class _BaseSerial:
msg = self._data
self._data = b""
self._cnt()._logger.debug_data("Rx:", msg)
log_debug_data(self._cnt(), "Rx:", msg)
return msg
@staticmethod
......@@ -710,7 +710,7 @@ class TangoSerial(_BaseSerial):
self._device.DevSerFlush(self.FLUSH_INPUT)
class Serial(LogMixin):
class Serial:
LOCAL, RFC2217, SER2NET, TANGO = list(range(4))
def __init__(
......@@ -762,8 +762,8 @@ class Serial(LogMixin):
def open(self):
if self._raw_handler is None:
serial_type = self._check_type()
self._logger.debug("open - serial_type=%s" % serial_type)
self._logger.debug(self._logger.log_format_dict(self._serial_kwargs))
log_debug(self, "open - serial_type=%s" % serial_type)
log_debug_data(self, "serial kwargs", self._serial_kwargs)
if serial_type == self.RFC2217:
self._raw_handler = RFC2217(self, **self._serial_kwargs)
elif serial_type == self.SER2NET:
......@@ -783,13 +783,13 @@ class Serial(LogMixin):
if self._raw_handler:
self._raw_handler.close()
self._raw_handler = None
self._logger.debug("close")
log_debug(self, "close")
@try_open
def raw_read(self, maxsize=None, timeout=None):
local_timeout = timeout or self._timeout
msg = self._raw_handler.raw_read(maxsize, local_timeout)
self._logger.debug_data("raw_read", msg)
log_debug_data(self, "raw_read", msg)
return msg
def read(self, size=1, timeout=None):
......@@ -800,7 +800,7 @@ class Serial(LogMixin):
def _read(self, size=1, timeout=None):
local_timeout = timeout or self._timeout
msg = self._raw_handler.read(size, local_timeout)
self._logger.debug_data("read", msg)
log_debug_data(self, "read", msg)
if len(msg) != size:
raise SerialError(
"read timeout on serial (%s)" % self._serial_kwargs.get(self._port, "")
......@@ -816,7 +816,7 @@ class Serial(LogMixin):
local_eol = eol or self._eol
local_timeout = timeout or self._timeout
msg = self._raw_handler.readline(local_eol, local_timeout)
self._logger.debug_data("readline", msg)
log_debug_data(self, "readline", msg)
return msg
def write(self, msg, timeout=None):
......@@ -828,7 +828,7 @@ class Serial(LogMixin):
@try_open
def _write(self, msg, timeout=None):
local_timeout = timeout or self._timeout
self._logger.debug_data("write", msg)
log_debug_data(self, "write", msg)
return self._raw_handler.write(msg, local_timeout)
@try_open
......@@ -870,7 +870,7 @@ class Serial(LogMixin):
@try_open
def flush(self):
self._logger.debug("flush")
log_debug(self, "flush")
self._raw_handler.flushInput()
def _check_type(self):
......
......@@ -20,7 +20,7 @@ import weakref
from functools import wraps
from bliss.common import event
from bliss.common import session
from bliss.common.logtools import LogMixin
from bliss.common.logtools import *
from .error import SpecClientNotConnectedError
from .channel import SpecChannel
from .message import *
......@@ -121,8 +121,9 @@ def connectionHandler(conn, socket_to_spec):
try:
reply = conn.registeredReplies[replyID]
except BaseException:
conn._logger.exception(
"Unexpected error while receiving a message from server"
log_exception(
conn,
"Unexpected error while receiving a message from server",
)
else:
del conn.registeredReplies[replyID]
......@@ -145,7 +146,7 @@ def connectionHandler(conn, socket_to_spec):
receivedStrings = [s[offset:]]
class SpecConnection(LogMixin):
class SpecConnection:
"""SpecConnection class
Signals:
connected() -- emitted when the required Spec version gets connected
......@@ -231,9 +232,7 @@ class SpecConnection(LogMixin):
# we received a value, so emit an update signal
channel.update(channelValue, force=True)
except BaseException:
self._logger.exception(
"Uncaught exception in SpecConnection.registerChannel"
)
log_exception(self, "Uncaught exception in SpecConnection.registerChannel")
@try_connect
def unregisterChannel(self, chanName):
......@@ -265,7 +264,7 @@ class SpecConnection(LogMixin):
def error(self, error):
"""Emit the 'error' signal when the remote Spec version signals an error."""
self._logger.error("Error from Spec: %s", error)
log_error(self, f"Error from Spec: {error}")
event.send(self, "error", (error,))
......@@ -278,10 +277,9 @@ class SpecConnection(LogMixin):
old_state = self.state
self.state = CONNECTED
if old_state != CONNECTED:
self._logger.info(
"Connected to %s:%s",
self.host,
(self.scanport and self.scanname) or self.port,
log_info(
self,
f"Connected to {self.host}:{(self.scanport and self.scanname) or self.port}",
)
self.connected_event.set()
......@@ -295,10 +293,9 @@ class SpecConnection(LogMixin):
old_state = self.state
self.state = DISCONNECTED
if old_state == CONNECTED:
self._logger.info(
"Disconnected from %s:%s",
self.host,
(self.scanport and self.scanname) or self.port,
log_info(
self,
f"Disconnected from {self.host}:{(self.scanport and self.scanname) or self.port}",
)
event.send(self, "disconnected")
......@@ -341,7 +338,7 @@ class SpecConnection(LogMixin):
"""
return self.__send_msg_with_reply(
replyCallback=callback,
*msg_cmd_with_return(cmd, version=self.serverVersion)
*msg_cmd_with_return(cmd, version=self.serverVersion),
)
@try_connect
......@@ -352,8 +349,9 @@ class SpecConnection(LogMixin):
cmd -- command string
"""
if self.serverVersion < 3:
self._logger.error(
"Cannot execute command in Spec : feature is available since Spec server v3 only"
log_error(
self,
"Cannot execute command in Spec : feature is available since Spec server v3 only",
)
else:
message = msg_func_with_return(cmd, version=self.serverVersion)
......@@ -374,8 +372,9 @@ class SpecConnection(LogMixin):
cmd -- command string
"""
if self.serverVersion < 3:
self._logger.error(
"Cannot execute command in Spec : feature is available since Spec server v3 only"
log_error(
self,
"Cannot execute command in Spec : feature is available since Spec server v3 only",
)
else:
self.__send_msg_no_reply(msg_func(cmd, version=self.serverVersion))
......
......@@ -25,7 +25,7 @@ from ..common.greenlet_utils import KillMask
from bliss.common.cleanup import error_cleanup, capture_exceptions
from bliss.common import session
from bliss.common.logtools import LogMixin
from bliss.common.logtools import *
class SocketTimeout(CommunicationTimeout):
......@@ -71,7 +71,7 @@ def try_connect_socket(fu):
return rfunc
class BaseSocket(LogMixin):
class BaseSocket:
"""Raw socket class. Provides raw socket access.
Consider using :class:`Tcp`."""
......@@ -112,8 +112,8 @@ class BaseSocket(LogMixin):
curr_host = host or self._host
curr_port = port or self._port
local_timeout = timeout if timeout is not None else self._timeout
self._logger.debug(f"connect to {curr_host}:{curr_port}")
self._logger.debug(f"timeout={local_timeout} ; eol={self._eol}")
log_debug(self, f"connect to {curr_host}:{curr_port}")
log_debug(self, f"timeout={local_timeout} ; eol={self._eol}")
self.close()
......@@ -148,7 +148,7 @@ class BaseSocket(LogMixin):