2014年12月8日 星期一

python 上一個簡單的 trace tools

寫程式時很怕測試人員沒頭沒尾的回報, 還是丟了一個用克林貢語寫的 bug 描述.

所以我都會額外自己發展一個小工具讓程序輸出 log, 讓測試人員回報用.

ptlog.py

#-------------------------------------------------------------------------------
# Name:        ptlog
# Purpose:
#
# Author:      nanoha
#
# Created:     10/12/2013
# Copyright:   (c) nanoha 2013
# Licence:     no licence
#-------------------------------------------------------------------------------

import os, inspect, sys
from time import strftime

__author__  = "nanoha <nanoha@nanoha.org>"
__status__  = "production"
__version__ = "0.0.6.0"
__date__    = "2014-05-07"

__IsPrint = True

if (os.name == 'nt'):
    __output = u'C:\\temp\\pt.txt'
elif (os.name == 'posix'):
    __output = u'/temp/pt.txt'

def SetOutput(szLog):
    global __output
    __output = szLog

def IsPrint(bPrint):
    global __IsPrint
    __IsPrint = bPrint

def __WriteFile(szFileName, szFuncName, szMsgline, szFlag, szMsg):
    hFile = None
    try:
        now = strftime('%Y-%m-%d %H:%M:%S')
        szFormatMsg = '[%s][%s][%s][%s][%s]%s\n' % ( now, szFileName, szFuncName, szMsgline, szFlag, szMsg)
        hFile = open(__output, 'a+')
        hFile.write(szFormatMsg)
    except Exception as error:
        try:
            now = strftime('%Y-%m-%d %H:%M:%S')
            szFormatMsg = u'[%s][%s][%s][%s][%s]%s\n' % (unicode(now), unicode(szFileName), unicode(szFuncName), unicode(szMsgline), unicode(szFlag), unicode(error))
            hFile = open(__output, 'a+')
            hFile.write(szFormatMsg.encode('utf-8'))
        except Exception as error:
            print error
    finally:
        if hFile:
            hFile.close()


def PTError(szMsg):
    try:
        Callerframel = inspect.stack()[1][0]
        if (__IsPrint):
            print '[%s][%s][%s][%s]%s' % (Callerframel.f_code.co_filename, Callerframel.f_code.co_name, Callerframel.f_lineno, 'PTERROR', szMsg)
        if (os.path.exists(__output)):
            __WriteFile(Callerframel.f_code.co_filename, Callerframel.f_code.co_name, Callerframel.f_lineno, 'PTERROR', szMsg)
    except Exception as error:
        pass

def convertToStr(szMsg):
    ret = ''
    try:
        if isinstance(szMsg, unicode):
            ret = szMsg.encode('utf-8')
        elif isinstance(szMsg, str):
            ret = szMsg
    except Exception as error:
        PTError(str(error))
    finally:
        return ret

def debug(szMsg):
    Callerframel = inspect.stack()[1][0]
    if (__IsPrint):
        try:
            print '[%s][%s][%s][%s]%s' % (Callerframel.f_code.co_filename, Callerframel.f_code.co_name, Callerframel.f_lineno, 'DEBUG', szMsg)
        except Exception as error:
            PTError(str(error))

    if (os.path.exists(__output)):
        szMsg = convertToStr(szMsg)
        if szMsg:
            __WriteFile(Callerframel.f_code.co_filename, Callerframel.f_code.co_name, Callerframel.f_lineno, 'DEBUG', szMsg)

def info(szMsg):
    Callerframel = inspect.stack()[1][0]
    if (__IsPrint):
        try:
            print '[%s][%s][%s][%s]%s' % (Callerframel.f_code.co_filename, Callerframel.f_code.co_name, Callerframel.f_lineno, 'INFO', szMsg)
        except Exception as error:
            PTError(str(error))

    if (os.path.exists(__output)):
        szMsg = convertToStr(szMsg)
        if szMsg:
            __WriteFile(Callerframel.f_code.co_filename, Callerframel.f_code.co_name, Callerframel.f_lineno, 'INFO', szMsg)

def warning(szMsg):
    Callerframel = inspect.stack()[1][0]
    if (__IsPrint):
        try:
            print '[%s][%s][%s][%s]%s' % (Callerframel.f_code.co_filename, Callerframel.f_code.co_name, Callerframel.f_lineno, 'WARNING', szMsg)
        except Exception as error:
            PTError(str(error))

    if (os.path.exists(__output)):
        szMsg = convertToStr(szMsg)
        if szMsg:
            __WriteFile(Callerframel.f_code.co_filename, Callerframel.f_code.co_name, Callerframel.f_lineno, 'WARNING', szMsg)

def error(szMsg):
    Callerframel = inspect.stack()[1][0]
    if (__IsPrint):
        try:
            print '[%s][%s][%s][%s]%s' % (Callerframel.f_code.co_filename, Callerframel.f_code.co_name, Callerframel.f_lineno, 'ERROR', szMsg)
        except Exception as error:
            PTError(str(error))

    if (os.path.exists(__output)):
        szMsg = convertToStr(szMsg)
        if szMsg:
            __WriteFile(Callerframel.f_code.co_filename, Callerframel.f_code.co_name, Callerframel.f_lineno, 'ERROR', szMsg)

def critical(szMsg):
    Callerframel = inspect.stack()[1][0]
    if (__IsPrint):
        try:
            print '[%s][%s][%s][%s]%s' % (Callerframel.f_code.co_filename, Callerframel.f_code.co_name, Callerframel.f_lineno, 'CRITICAL', szMsg)
        except Exception as error:
            PTError(str(error))

    if (os.path.exists(__output)):
        szMsg = convertToStr(szMsg)
        if szMsg:
            __WriteFile(Callerframel.f_code.co_filename, Callerframel.f_code.co_name, Callerframel.f_lineno, 'CRITICAL', szMsg)
#-------------------------------------------------------------------------------

測試, 如果 C:\\temp\\pt.txt 不存在則不輸出,
要輸出別的名稱可用 ptlog.SetOutput('.\\a.txt')
#-------------------------------------------------------------------------------
import ptlog

def main():
    ptlog.debug('test 1')
    ptlog.info('test 2')
    ptlog.warning('test 3')
    ptlog.error('test 4')
    ptlog.critical('test 5')

if __name__ == '__main__':
    main()

#-------------------------------------------------------------------------------

沒有留言:

張貼留言