<s> import os <EOL> import sys <EOL> import traceback <EOL> import time <EOL> import cfg , cfgtypes <EOL> _thisFile = os . path . basename ( sys . modules [ __name__ ] . __file__ ) <EOL> _LOG = None <EOL> MaxArgLen = <NUM_LIT> <EOL> def logTime ( ) : <EOL> t = time . time ( ) <EOL> ret = time . strftime ( "<STR_LIT>" , time . localtime ( t ) ) <EOL> ret = ret + "<STR_LIT>" % ( int ( ( t - int ( t ) ) * <NUM_LIT:1000> ) , ) <EOL> return ret <EOL> def printErr ( * args ) : <EOL> pid = os . getpid ( ) <EOL> for arg in args : <EOL> sys . stderr . write ( "<STR_LIT>" % ( <EOL> pid , logTime ( ) , arg ) ) <EOL> sys . stderr . flush ( ) <EOL> class NullLog : <EOL> def __init__ ( self , filename = None , level = <NUM_LIT:0> ) : <EOL> self . pid = os . getpid ( ) <EOL> self . level = level <EOL> self . filename = filename <EOL> def log ( self , * args ) : <EOL> pass <EOL> def reset ( self , level = None ) : <EOL> if level : <EOL> self . level = level <EOL> def close ( self ) : <EOL> pass <EOL> def __call__ ( self , level , * args ) : <EOL> if level <= self . level : <EOL> self . log ( level , * args ) <EOL> class FileLog ( NullLog ) : <EOL> def __init__ ( self , filename = "<STR_LIT>" , level = <NUM_LIT:1> ) : <EOL> NullLog . __init__ ( self , filename , level ) <EOL> def __getFD ( ) : <EOL> isFile = <NUM_LIT:0> <EOL> if self . filename in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> return ( isFile , getattr ( sys , self . filename ) ) <EOL> mustChmod = not os . path . exists ( self . filename ) <EOL> try : <EOL> fd = open ( self . filename , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> if mustChmod : <EOL> os . chmod ( self . filename , <NUM_LIT:0> <NUM_LIT> ) <EOL> except : <EOL> printErr ( "<STR_LIT>" % ( self . filename , ) , <EOL> sys . exc_info ( ) [ : <NUM_LIT:2> ] ) <EOL> self . filename = "<STR_LIT>" <EOL> fd = sys . stderr <EOL> else : <EOL> isFile = <NUM_LIT:1> <EOL> return ( isFile , fd ) <EOL> ( self . isFile , self . fd ) = __getFD ( ) <EOL> self . reset ( ) <EOL> self . log ( <NUM_LIT:0> , "<STR_LIT>" % ( <EOL> self . level , self . pid , self . filename ) ) <EOL> def argStr ( self , val ) : <EOL> global MaxArgLen <EOL> ret = str ( val ) <EOL> if self . level > <NUM_LIT:3> : <EOL> return ret <EOL> if type ( val ) in set ( [ list , tuple , dict , set ] ) and len ( ret ) > MaxArgLen : <EOL> if len ( val ) > <NUM_LIT:1> : <EOL> ret = "<STR_LIT>" % ( type ( val ) , len ( val ) ) <EOL> return ret <EOL> def log ( self , level = <NUM_LIT:0> , * args ) : <EOL> global _thisFile <EOL> tbStack = traceback . extract_stack ( ) <EOL> callid = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> if _thisFile . startswith ( os . path . basename ( tbStack [ callid ] [ <NUM_LIT:0> ] ) ) : <EOL> callid = callid - <NUM_LIT:1> <EOL> break <EOL> callid = callid + <NUM_LIT:1> <EOL> module = '<STR_LIT>' <EOL> try : <EOL> path = tbStack [ callid ] [ <NUM_LIT:0> ] . split ( '<STR_LIT:/>' ) <EOL> if len ( path ) > <NUM_LIT:1> : <EOL> dirname = path [ - <NUM_LIT:2> ] + "<STR_LIT:/>" <EOL> else : <EOL> dirname = "<STR_LIT>" <EOL> filename = path [ - <NUM_LIT:1> ] . split ( '<STR_LIT:.>' ) <EOL> module = "<STR_LIT>" % ( dirname , "<STR_LIT:.>" . join ( filename [ : - <NUM_LIT:1> ] ) ) <EOL> del path <EOL> except : <EOL> module = '<STR_LIT>' <EOL> location = tbStack [ callid ] [ <NUM_LIT:2> ] <EOL> if location == "<STR_LIT:?>" : <EOL> location = "<STR_LIT:__main__>" <EOL> msg = "<STR_LIT>" % ( module , location ) <EOL> if len ( args ) : <EOL> msg = "<STR_LIT>" % ( msg , "<STR_LIT:U+0020>" . join ( [ self . argStr ( x ) for x in args ] ) ) <EOL> self . printLog ( level , msg ) <EOL> def printLog ( self , level , msg ) : <EOL> try : <EOL> self . fd . write ( "<STR_LIT>" % ( logTime ( ) , self . pid , msg ) ) <EOL> except OSError , e : <EOL> pass <EOL> def close ( self ) : <EOL> if self . isFile : <EOL> self . fd . close ( ) <EOL> self . level = self . filename = self . isFile = self . fd = None <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> class TraceLog ( FileLog ) : <EOL> def __init__ ( self , filename = "<STR_LIT>" , level = <NUM_LIT:1> ) : <EOL> self . times = [ time . time ( ) ] * level <EOL> FileLog . __init__ ( self , filename , level ) <EOL> def printLog ( self , level , msg ) : <EOL> t = time . time ( ) <EOL> timeStr = time . strftime ( "<STR_LIT>" , time . localtime ( t ) ) <EOL> timeIdx = max ( level - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> lineStr = "<STR_LIT>" % ( <EOL> self . pid , timeStr , t - self . times [ timeIdx ] , msg ) <EOL> for i in range ( timeIdx , self . level ) : <EOL> self . times [ i ] = t <EOL> try : <EOL> self . fd . write ( "<STR_LIT>" % ( lineStr , ) ) <EOL> except OSError , e : <EOL> pass <EOL> def reset ( self , level = None ) : <EOL> FileLog . reset ( self , level ) <EOL> self . times = [ time . time ( ) ] * self . level <EOL> def getLog ( filename = "<STR_LIT>" , level = <NUM_LIT:0> , trace = <NUM_LIT:0> ) : <EOL> if filename is None : <EOL> return NullLog ( None , <NUM_LIT:0> ) <EOL> path = os . path . dirname ( filename ) <EOL> if path : <EOL> if not os . path . exists ( path ) : <EOL> printErr ( "<STR_LIT>" % ( path , ) ) <EOL> filename = "<STR_LIT>" <EOL> if os . path . exists ( filename ) : <EOL> if not os . access ( filename , os . W_OK ) : <EOL> printErr ( "<STR_LIT>" % ( <EOL> filename , ) ) <EOL> filename = "<STR_LIT>" <EOL> elif not os . access ( path , os . W_OK | os . X_OK ) : <EOL> printErr ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( filename , ) ) <EOL> filename = "<STR_LIT>" <EOL> if trace : <EOL> return TraceLog ( filename , level ) <EOL> return FileLog ( filename , level ) <EOL> def initLog ( filename = "<STR_LIT>" , level = <NUM_LIT:0> , trace = <NUM_LIT:0> ) : <EOL> global _LOG <EOL> if _LOG is not None : <EOL> if filename is None or _LOG . filename == filename : <EOL> _LOG . reset ( level ) <EOL> return <EOL> _LOG = None <EOL> elif filename is None : <EOL> filename = "<STR_LIT>" <EOL> _LOG = getLog ( filename , level , trace ) <EOL> return <NUM_LIT:0> <EOL> def logMe ( level , * args ) : <EOL> global _LOG <EOL> if _LOG and _LOG . level >= level : <EOL> _LOG . log ( <NUM_LIT:0> , * args ) <EOL> def logErr ( * args ) : <EOL> global _LOG <EOL> if not args : <EOL> return <EOL> if _LOG : <EOL> _LOG . log ( <NUM_LIT:0> , * ( ( "<STR_LIT>" , ) + args ) ) <EOL> if _LOG . filename != "<STR_LIT>" : <EOL> printErr ( "<STR_LIT:U+0020>" . join ( [ str ( x ) for x in args ] ) ) <EOL> class CfgTraceLog ( cfg . CfgType ) : <EOL> def parseString ( self , str ) : <EOL> s = str . split ( ) <EOL> if len ( s ) != <NUM_LIT:2> : <EOL> raise cfgtypes . ParseError ( "<STR_LIT>" ) <EOL> try : <EOL> s = ( int ( s [ <NUM_LIT:0> ] ) , cfgtypes . Path ( s [ <NUM_LIT:1> ] ) ) <EOL> except Exception , e : <EOL> raise cfgtypes . ParseError ( <EOL> "<STR_LIT>" % ( e , ) ) <EOL> return s <EOL> def format ( self , val , displayOptions = None ) : <EOL> return "<STR_LIT>" % val </s>
