fix the log of MUC messages in files

parent c29cc8b1
......@@ -20,36 +20,59 @@ import os
from datetime import datetime
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):
Appends things to files. Error/information/warning logs
and also log the conversations to logfiles
def __init__(self):# , logfile, loglevel):
def __init__(self):
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():
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':
dir = DATA_HOME+'logs/'
return None
directory = os.path.join(DATA_HOME, 'logs')
except OSError:
fd = open(dir+room, 'a')
fd = open(os.path.join(directory, room), 'a')
self.groupchat_fds[room] = fd
return fd
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]
fd = self.check_and_create_log_dir(room)
if not fd:
msg = msg
if nick:
fd.write('%d-%m-%y [%H:%M:%S] ')+nick+': '+msg+'\n')
fd.write('%d-%m-%y [%H:%M:%S] ')+'* '+msg+'\n')
fd.flush() # TODO do something better here?
logger = Logger()
......@@ -24,6 +24,10 @@ from message import Message
import common
import theme
import logging
log = logging.getLogger(__name__)
class Room(TextBuffer):
def __init__(self, name, nick):
......@@ -46,8 +50,8 @@ class Room(TextBuffer):
Log the messages in the archives, if it needs
to be
if time == None and self.joined: # don't log the history messages
logger.message(, nickname, txt)
if time is None and self.joined: # don't log the history messages
logger.groupchat(, nickname, txt)
def do_highlight(self, txt, time, nickname):
......@@ -94,7 +98,7 @@ class Room(TextBuffer):
when we receive an history message said by someone who is not
in the room anymore
# self.log_message(txt, time, nickname)
self.log_message(txt, time, nickname)
if txt.startswith('/me '):
txt = "* " + nickname + ' ' + txt[4:]
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