1#------------------------------------------------------------------------------
2#	asklog.py - log related functions
3#
4#	(C) 2001-2006 by Marco Paganini (paganini@paganini.net)
5#
6#   This file is part of ASK - Active Spam Killer
7#
8#   ASK is free software; you can redistribute it and/or modify
9#   it under the terms of the GNU General Public License as published by
10#   the Free Software Foundation; either version 2 of the License, or
11#   (at your option) any later version.
12#
13#   ASK is distributed in the hope that it will be useful,
14#   but WITHOUT ANY WARRANTY; without even the implied warranty of
15#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16#   GNU General Public License for more details.
17#
18#   You should have received a copy of the GNU General Public License
19#   along with ASK; if not, write to the Free Software
20#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21#
22#	$Id: asklog.py,v 1.9 2006/01/09 04:22:26 paganini Exp $
23#------------------------------------------------------------------------------
24
25import time
26import os
27
28#------------------------------------------------------------------------------
29
30class AskLog:
31	"""
32	Logs the output.
33
34	Variables:
35
36	- level:   global log level
37	- outfile: output filename
38	- logfh:   file object pointing to the logfile
39	"""
40
41	outfile   = ""
42	loglevel  = 0
43
44	#------------------------------------------------------------------------------
45	def __init__(self, outfile):
46		"""
47		Initializes the AskLog class. 'outfile' should point to the
48		desired filename (will be opened in 'append' mode).
49		"""
50
51		if (outfile != ''):
52			self.outfile = outfile;
53			self.logfh   = open(outfile, "a")
54
55	#------------------------------------------------------------------------------
56	def __del__(self):
57		## If no logging exist, logfh will not be valid
58		try:
59			self.logfh.close()
60		except:
61			pass
62
63	#------------------------------------------------------------------------------
64	def write(self, level, str):
65		"Generate a log output if self.loglevel >= level and outfile is not null"
66
67		if (self.loglevel >= level and self.outfile != ''):
68			self.logfh.write("%s [%s]: %s\n" % (time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(time.time())), os.getpid(), str))
69			self.logfh.flush()
70
71## EOF ##
72