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)

This commit is contained in:
Goffi 2017-11-18 18:49:57 +01:00
commit 3042d42bc4

View file

@ -21,8 +21,8 @@ from sat.core.i18n import _
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 @@ PLUGIN_INFO = {
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
class LoggingXmlStream(XmlStream):
"""This class send the raw XML to the Bridge, for logging purpose"""
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)
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 @@ class XmlLog(object):
</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 @@ class XmlLog(object):
#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