<s> import datetime <EOL> import os <EOL> import time <EOL> from contextlib import contextmanager <EOL> _instatrace = None <EOL> def init_trace ( filename ) : <EOL> global _instatrace <EOL> if _instatrace is not None : <EOL> _instatrace . close ( ) <EOL> _instatrace = Instatrace ( filename ) <EOL> class Instatrace : <EOL> def __init__ ( self , filename ) : <EOL> if os . path . exists ( filename ) : <EOL> now = datetime . datetime . now ( ) <EOL> stamp = now . strftime ( "<STR_LIT>" ) <EOL> os . rename ( filename , "<STR_LIT>" % ( filename , stamp ) ) <EOL> self . _fd = open ( filename , "<STR_LIT:w>" ) <EOL> def now ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( time . time ( ) * <NUM_LIT> ) <EOL> def now_ms ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( time . time ( ) * <NUM_LIT:1000> ) <EOL> def trace ( self , stat , value , data = None ) : <EOL> extra = "<STR_LIT>" <EOL> if data is not None : <EOL> extra = "<STR_LIT:U+0020>" + repr ( data ) <EOL> self . _fd . write ( "<STR_LIT>" % ( stat , value , extra ) ) <EOL> def trace ( stat , value , user_data = None ) : <EOL> if _instatrace is not None : <EOL> _instatrace . trace ( stat , value , user_data ) <EOL> @ contextmanager <EOL> def trace_us ( statName ) : <EOL> if _instatrace is None : <EOL> yield <EOL> return <EOL> now = _instatrace . now ( ) <EOL> yield <EOL> _instatrace . trace ( statName , _instatrace . now ( ) - now ) <EOL> @ contextmanager <EOL> def trace_ms ( statName ) : <EOL> if _instatrace is None : <EOL> yield <EOL> return <EOL> now = _instatrace . now_ms ( ) <EOL> yield <EOL> _instatrace . trace ( statName , _instatrace . now_ms ( ) - now ) </s>
