comparison libervia/backend/plugins/plugin_misc_xmllog.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_misc_xmllog.py@524856bd7b19
children
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SàT plugin for managing raw XML log
5 # Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from libervia.backend.core.i18n import _
21 from libervia.backend.core.constants import Const as C
22 from libervia.backend.core.log import getLogger
23 from twisted.words.xish import domish
24 from functools import partial
25
26 log = getLogger(__name__)
27
28 PLUGIN_INFO = {
29 C.PI_NAME: "Raw XML log Plugin",
30 C.PI_IMPORT_NAME: "XmlLog",
31 C.PI_TYPE: "Misc",
32 C.PI_PROTOCOLS: [],
33 C.PI_DEPENDENCIES: [],
34 C.PI_MAIN: "XmlLog",
35 C.PI_HANDLER: "no",
36 C.PI_DESCRIPTION: _("""Send raw XML logs to bridge"""),
37 }
38
39
40 class XmlLog(object):
41
42 params = """
43 <params>
44 <general>
45 <category name="Debug">
46 <param name="Xml log" label="%(label_xmllog)s" value="false" type="bool" />
47 </category>
48 </general>
49 </params>
50 """ % {
51 "label_xmllog": _("Activate XML log")
52 }
53
54 def __init__(self, host):
55 log.info(_("Plugin XML Log initialization"))
56 self.host = host
57 host.memory.update_params(self.params)
58 host.bridge.add_signal(
59 "xml_log", ".plugin", signature="sss"
60 ) # args: direction("IN" or "OUT"), xml_data, profile
61
62 host.trigger.add("stream_hooks", self.add_hooks)
63
64 def add_hooks(self, client, receive_hooks, send_hooks):
65 self.do_log = self.host.memory.param_get_a("Xml log", "Debug")
66 if self.do_log:
67 receive_hooks.append(partial(self.on_receive, client=client))
68 send_hooks.append(partial(self.on_send, client=client))
69 log.info(_("XML log activated"))
70 return True
71
72 def on_receive(self, element, client):
73 self.host.bridge.xml_log("IN", element.toXml(), client.profile)
74
75 def on_send(self, obj, client):
76 if isinstance(obj, str):
77 xml_log = obj
78 elif isinstance(obj, domish.Element):
79 xml_log = obj.toXml()
80 else:
81 log.error(_("INTERNAL ERROR: Unmanaged XML type"))
82 self.host.bridge.xml_log("OUT", xml_log, client.profile)