diff options
author | louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13> | 2011-01-02 16:58:40 +0000 |
---|---|---|
committer | louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13> | 2011-01-02 16:58:40 +0000 |
commit | ab85a3dc219ac3b64d7294deb52847c093a2a516 (patch) | |
tree | a2ca2db2a30473cc65b883796bd3cc37c5c26fd5 /src/logger.py | |
parent | c29cc8b15facd52aa6f202402565e07b2477146b (diff) | |
download | poezio-ab85a3dc219ac3b64d7294deb52847c093a2a516.tar.gz poezio-ab85a3dc219ac3b64d7294deb52847c093a2a516.tar.bz2 poezio-ab85a3dc219ac3b64d7294deb52847c093a2a516.tar.xz poezio-ab85a3dc219ac3b64d7294deb52847c093a2a516.zip |
fix the log of MUC messages in files
Diffstat (limited to 'src/logger.py')
-rw-r--r-- | src/logger.py | 43 |
1 files changed, 33 insertions, 10 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() |