python/qemu/qmp.py: QMP debug with VM label
PATCH 1/1 python/qemu/qmp.py: QMP debug with VM label
QEMUMachine writes some messages to the default logger. But it sometimes to hard the read the output if we have requested to more than one VM.
This patch adds name in QMP command if it needs and labels with it in debug mode.
--- python/qemu/machine.py | 8 ++++---- python/qemu/qmp.py | 9 ++++++--- 2 files changed, 10 insertions(+), 7 deletions(-)
python/qemu/machine.py +4 -4
@@ -391,7 +391,7 @@ class QEMUMachine(object): self._qmp_set = False self._qmp = None - def qmp(self, cmd, conv_keys=True, **args): + def qmp(self, cmd, conv_keys=True, vm_name=None, **args): """ Invoke a QMP command and return the response dict """
@@ -402,15 +402,15 @@ class QEMUMachine(object): else: qmp_args[key] = value - return self._qmp.cmd(cmd, args=qmp_args) + return self._qmp.cmd(cmd, args=qmp_args, vm_name=vm_name) - def command(self, cmd, conv_keys=True, **args): + def command(self, cmd, conv_keys=True, vm_name=None, **args): """ Invoke a QMP command. On success return the response dict. On failure raise an exception. """ - reply = self.qmp(cmd, conv_keys, **args) + reply = self.qmp(cmd, conv_keys, vm_name, **args) if reply is None: raise qmp.QMPError("Monitor is closed") if "error" in reply:
python/qemu/qmp.py +6 -3
@@ -180,11 +180,12 @@ class QEMUMonitorProtocol: self.__sockfile = self.__sock.makefile() return self.__negotiate_capabilities() - def cmd_obj(self, qmp_cmd): + def cmd_obj(self, qmp_cmd, vm_name=None): """ Send a QMP command to the QMP Monitor. @param qmp_cmd: QMP command to be sent as a Python dict + @param vm_name: name for the virtual machine (string) @return QMP response as a Python dict or None if the connection has been closed """
@@ -196,10 +197,12 @@ class QEMUMonitorProtocol: return None raise err resp = self.__json_read() + if vm_name: + self.logger.debug("<<< {'vm_name' : %s }", vm_name) self.logger.debug("<<< %s", resp) return resp - def cmd(self, name, args=None, cmd_id=None): + def cmd(self, name, args=None, cmd_id=None, vm_name=None): """ Build a QMP command and send it to the QMP Monitor.
@@ -212,7 +215,7 @@ class QEMUMonitorProtocol: qmp_cmd['arguments'] = args if cmd_id: qmp_cmd['id'] = cmd_id - return self.cmd_obj(qmp_cmd) + return self.cmd_obj(qmp_cmd, vm_name) def command(self, cmd, **kwds): """
Signature · 1 line
2.21.1
Quoted 6 lines · Oksana Vohchana
QEMUMachine writes some messages to the default logger. But it sometimes to hard the read the output if we have requested to more than one VM.
This patch adds name in QMP command if it needs and labels with it in debug mode.
Hiya!
I like the end result, but I don't like the methodology of pushing higher-level details into a lower-level library.
Quoted 11 lines
Signed-off-by Oksana Vohchana <[email protected]>--- python/qemu/machine.py | 8 ++++---- python/qemu/qmp.py | 9 ++++++--- 2 files changed, 10 insertions(+), 7 deletions(-)python/qemu/machine.py +0
-0
python/qemu/machine.py class QEMUMachine(object): · line 394
Quoted 5 lines
python/qemu/machine.py +1
-1@@ -391,7 +391,7 @@ class QEMUMachine(object): self._qmp_set = False self._qmp = None - def qmp(self, cmd, conv_keys=True, **args): + def qmp(self, cmd, conv_keys=True, vm_name=None, **args):
in machine.py, we should already have access to self._name -- the name of the machine. Let's use that.
python/qemu/machine.py class QEMUMachine(object): · line 406
Quoted 10 lines
python/qemu/machine.py +1
-1""" Invoke a QMP command and return the response dict """@@ -402,15 +402,15 @@ class QEMUMachine(object): else: qmp_args[key] = value - return self._qmp.cmd(cmd, args=qmp_args) + return self._qmp.cmd(cmd, args=qmp_args, vm_name=vm_name)
Adding a name per-each call to QMP is a bit much. Let's consolidate it and set it *exactly once*.
A fine place to do that would be QMP's __init__ method:
(in machine.py:)
self._qmp = qmp.QEMUMonitorProtocol(self._vm_monitor, server=True, remote_name=self._name)
Then, in QMP's init, you can do something like:
def __init__(self, address, server=False, nickname=None):
self.nickname = nickname
... and then on subsequent logging calls, you can use the nickname of the connection to print better logging messages.
Some other notes:
1. QEMUMonitorProtocol uses a class variable logger instead of an instance variable logger. If this was made per-instance, you could change the logger of any given QMP object as-desired from the caller.
2. I'd rename the default QMP logger to be 'qemu.QMP' instead of 'QMP' to respect the hierarchical logging namespace.
3. If a caller set qmp.logger = logging.getLogger('qemu.QMP.mynamehere') then all messages printed by this QMP instance would use the mynamehere prefix by default for all messages it printed. This might be enough to get the behavior you want.
(Also, it would be very powerful for many other reasons, well beyond what you're asking for here, to allow callers to change how QMP logs, where, and with what messages.)
There's probably a lot of ways to do it; but I'd pick one where we don't have to pass names around for every call.
--js
Quoted 54 lines
python/qemu/machine.py +2
-2- def command(self, cmd, conv_keys=True, **args): + def command(self, cmd, conv_keys=True, vm_name=None, **args): """ Invoke a QMP command. On success return the response dict. On failure raise an exception. """ - reply = self.qmp(cmd, conv_keys, **args) + reply = self.qmp(cmd, conv_keys, vm_name, **args) if reply is None: raise qmp.QMPError("Monitor is closed") if "error" in reply:python/qemu/qmp.py +6
-3@@ -180,11 +180,12 @@ class QEMUMonitorProtocol: self.__sockfile = self.__sock.makefile() return self.__negotiate_capabilities() - def cmd_obj(self, qmp_cmd): + def cmd_obj(self, qmp_cmd, vm_name=None): """ Send a QMP command to the QMP Monitor. @param qmp_cmd: QMP command to be sent as a Python dict + @param vm_name: name for the virtual machine (string) @return QMP response as a Python dict or None if the connection has been closed """@@ -196,10 +197,12 @@ class QEMUMonitorProtocol: return None raise err resp = self.__json_read() + if vm_name: + self.logger.debug("<<< {'vm_name' : %s }", vm_name) self.logger.debug("<<< %s", resp) return resp - def cmd(self, name, args=None, cmd_id=None): + def cmd(self, name, args=None, cmd_id=None, vm_name=None): """ Build a QMP command and send it to the QMP Monitor.@@ -212,7 +215,7 @@ class QEMUMonitorProtocol: qmp_cmd['arguments'] = args if cmd_id: qmp_cmd['id'] = cmd_id - return self.cmd_obj(qmp_cmd) + return self.cmd_obj(qmp_cmd, vm_name) def command(self, cmd, **kwds): """
Hi John
Your approach to using logger hierarchy it's actually what I need. Thanks for the review.
The second version of the patch I will send in a few mins
Quoted 148 lines · John Snow
Quoted 6 lines · Oksana Vohchana
QEMUMachine writes some messages to the default logger. But it sometimes to hard the read the output if we have requested to more than one VM.
This patch adds name in QMP command if it needs and labels with it in debug mode.Hiya!
I like the end result, but I don't like the methodology of pushing higher-level details into a lower-level library.
Quoted 11 lines
Signed-off-by Oksana Vohchana <[email protected]>--- python/qemu/machine.py | 8 ++++---- python/qemu/qmp.py | 9 ++++++--- 2 files changed, 10 insertions(+), 7 deletions(-)python/qemu/machine.py +0
-0python/qemu/machine.py class QEMUMachine(object): · line 394
Quoted 5 lines
python/qemu/machine.py +1
-1@@ -391,7 +391,7 @@ class QEMUMachine(object): self._qmp_set = False self._qmp = None - def qmp(self, cmd, conv_keys=True, **args): + def qmp(self, cmd, conv_keys=True, vm_name=None, **args):in
machine.py, we should already have access to self._name -- the name of the machine. Let's use that.python/qemu/machine.py class QEMUMachine(object): · line 406
Quoted 10 lines
python/qemu/machine.py +1
-1""" Invoke a QMP command and return the response dict """@@ -402,15 +402,15 @@ class QEMUMachine(object): else: qmp_args[key] = value - return self._qmp.cmd(cmd, args=qmp_args) + return self._qmp.cmd(cmd, args=qmp_args, vm_name=vm_name)Adding a name per-each call to QMP is a bit much. Let's consolidate it and set it *exactly once*.
A fine place to do that would be QMP's
__init__method:(in
machine.py:)self._qmp =
qmp.QEMUMonitorProtocol(self._vm_monitor, server=True, remote_name=self._name)Then, in QMP's init, you can do something like:
def
__init__(self, address, server=False, nickname=None):
self.nickname = nickname... and then on subsequent logging calls, you can use the nickname of the connection to print better logging messages.
Some other notes:
1. QEMUMonitorProtocol uses a class variable
loggerinstead of an instance variable logger. If this was made per-instance, you could change the logger of any given QMP object as-desired from the caller.2. I'd rename the default QMP logger to be 'qemu.QMP' instead of 'QMP' to respect the hierarchical logging namespace.
3. If a caller set qmp.logger =
logging.getLogger('qemu.QMP.mynamehere')then all messages printed by this QMP instance would use themynamehereprefix by default for all messages it printed. This might be enough to get the behavior you want.(Also, it would be very powerful for many other reasons, well beyond what you're asking for here, to allow callers to change how QMP logs, where, and with what messages.)
There's probably a lot of ways to do it; but I'd pick one where we don't have to pass names around for every call.
--js
Quoted 18 lines
python/qemu/machine.py +2
-2- def command(self, cmd, conv_keys=True, **args): + def command(self, cmd, conv_keys=True, vm_name=None, **args): """ Invoke a QMP command. On success return the response dict. On failure raise an exception. """ - reply = self.qmp(cmd, conv_keys, **args) + reply = self.qmp(cmd, conv_keys, vm_name, **args) if reply is None: raise qmp.QMPError("Monitor is closed") if "error" in reply:python/qemu/qmp.py +0
-0@@ -180,11 +180,12 @@ class QEMUMonitorProtocol: self.__sockfile = self.__sock.makefile()python/qemu/qmp.py class QEMUMonitorProtocol: · line 189
Quoted 10 lines
python/qemu/qmp.py +2
-1return self.__negotiate_capabilities() - def cmd_obj(self, qmp_cmd): + def cmd_obj(self, qmp_cmd, vm_name=None): """ Send a QMP command to the QMP Monitor. @param qmp_cmd: QMP command to be sent as a Python dict + @param vm_name: name for the virtual machine (string) @return QMP response as a Python dict or None if the connectionhas
Quoted 26 lines
python/qemu/qmp.py +4
-2been closed """@@ -196,10 +197,12 @@ class QEMUMonitorProtocol: return None raise err resp = self.__json_read() + if vm_name: + self.logger.debug("<<< {'vm_name' : %s }", vm_name) self.logger.debug("<<< %s", resp) return resp - def cmd(self, name, args=None, cmd_id=None): + def cmd(self, name, args=None, cmd_id=None, vm_name=None): """ Build a QMP command and send it to the QMP Monitor.@@ -212,7 +215,7 @@ class QEMUMonitorProtocol: qmp_cmd['arguments'] = args if cmd_id: qmp_cmd['id'] = cmd_id - return self.cmd_obj(qmp_cmd) + return self.cmd_obj(qmp_cmd, vm_name) def command(self, cmd, **kwds): """