2005-05-12 15:43:17 +02:00
|
|
|
## logger.py
|
2003-11-30 17:02:00 +01:00
|
|
|
##
|
|
|
|
## Gajim Team:
|
2005-05-12 15:43:17 +02:00
|
|
|
## - Yann Le Boulanger <asterix@lagaule.org>
|
|
|
|
## - Vincent Hanquez <tab@snarc.org>
|
2005-05-20 02:05:18 +02:00
|
|
|
## - Nikos Kouremenos <kourem@gmail.com>
|
2003-11-30 17:02:00 +01:00
|
|
|
##
|
2005-01-07 22:52:38 +01:00
|
|
|
## Copyright (C) 2003-2005 Gajim Team
|
2003-11-30 17:02:00 +01:00
|
|
|
##
|
|
|
|
## This program is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 2 only.
|
|
|
|
##
|
|
|
|
## This program is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
|
|
|
|
import os
|
2005-05-12 15:43:17 +02:00
|
|
|
import sys
|
2003-11-30 17:02:00 +01:00
|
|
|
import time
|
2005-04-16 00:02:13 +02:00
|
|
|
import common.gajim
|
2004-05-17 01:47:14 +02:00
|
|
|
from common import i18n
|
|
|
|
_ = i18n._
|
2005-07-02 14:36:21 +02:00
|
|
|
from common import helpers
|
2003-11-30 17:02:00 +01:00
|
|
|
|
2005-05-12 19:17:20 +02:00
|
|
|
LOGPATH = os.path.expanduser('~/.gajim/logs')
|
|
|
|
if os.name == 'nt':
|
|
|
|
try:
|
2005-05-20 02:09:18 +02:00
|
|
|
# Documents and Settings\[User Name]\Application Data\Gajim\logs
|
2005-05-20 20:18:06 +02:00
|
|
|
LOGPATH = os.environ['appdata'] + '/Gajim/Logs'
|
2005-05-20 02:09:18 +02:00
|
|
|
except KeyError:
|
2005-05-20 20:13:38 +02:00
|
|
|
# win9x, ./logs
|
2005-05-20 20:18:06 +02:00
|
|
|
LOGPATH = 'Logs'
|
2005-05-12 19:17:20 +02:00
|
|
|
|
2005-04-16 00:02:13 +02:00
|
|
|
class Logger:
|
|
|
|
def __init__(self):
|
2005-05-20 02:05:18 +02:00
|
|
|
dot_gajim = os.path.dirname(LOGPATH)
|
2005-05-12 15:43:17 +02:00
|
|
|
if os.path.isfile(dot_gajim):
|
2005-05-20 02:05:18 +02:00
|
|
|
print dot_gajim, 'is file but it should be a directory'
|
2005-05-12 15:43:17 +02:00
|
|
|
print 'Gajim will now exit'
|
|
|
|
sys.exit()
|
|
|
|
if os.path.isdir(dot_gajim):
|
|
|
|
if os.path.isfile(LOGPATH):
|
2005-05-20 02:12:15 +02:00
|
|
|
print LOGPATH, 'is file but it should be a directory'
|
2005-05-12 15:43:17 +02:00
|
|
|
print 'Gajim will now exit'
|
|
|
|
sys.exit()
|
2005-05-23 19:01:13 +02:00
|
|
|
else: # dot_gajim doesn't exist
|
|
|
|
if dot_gajim: # is '' on win9x so avoid that
|
2005-05-23 13:35:58 +02:00
|
|
|
print 'creating', dot_gajim , 'directory'
|
2005-05-23 19:01:13 +02:00
|
|
|
os.mkdir(dot_gajim)
|
2005-05-23 17:32:35 +02:00
|
|
|
if not os.path.isdir(LOGPATH):
|
|
|
|
print 'creating', LOGPATH, 'directory'
|
2005-05-23 19:01:13 +02:00
|
|
|
os.mkdir(LOGPATH)
|
2005-07-02 01:13:46 +02:00
|
|
|
|
2005-04-16 00:02:13 +02:00
|
|
|
def write(self, kind, msg, jid, show = None, tim = None):
|
|
|
|
if not tim:
|
|
|
|
tim = time.time()
|
|
|
|
else:
|
|
|
|
tim = time.mktime(tim)
|
|
|
|
|
|
|
|
if not msg:
|
|
|
|
msg = ''
|
2005-07-02 14:36:21 +02:00
|
|
|
msg = helpers.to_one_line(msg)
|
2005-06-08 11:19:54 +02:00
|
|
|
if len(jid.split('/')) > 1:
|
|
|
|
ji, nick = jid.split('/', 1)
|
|
|
|
else:
|
|
|
|
ji = jid
|
|
|
|
nick = ''
|
2005-04-16 00:02:13 +02:00
|
|
|
files = []
|
|
|
|
if kind == 'status': #we save time:jid:show:msg
|
|
|
|
if not show:
|
|
|
|
show = 'online'
|
2005-05-12 19:46:17 +02:00
|
|
|
if common.gajim.config.get('log_notif_in_user_file'):
|
2005-06-08 11:19:54 +02:00
|
|
|
path_to_file = os.path.join(LOGPATH, ji)
|
|
|
|
if os.path.isdir(path_to_file):
|
2005-06-13 16:46:08 +02:00
|
|
|
jid = 'gcstatus'
|
|
|
|
msg = show + ':' + msg
|
|
|
|
show = nick
|
2005-06-08 11:19:54 +02:00
|
|
|
files.append(ji + '/' + ji)
|
|
|
|
if os.path.isfile(jid):
|
|
|
|
files.append(jid)
|
|
|
|
else:
|
|
|
|
files.append(ji)
|
2005-05-12 19:46:17 +02:00
|
|
|
if common.gajim.config.get('log_notif_in_sep_file'):
|
2005-04-16 00:02:13 +02:00
|
|
|
files.append('notify.log')
|
2005-04-16 19:03:21 +02:00
|
|
|
elif kind == 'incoming': # we save time:recv:message
|
2005-06-08 11:19:54 +02:00
|
|
|
path_to_file = os.path.join(LOGPATH, ji)
|
|
|
|
if os.path.isdir(path_to_file):
|
|
|
|
files.append(jid)
|
|
|
|
else:
|
|
|
|
files.append(ji)
|
2005-04-16 00:02:13 +02:00
|
|
|
jid = 'recv'
|
|
|
|
show = msg
|
|
|
|
msg = ''
|
2005-04-16 19:03:21 +02:00
|
|
|
elif kind == 'outgoing': # we save time:sent:message
|
2005-06-08 11:19:54 +02:00
|
|
|
path_to_file = os.path.join(LOGPATH, ji)
|
|
|
|
if os.path.isdir(path_to_file):
|
|
|
|
files.append(jid)
|
|
|
|
else:
|
|
|
|
files.append(ji)
|
2005-04-16 00:02:13 +02:00
|
|
|
jid = 'sent'
|
|
|
|
show = msg
|
|
|
|
msg = ''
|
2005-06-08 11:19:54 +02:00
|
|
|
elif kind == 'gc': # we save time:gc:nick:message
|
|
|
|
# create the folder if needed
|
|
|
|
ji_fn = os.path.join(LOGPATH, ji)
|
|
|
|
if os.path.isfile(ji_fn):
|
|
|
|
os.remove(ji_fn)
|
|
|
|
if not os.path.isdir(ji_fn):
|
|
|
|
os.mkdir(ji_fn)
|
|
|
|
files.append(ji + '/' + ji)
|
|
|
|
jid = 'gc'
|
2005-04-16 00:02:13 +02:00
|
|
|
show = nick
|
|
|
|
for f in files:
|
2005-05-12 15:43:17 +02:00
|
|
|
path_to_file = os.path.join(LOGPATH, f)
|
|
|
|
fic = open(path_to_file, 'a')
|
2005-04-16 11:36:18 +02:00
|
|
|
fic.write('%s:%s:%s' % (tim, jid, show))
|
|
|
|
if msg:
|
|
|
|
fic.write(':' + msg)
|
|
|
|
fic.write('\n')
|
2005-04-16 00:02:13 +02:00
|
|
|
fic.close()
|
|
|
|
|
2005-06-13 16:46:08 +02:00
|
|
|
def __get_path_to_file(self, fjid):
|
|
|
|
jid = fjid.split('/')[0]
|
|
|
|
path_to_file = os.path.join(LOGPATH, jid)
|
|
|
|
if os.path.isdir(path_to_file):
|
|
|
|
if fjid == jid: # we want to read the gc history
|
|
|
|
path_to_file = os.path.join(LOGPATH, jid + '/' + jid)
|
|
|
|
else: #we want to read pm history
|
|
|
|
path_to_file = os.path.join(LOGPATH, fjid)
|
|
|
|
return path_to_file
|
|
|
|
|
|
|
|
def get_nb_line(self, fjid):
|
2005-05-30 23:00:04 +02:00
|
|
|
'''return total number of lines in a log file
|
|
|
|
return 0 if log file does not exist'''
|
2005-06-13 16:46:08 +02:00
|
|
|
path_to_file = self.__get_path_to_file(fjid)
|
2005-05-30 23:00:04 +02:00
|
|
|
if not os.path.exists(path_to_file):
|
|
|
|
return 0
|
2005-05-12 15:43:17 +02:00
|
|
|
fic = open(path_to_file, 'r')
|
2005-05-30 23:00:04 +02:00
|
|
|
nb = 0 # number of lines
|
|
|
|
while fic.readline():
|
2005-04-16 00:02:13 +02:00
|
|
|
nb += 1
|
|
|
|
fic.close()
|
|
|
|
return nb
|
|
|
|
|
2005-06-13 16:46:08 +02:00
|
|
|
def read(self, fjid, begin_line, end_line):
|
2005-05-30 23:00:04 +02:00
|
|
|
'''return number of lines read and the text in the lines
|
|
|
|
return 0 and empty respectively if log file does not exist'''
|
2005-06-13 16:46:08 +02:00
|
|
|
path_to_file = self.__get_path_to_file(fjid)
|
2005-05-30 23:00:04 +02:00
|
|
|
if not os.path.exists(path_to_file):
|
|
|
|
return 0, []
|
2005-05-12 15:43:17 +02:00
|
|
|
fic = open(path_to_file, 'r')
|
2005-05-30 23:00:04 +02:00
|
|
|
nb = 0 # number of lines
|
2005-04-16 00:02:13 +02:00
|
|
|
lines = []
|
|
|
|
while (nb < begin_line and fic.readline()):
|
|
|
|
nb += 1
|
|
|
|
while nb < end_line:
|
|
|
|
line = fic.readline()
|
|
|
|
if line:
|
2005-07-02 14:36:21 +02:00
|
|
|
line = helpers.from_one_line(line)
|
2005-04-16 00:02:13 +02:00
|
|
|
lineSplited = line.split(':')
|
|
|
|
if len(lineSplited) > 2:
|
|
|
|
lines.append(lineSplited)
|
|
|
|
nb += 1
|
|
|
|
return nb, lines
|