fix the log of MUC messages in files

parent c29cc8b1
...@@ -20,36 +20,59 @@ import os ...@@ -20,36 +20,59 @@ import os
from datetime import datetime from datetime import datetime
from config import config from config import config
DATA_HOME = config.get('log_dir', os.path.join(environ.get('XDG_DATA_HOME') or os.path.join(environ.get('HOME'), '.local', 'share'), 'poezio')) import logging
log = logging.getLogger(__name__)
DATA_HOME = config.get('log_dir', '') or os.path.join(environ.get('XDG_DATA_HOME') or os.path.join(environ.get('HOME'), '.local', 'share'), 'poezio')
class Logger(object): class Logger(object):
""" """
Appends things to files. Error/information/warning logs Appends things to files. Error/information/warning logs
and also log the conversations to logfiles and also log the conversations to logfiles
""" """
def __init__(self):# , logfile, loglevel): def __init__(self):
self.logfile = config.get('logfile', 'logs') self.logfile = config.get('logfile', 'logs')
# a dict of 'groupchatname': file-object (opened)
self.groupchat_fds = dict()
def groupchat(self, room, nick, msg): def __del__(self):
for opened_file in self.groupchat_fds.values():
opened_file.close()
def check_and_create_log_dir(self, room):
""" """
log the message in the appropriate room's file Check that the directory where we want to log the messages
exists. if not, create it
""" """
if config.get('use_log', 'false') == 'false': if config.get('use_log', 'false') == 'false':
return return None
dir = DATA_HOME+'logs/' directory = os.path.join(DATA_HOME, 'logs')
try: try:
makedirs(dir) makedirs(directory)
except OSError: except OSError:
pass pass
try: try:
fd = open(dir+room, 'a') fd = open(os.path.join(directory, room), 'a')
self.groupchat_fds[room] = fd
return fd
except IOError: except IOError:
return None
def groupchat(self, room, nick, msg):
"""
log the message in the appropriate room's file
"""
if room in self.groupchat_fds.keys():
fd = self.groupchat_fds[room]
else:
fd = self.check_and_create_log_dir(room)
if not fd:
return return
msg = msg
if nick: if nick:
fd.write(datetime.now().strftime('%d-%m-%y [%H:%M:%S] ')+nick+': '+msg+'\n') fd.write(datetime.now().strftime('%d-%m-%y [%H:%M:%S] ')+nick+': '+msg+'\n')
else: else:
fd.write(datetime.now().strftime('%d-%m-%y [%H:%M:%S] ')+'* '+msg+'\n') fd.write(datetime.now().strftime('%d-%m-%y [%H:%M:%S] ')+'* '+msg+'\n')
fd.close() fd.flush() # TODO do something better here?
logger = Logger() logger = Logger()
...@@ -24,6 +24,10 @@ from message import Message ...@@ -24,6 +24,10 @@ from message import Message
import common import common
import theme import theme
import logging
log = logging.getLogger(__name__)
class Room(TextBuffer): class Room(TextBuffer):
def __init__(self, name, nick): def __init__(self, name, nick):
TextBuffer.__init__(self) TextBuffer.__init__(self)
...@@ -46,8 +50,8 @@ class Room(TextBuffer): ...@@ -46,8 +50,8 @@ class Room(TextBuffer):
Log the messages in the archives, if it needs Log the messages in the archives, if it needs
to be to be
""" """
if time == None and self.joined: # don't log the history messages if time is None and self.joined: # don't log the history messages
logger.message(self.name, nickname, txt) logger.groupchat(self.name, nickname, txt)
def do_highlight(self, txt, time, nickname): def do_highlight(self, txt, time, nickname):
""" """
...@@ -94,7 +98,7 @@ class Room(TextBuffer): ...@@ -94,7 +98,7 @@ class Room(TextBuffer):
when we receive an history message said by someone who is not when we receive an history message said by someone who is not
in the room anymore in the room anymore
""" """
# self.log_message(txt, time, nickname) self.log_message(txt, time, nickname)
if txt.startswith('/me '): if txt.startswith('/me '):
txt = "* " + nickname + ' ' + txt[4:] txt = "* " + nickname + ' ' + txt[4:]
nickname = None nickname = None
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment