~ fixed logging usage : should use the module's logger rather than the root one

This commit is contained in:
nicobo 2021-01-25 23:32:38 +01:00
parent 560d838e60
commit efa036b265
No known key found for this signature in database
GPG key ID: 2581E71C5FA5285F

View file

@ -18,6 +18,8 @@ from omemo.exceptions import MissingBundleException
from .chatter import Chatter from .chatter import Chatter
from .helpers import * from .helpers import *
log = logging.getLogger(__name__)
class SliXmppClient(ClientXMPP): class SliXmppClient(ClientXMPP):
@ -57,7 +59,7 @@ class SliXmppClient(ClientXMPP):
module=slixmpp_omemo, module=slixmpp_omemo,
) # OMEMO ) # OMEMO
except (PluginCouldNotLoad,): except (PluginCouldNotLoad,):
logging.exception('And error occured when loading the omemo plugin.') log.exception('And error occured when loading the omemo plugin.')
sys.exit(1) sys.exit(1)
self.message_handler = message_handler self.message_handler = message_handler
@ -73,11 +75,11 @@ class SliXmppClient(ClientXMPP):
# try: # try:
# self.get_roster() # self.get_roster()
# except IqError as err: # except IqError as err:
# logging.error('There was an error getting the roster') # log.error('There was an error getting the roster')
# logging.error(err.iq['error']['condition']) # log.error(err.iq['error']['condition'])
# self.disconnect() # self.disconnect()
# except IqTimeout: # except IqTimeout:
# logging.error('Server is taking too long to respond') # log.error('Server is taking too long to respond')
# self.disconnect() # self.disconnect()
@ -113,15 +115,15 @@ class SliXmppClient(ClientXMPP):
# <body>My message</body> # <body>My message</body>
# </message> # </message>
logging.debug("XMPP message received : %r",msg) log.debug("XMPP message received : %r",msg)
# TODO ? with xmppy I used to allow the following types : ["message","chat","normal",None] # TODO ? with xmppy I used to allow the following types : ["message","chat","normal",None]
if msg['type'] not in ('chat', 'normal'): if msg['type'] not in ('chat', 'normal'):
logging.debug("Discarding message of type %r",msg['type']) log.debug("Discarding message of type %r",msg['type'])
return None return None
if not self['xep_0384'].is_encrypted(msg): if not self['xep_0384'].is_encrypted(msg):
logging.debug('This message was not encrypted') log.debug('This message was not encrypted')
self.message_handler(msg,msg['body']) self.message_handler(msg,msg['body'])
return None return None
@ -135,7 +137,7 @@ class SliXmppClient(ClientXMPP):
except (MissingOwnKey,): except (MissingOwnKey,):
# The message is missing our own key, it was not encrypted for # The message is missing our own key, it was not encrypted for
# us, and we can't decrypt it. # us, and we can't decrypt it.
logging.exception('I can\'t decrypt this message as it is not encrypted for me : %r',msg) log.exception('I can\'t decrypt this message as it is not encrypted for me : %r',msg)
return None return None
except (NoAvailableSession,): except (NoAvailableSession,):
# We received a message from that contained a session that we # We received a message from that contained a session that we
@ -144,7 +146,7 @@ class SliXmppClient(ClientXMPP):
# Here, as we need to initiate a new encrypted session, it is # Here, as we need to initiate a new encrypted session, it is
# best if we send an encrypted message directly. XXX: Is it # best if we send an encrypted message directly. XXX: Is it
# where we talk about self-healing messages? # where we talk about self-healing messages?
logging.exception('I can\'t decrypt this message as it uses an encrypted session I don\'t know about : %r',msg) log.exception('I can\'t decrypt this message as it uses an encrypted session I don\'t know about : %r',msg)
return None return None
except (UndecidedException, UntrustedException) as exn: except (UndecidedException, UntrustedException) as exn:
# We received a message from an untrusted device. We can # We received a message from an untrusted device. We can
@ -155,7 +157,7 @@ class SliXmppClient(ClientXMPP):
# or not. Clients _should_ indicate that the message was not # or not. Clients _should_ indicate that the message was not
# trusted, or in undecided state, if they decide to decrypt it # trusted, or in undecided state, if they decide to decrypt it
# anyway. # anyway.
logging.exception("Your device '%s' is not in my trusted devices.", exn.device) log.exception("Your device '%s' is not in my trusted devices.", exn.device)
# We resend, setting the `allow_untrusted` parameter to True. # We resend, setting the `allow_untrusted` parameter to True.
await self.message(msg, allow_untrusted=True) await self.message(msg, allow_untrusted=True)
return None return None
@ -163,10 +165,10 @@ class SliXmppClient(ClientXMPP):
# Slixmpp tried its best, but there were errors it couldn't # Slixmpp tried its best, but there were errors it couldn't
# resolve. At this point you should have seen other exceptions # resolve. At this point you should have seen other exceptions
# and given a chance to resolve them already. # and given a chance to resolve them already.
logging.exception('I was not able to decrypt the message : %r',msg) log.exception('I was not able to decrypt the message : %r',msg)
return None return None
except (Exception,): except (Exception,):
logging.exception('An error occured while attempting decryption') log.exception('An error occured while attempting decryption')
raise raise
return None return None
@ -237,15 +239,15 @@ class SliXmppClient(ClientXMPP):
# device won't be able to decrypt and should display a # device won't be able to decrypt and should display a
# generic message. The receiving end-user at this # generic message. The receiving end-user at this
# point can bring up the issue if it happens. # point can bring up the issue if it happens.
logging.warning('Could not find keys for device "%d" of recipient "%s". Skipping.', error.device, error.bare_jid) log.warning('Could not find keys for device "%d" of recipient "%s". Skipping.', error.device, error.bare_jid)
jid = JID(error.bare_jid) jid = JID(error.bare_jid)
device_list = expect_problems.setdefault(jid, []) device_list = expect_problems.setdefault(jid, [])
device_list.append(error.device) device_list.append(error.device)
except (IqError, IqTimeout) as exn: except (IqError, IqTimeout) as exn:
logging.exception('An error occured while fetching information on %r', recipient) log.exception('An error occured while fetching information on %r', recipient)
return None return None
except Exception as exn: except Exception as exn:
logging.exception('An error occured while attempting to encrypt to %r', recipient) log.exception('An error occured while attempting to encrypt to %r', recipient)
raise raise
return None return None
@ -254,7 +256,7 @@ class SliXmppClient(ClientXMPP):
async def encrypted_reply(self, original_msg, body): async def encrypted_reply(self, original_msg, body):
"""Helper to reply with encrypted messages""" """Helper to reply with encrypted messages"""
logging.debug("Replying to %r",original_msg) log.debug("Replying to %r",original_msg)
return self.encrypted_send( body, recipient=original_msg['from'], type=original_msg['type'] ) return self.encrypted_send( body, recipient=original_msg['from'], type=original_msg['type'] )
@ -280,13 +282,13 @@ class JabberChatter(Chatter):
decrypted_body: either the given body if it was plain text or the OMEMO-decrypted one (always a string) decrypted_body: either the given body if it was plain text or the OMEMO-decrypted one (always a string)
""" """
logging.log(TRACE,"<<< %r",original_message) log.log(TRACE,"<<< %r",original_message)
logging.debug("<<< %r",decrypted_body) log.debug("<<< %r",decrypted_body)
self.bot.onMessage(decrypted_body) self.bot.onMessage(decrypted_body)
def connect(self): def connect(self):
logging.debug("Connecting...") log.debug("Connecting...")
# Connects and waits for the connection to be established # Connects and waits for the connection to be established
# See https://slixmpp.readthedocs.io/using_asyncio.html # See https://slixmpp.readthedocs.io/using_asyncio.html
self.xmpp.connected_event = asyncio.Event() self.xmpp.connected_event = asyncio.Event()
@ -296,7 +298,7 @@ class JabberChatter(Chatter):
# TODO use asyncio.run() in latest python # TODO use asyncio.run() in latest python
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.run_until_complete(self.xmpp.connected_event.wait()) loop.run_until_complete(self.xmpp.connected_event.wait())
logging.debug("Connected.") log.debug("Connected.")
def start( self, bot ): def start( self, bot ):
""" """
@ -313,7 +315,7 @@ class JabberChatter(Chatter):
""" """
Sends the given message using the underlying implemented chat protocol Sends the given message using the underlying implemented chat protocol
""" """
logging.debug(">>> %s",message) log.debug(">>> %s",message)
# TODO use asyncio.make_task() in latest python # TODO use asyncio.make_task() in latest python
asyncio.ensure_future( self.xmpp.encrypted_send( body=message, recipient=self.recipient ) ) asyncio.ensure_future( self.xmpp.encrypted_send( body=message, recipient=self.recipient ) )