2011-07-15 9 views
1

calibreのようなパイソンプログラムをデバッグしたいです。通常、私はpdbをコンソールからデバッグするために使用していましたが、python GUIプログラムでpdbを使用すると、GUI部分(キャンバスまたはそれが何であるか)がフリーズし、そのようにデバッグするのは非常に困難です。Python GUIプログラムをどのようにデバッグしますか?

pythonのGUIプログラムをデバッグするための提案はありますか?どうやってやるの?

+0

は、標準ロギングモジュールは、あなたのニーズを満たしていませんか? – wberry

+0

@wberry Python GUIプログラムをデバッグする際に、標準のロギングモジュールを使用するための例やリンクを教えてください。 –

答えて

0

私のGUIコードの各イベントハンドラの関数/メソッドの最上部に "user action"(つまり、マウスがクリックされたとき)を表すコールをlogging.debugに配置します。また、ビューを更新する上位レベルの関数は、最初にlogging.debugコールを持ちます。これらのログメッセージは、関数/メソッドで使用される重要な情報を報告します。メッセージはDEBUGレベルに記録されているため、簡単な設定変更でオン/オフを切り替えることができます。

また、複雑ではないが、問題が見つかるまで、loggingモジュールを忘れてprintステートメントを一時的に挿入する方が早いかもしれません。

ここで私は、回転ログファイルをloggingモジュールを初期化するために書いたいくつかのコードです:

import pytz 

timestamp_detailed_format = '%Y-%m-%d %H:%M:%S.%f %Z' 

def detailed_format(date): 
    u"Given a datetime object return a detailed representation including fractional seconds and time zone" 
    return unicode(date.strftime(timestamp_detailed_format)) 

def localize_epoch_time(epoch_time, timezone=pytz.UTC): 
    u"Given an epoch time return an accurate, timezone-aware datetime object" 
    t = localtime(epoch_time) 
    epochdt = datetime(*(t[:6] + (int((epoch_time - long(epoch_time)) * 1000000),))).astimezone(timezone) 
    if hasattr(timezone, 'normalize'): # pytz tzinfo objects have this 
    return timezone.normalize(epochdt) 
    else: # tzinfo object not from pytz module 
    return epochdt 

class TimezoneAwareFormatter(logging.Formatter): 
    u"custom log formatter using timezone-aware timestamps" 
    def __init__(self, logformat=None, timezone=pytz.UTC): 
    logging.Formatter.__init__(self, logformat) 
    self._timezone = timezone 
    def formatTime(self, record, _=None): 
    u"times will be formatted as YYYY-MM-DD HH:MM:SS.ssssss TZ" 
    return detailed_format(localize_epoch_time(record.created, self._timezone)) 

def simple_log_file(filename, logname=None, level=logging.NOTSET, 
        threshold=10485760, generations=2, quiet=False, timezone=pytz.UTC): 
    u"initialize logging API for a simple generational log file, return logger object" 
    formatter = TimezoneAwareFormatter('%(asctime)s %(levelname)s %(message)s', timezone) 
    handler = logging.handlers.RotatingFileHandler(filename, 'a', threshold, generations, 'UTF-8') 
    handler.setFormatter(formatter) 
    logger = logging.getLogger(logname) 
    logger.addHandler(handler) 
    logger.setLevel(level) 
    if not quiet: logger.info(u'Logging to this destination has started') 
    return logger 
関連する問題