#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# hylaPEx, an hylafax client written in python
#
# www.unipex.it/hylapex/
#
# License:
# GNU General Public License (GPL)
# For more info see LICENSE.txt and LICENSE-GPL.txt
#
# Copyright (C) 2004-2006 Unipex s.r.l., All Rights Reserved.
# Via Vittorio Veneto 83/A
# 33050 Gonars (UD) - Italy
# phone 0039 0432 931511 - fax 0039 0432 931378
# www.unipex.it - michele.petrazzo@unipex.it
DEBUG = 0
import time, string, tempfile, os, sys
from types import *
def log_complete_call():
msg = ""
numFrames = 2
while True:
try:
frame = sys._getframe(numFrames)
except ValueError:
break
fname, linen, method = frame.f_code.co_filename, frame.f_lineno, frame.f_code.co_name
msg += ( "%s, %s, %s: \n" % (fname, linen, method ) )
numFrames += 1
return msg
class log(object):
def __init__(self,f,t='w', tmp_dir=None):
if not tmp_dir:
tmp_dir = tempfile.gettempdir()
try:
self.fileName = f
self.f = open(f, t)
except:
fd, self.fileName = tempfile.mkstemp(prefix="hylapex_tmp_log", dir=tmp_dir)
self.f = os.fdopen(fd,t)
#self.f.write("%s\n" % tmp_dir)
def _internal_write(self, *args, **kw):
if DEBUG:
print args, kw
self.f.write(*args, **kw)
def close(self):
self.f.close()
def getAll(self):
self.close()
f = open(self.fileName, 'r')
lines = f.readlines()
f.close()
self.f = open(self.fileName, 'a')
return lines
def write(self, obj, *args, **kw): #Wrote the log of the obj passed
hour = time.ctime()
if obj != None:
if len(str(obj)) == 0:
self._internal_write(hour + ' - ' + 'No data' + "\n")
return
if type(obj) is FileType: # Se l'oggetto e' un file
try:
lines = obj.readlines()
for line in lines:
self._internal_write(hour + ' - ' + self._del_n(line) + "\n")
except Exception, ex:
print ex
elif type(obj) is StringType:
self._internal_write(hour + ' - ' + self._del_n(obj) + "\n")
elif type(obj) is DictType:
for k in obj.keys():
try:
self._internal_write(hour + ' - ' + k + '=' + self._del_n(obj[k]) + "\n")
except:
self._internal_write(hour + ' - ' + str(obj) + '\n')
elif type(obj) in (ListType,TupleType):
try:
if len(obj) > 0 and not self.ctrl_str(obj[0]):
for o in obj:
self.write(o)
else:
self._internal_write(hour + ' - ' + str(obj) + '\n')
except:
self._internal_write(hour + ' - ' + str(obj) + "\n")
else:
self._internal_write(hour + ' - ' + str(obj) + "\n")
else:
self._internal_write(hour + ' - ' + 'No data' + "\n")
if args: self._internal_write(str(args) + "\n")
if kw: self._internal_write(str(kw) + "\n")
def ctrl_str(self, obj):
if type(obj) == StringType:
return 1
else:
return 0
def _del_n(self,obj):
try:
return obj.strip()
except:
return ''
if __name__ == 'main':
print 'This class is for module use only'
|