log.py :  » Business-Application » hylaPEx » hylapex » library » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Business Application » hylaPEx 
hylaPEx » hylapex » library » log.py
#!/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'
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.