Simplify log_message()

- Pass EventObj instead of variables
- decrease indent level
- check all conditions inside log_message() not before calling
- fixed bug where gc private messages where not logged with the correct jid
This commit is contained in:
Philipp Hörist 2017-04-08 20:10:57 +02:00
parent 8a789763a2
commit 54eb1cbbb8
2 changed files with 39 additions and 45 deletions

View file

@ -507,37 +507,42 @@ class CommonConnection:
gajim.nec.push_incoming_event( gajim.nec.push_incoming_event(
StanzaMessageOutgoingEvent(None, **vars(obj))) StanzaMessageOutgoingEvent(None, **vars(obj)))
def log_message(self, jid, msg, forward_from, session, original_message, def log_message(self, obj):
subject, type_, xhtml=None, additional_data=None): if not obj.is_loggable:
if additional_data is None: return
additional_data = {}
if not forward_from and session and session.is_loggable(): if obj.forward_from or not obj.session or not obj.session.is_loggable():
ji = gajim.get_jid_without_resource(jid) return
if gajim.config.should_log(self.name, ji):
log_msg = msg if not gajim.config.should_log(self.name, obj.jid):
if original_message is not None: return
log_msg = original_message
if log_msg: if obj.xhtml and gajim.config.get('log_xhtml_messages'):
if type_ == 'chat': message = '<body xmlns="%s">%s</body>' % (nbxmpp.NS_XHTML,
kind = 'chat_msg_sent' obj.xhtml)
else: else:
kind = 'single_msg_sent' message = obj.original_message or obj.message
try: if not message:
if xhtml and gajim.config.get('log_xhtml_messages'): return
log_msg = '<body xmlns="%s">%s</body>' % (
nbxmpp.NS_XHTML, xhtml) if obj.type_ == 'chat':
gajim.logger.write(kind, jid, log_msg, subject=subject, additional_data=additional_data) kind = 'chat_msg_sent'
except exceptions.PysqliteOperationalError as e: else:
self.dispatch('DB_ERROR', (_('Disk Write Error'), kind = 'single_msg_sent'
str(e))) try:
except exceptions.DatabaseMalformed: gajim.logger.write(
pritext = _('Database Error') kind, obj.jid, message, subject=obj.subject,
sectext = _('The database file (%s) cannot be read. Try' additional_data=obj.additional_data)
' to repair it (see ' except exceptions.PysqliteOperationalError as error:
'http://trac.gajim.org/wiki/DatabaseBackup)' self.dispatch('DB_ERROR', (_('Disk Write Error'), str(error)))
' or remove it (all history will be lost).') % \ except exceptions.DatabaseMalformed:
common.logger.LOG_DB_PATH pritext = _('Database Error')
self.dispatch('DB_ERROR', (pritext, sectext)) sectext = _('The database file (%s) cannot be read. Try'
' to repair it (see '
'https://dev.gajim.org/gajim/gajim/wikis/help/DatabaseBackup)'
' or remove it (all history will be lost).') % \
common.logger.LOG_DB_PATH
self.dispatch('DB_ERROR', (pritext, sectext))
def ack_subscribed(self, jid): def ack_subscribed(self, jid):
""" """
@ -2136,21 +2141,13 @@ class Connection(CommonConnection, ConnectionHandlers):
if obj.callback: if obj.callback:
obj.callback(obj, obj.msg_iq, *obj.callback_args) obj.callback(obj, obj.msg_iq, *obj.callback_args)
if not obj.is_loggable:
return
if isinstance(obj.jid, list): if isinstance(obj.jid, list):
for j in obj.jid: for j in obj.jid:
if obj.session is None: if obj.session is None:
obj.session = self.get_or_create_session(j, '') obj.session = self.get_or_create_session(j, '')
self.log_message( self.log_message(obj)
j, obj.message, obj.forward_from, obj.session,
obj.original_message, obj.subject, obj.type_, obj.xhtml,
obj.additional_data)
else: else:
self.log_message( self.log_message(obj)
obj.jid, obj.message, obj.forward_from, obj.session,
obj.original_message, obj.subject, obj.type_, obj.xhtml,
obj.additional_data)
def send_contacts(self, contacts, fjid, type_='message'): def send_contacts(self, contacts, fjid, type_='message'):
""" """

View file

@ -342,10 +342,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
if obj.callback: if obj.callback:
obj.callback(obj.msg_iq, *obj.callback_args) obj.callback(obj.msg_iq, *obj.callback_args)
if not obj.is_loggable: self.log_message(obj)
return
self.log_message(obj.jid, obj.message, obj.forward_from,
obj.session, obj.original_message, obj.subject, obj.type_)
def on_send_not_ok(reason): def on_send_not_ok(reason):
reason += ' ' + _('Your message could not be sent.') reason += ' ' + _('Your message could not be sent.')