diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/logger.py | 43 | ||||
-rw-r--r-- | src/room.py | 10 |
2 files changed, 40 insertions, 13 deletions
diff --git a/src/logger.py b/src/logger.py index 102d4ee1..991187a0 100644 --- a/src/logger.py +++ b/src/logger.py @@ -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(): + 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': - return - dir = DATA_HOME+'logs/' + return None + directory = os.path.join(DATA_HOME, 'logs') try: - makedirs(dir) + makedirs(directory) except OSError: pass try: - 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] + else: + fd = self.check_and_create_log_dir(room) + if not fd: return - msg = msg if nick: fd.write(datetime.now().strftime('%d-%m-%y [%H:%M:%S] ')+nick+': '+msg+'\n') else: fd.write(datetime.now().strftime('%d-%m-%y [%H:%M:%S] ')+'* '+msg+'\n') - fd.close() + fd.flush() # TODO do something better here? logger = Logger() diff --git a/src/room.py b/src/room.py index f263aef5..b1b77d1e 100644 --- a/src/room.py +++ b/src/room.py @@ -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): TextBuffer.__init__(self) @@ -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(self.name, nickname, txt) + if time is None and self.joined: # don't log the history messages + logger.groupchat(self.name, 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 |