这是limodou的ulipad的Debug.py模块,独立运行此模块,会出现异常。 试图改正它后,又有一个错误。
类有属性和方法作用域的问题,还有初始化先后作用域的问题。一般我们很少碰到这样的问题。
声明一下:模块中出现的问题无损于程序的优秀性。这也是我要全文张贴的原因
# Programmer: limodou
# E-mail: limodou@gmail.com
#
# Copyleft 2006 limodou
#
# Distributed under the terms of the GPL (GNU Public License)
#
# UliPad 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# $Id: Debug.py 1504 2006-09-02 04:08:43Z limodou $
DEBUG = True import types import sys import traceback import time import os.path class Debug: def __init__(self, filename=os.path.abspath('debug.txt'), debug=None): self.filename = filename self.reset(filename) self.debug = debug def log(self, *args): self.output(*args) def info(self, *args): self.output('[ INFO] -- ', *args) def warn(self, *args): self.output('[ WARN] -- ', *args) def error(self, *args): self.output('[ERROR] -- ', *args) def debug(self, *args): self.output('[DEBUG] -- ', *args) def traceback(self):
message = traceback.format_exception(*sys.exc_info()) self.output('[Traceback]', ''.join(message)) def time(self, *args): self.output('[%s] -- ' % time.ctime(time.time()), *args) def output(self, *args): ifself.is_debug():
#encoding = locale.getdefaultlocale()[1]
encoding = 'utf-8'
out = open(self.filename, 'a') for i in args: ifnot type(i) in [types.UnicodeType, types.StringTypes, types.StringType]:
s= repr(i) else:
s = i if type(s) == type(u''):
out.write(s.encode(encoding)) else:
out.write(s)
out.write("\n")
out.close() def reset(self, filename): ifself.is_debug():
open(filename, 'w') self.filename = filename def is_debug(self): ifnot DEBUG: returnFalse ifself.debug isNone: return DEBUG else: returnself.debug
debug = None
error = None if __name__ == '__main__':
debug = Debug()
error = Debug(os.path.abspath('error.txt'))
debug.log('log')
debug.info('info')
debug.warn('warn')
debug.error('error')
debug.debug('debug')