Mercurial > libervia-backend
changeset 2440:51d346e283fd
plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 18 Nov 2017 18:49:57 +0100 |
parents | 6908fe4c6eca |
children | e86dc8cb4345 |
files | src/plugins/plugin_misc_xmllog.py |
diffstat | 1 files changed, 33 insertions(+), 29 deletions(-) [+] |
line wrap: on
line diff
--- a/src/plugins/plugin_misc_xmllog.py Sat Nov 18 18:37:21 2017 +0100 +++ b/src/plugins/plugin_misc_xmllog.py Sat Nov 18 18:49:57 2017 +0100 @@ -21,8 +21,8 @@ from sat.core.constants import Const as C from sat.core.log import getLogger log = getLogger(__name__) -from twisted.words.protocols.jabber.xmlstream import XmlStream from twisted.words.xish import domish +from twisted.words.xish import xmlstream PLUGIN_INFO = { C.PI_NAME: "Raw XML log Plugin", @@ -32,28 +32,27 @@ C.PI_DEPENDENCIES: [], C.PI_MAIN: "XmlLog", C.PI_HANDLER: "no", - C.PI_DESCRIPTION: _("""Send raw XML logs to bridge""") + C.PI_DESCRIPTION: _(u"""Send raw XML logs to bridge""") } +host = None + +def send(self, obj): + global host + if isinstance(obj, basestring): + log = unicode(obj) + elif isinstance(obj, domish.Element): + log = obj.toXml() + else: + log.error(_(u'INTERNAL ERROR: Unmanaged XML type')) + host.bridge.xmlLog("OUT", log, self._profile) + return self._original_send(obj) + -class LoggingXmlStream(XmlStream): - """This class send the raw XML to the Bridge, for logging purpose""" - - def send(self, obj): - if isinstance(obj, basestring): - log = unicode(obj) - elif isinstance(obj, domish.Element): - log = obj.toXml() - else: - log.error(_('INTERNAL ERROR: Unmanaged XML type')) - self._host.bridge.xmlLog("OUT", log, self._profile) - return XmlStream.send(self, obj) - - def dataReceived(self, data): - # FIXME: we use "replace" here because a big stanza can be cut in the middle of a unicode char - # this plugin should probably moved to a better place, where stanzas are fully received - self._host.bridge.xmlLog("IN", data.decode('utf-8', 'replace'), self._profile) - return XmlStream.dataReceived(self, data) +def onElement(self, element): + global host + host.bridge.xmlLog("IN", element.toXml(), self._profile) + return self._original_onElement(element) class XmlLog(object): @@ -68,9 +67,10 @@ </params> """ % {"label_xmllog": _("Activate XML log")} - def __init__(self, host): + def __init__(self, host_): log.info(_("Plugin XML Log initialization")) - self.host = host + global host + host = host_ #parameters host.memory.updateParams(self.params) @@ -78,13 +78,17 @@ #bridge host.bridge.addSignal("xmlLog", ".plugin", signature='sss') # args: direction("IN" or "OUT"), xml_data, profile - do_log = self.host.memory.getParamA("Xml log", "Debug") - if do_log: - log.info(_("XML log activated")) - host.trigger.add("XML Initialized", self.logXml) + self.do_log = host.memory.getParamA("Xml log", "Debug") + if self.do_log: + XmlStream = xmlstream.XmlStream + XmlStream._original_send = XmlStream.send + XmlStream._original_onElement = XmlStream.onElement + XmlStream.send = send + XmlStream.onElement = onElement + XmlStream._profile = '' + host.trigger.add("XML Initialized", self.setProfile) + log.info(_(u"XML log activated")) - def logXml(self, xmlstream, profile): - xmlstream.__class__ = LoggingXmlStream + def setProfile(self, xmlstream, profile): xmlstream._profile = profile - xmlstream._host = self.host return True