<s> """<STR_LIT>""" <EOL> import os <EOL> import time <EOL> REQUEST_LOG_ID = '<STR_LIT>' <EOL> _U_SEC = <NUM_LIT> <EOL> FIXED_LOG_LINE_OVERHEAD = <NUM_LIT:15> <EOL> LOG_LEVEL_DEBUG = <NUM_LIT:0> <EOL> LOG_LEVEL_INFO = <NUM_LIT:1> <EOL> LOG_LEVEL_WARNING = <NUM_LIT:2> <EOL> LOG_LEVEL_ERROR = <NUM_LIT:3> <EOL> LOG_LEVEL_CRITICAL = <NUM_LIT:4> <EOL> LOG_LEVELS = [ LOG_LEVEL_DEBUG , <EOL> LOG_LEVEL_INFO , <EOL> LOG_LEVEL_WARNING , <EOL> LOG_LEVEL_ERROR , <EOL> LOG_LEVEL_CRITICAL ] <EOL> _DEFAULT_LEVEL = LOG_LEVEL_ERROR <EOL> def _CurrentTimeMicro ( ) : <EOL> return int ( time . time ( ) * _U_SEC ) <EOL> def _Clean ( e ) : <EOL> return e . replace ( '<STR_LIT>' , '<STR_LIT:\n>' ) <EOL> def Stripnl ( message ) : <EOL> if message and message [ - <NUM_LIT:1> ] == '<STR_LIT:\n>' : <EOL> return message [ : - <NUM_LIT:1> ] <EOL> return message <EOL> def RequestID ( ) : <EOL> """<STR_LIT>""" <EOL> return os . environ . get ( REQUEST_LOG_ID , None ) <EOL> def _StrictParseLogEntry ( entry , clean_message = True ) : <EOL> """<STR_LIT>""" <EOL> magic , level , timestamp , message = entry . split ( '<STR_LIT:U+0020>' , <NUM_LIT:3> ) <EOL> if magic != '<STR_LIT>' : <EOL> raise ValueError ( ) <EOL> timestamp , level = int ( timestamp ) , int ( level ) <EOL> if level not in LOG_LEVELS : <EOL> raise ValueError ( ) <EOL> return timestamp , level , _Clean ( message ) , None if clean_message else message <EOL> def ParseLogEntry ( entry ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return _StrictParseLogEntry ( entry ) <EOL> except ValueError : <EOL> return _CurrentTimeMicro ( ) , _DEFAULT_LEVEL , _Clean ( entry ) , None <EOL> def ParseLogs ( logs ) : <EOL> """<STR_LIT>""" <EOL> return [ ParseLogEntry ( line ) for line in logs . split ( '<STR_LIT:\n>' ) if line ] <EOL> class LoggingRecord ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , level , created , message , source_location ) : <EOL> self . level = level <EOL> self . created = created <EOL> self . source_location = source_location <EOL> self . message = message <EOL> def IsBlank ( self ) : <EOL> return False <EOL> def IsComplete ( self ) : <EOL> return True <EOL> def Tuple ( self ) : <EOL> return self . level , self . created , self . source_location , self . message <EOL> def __len__ ( self ) : <EOL> return len ( self . message ) + FIXED_LOG_LINE_OVERHEAD <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . level , self . created , self . message ) <EOL> def __eq__ ( self , x ) : <EOL> return ( self . level == x . level and self . created == x . created and <EOL> self . source_location == x . source_location and <EOL> self . message == x . message ) <EOL> class StderrRecord ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message ) : <EOL> self . message = message <EOL> self . _created = _CurrentTimeMicro ( ) <EOL> @ property <EOL> def level ( self ) : <EOL> return _DEFAULT_LEVEL <EOL> @ property <EOL> def created ( self ) : <EOL> return self . _created <EOL> def Tuple ( self ) : <EOL> return self . level , self . created , Stripnl ( self . message ) , self . source_location <EOL> @ property <EOL> def source_location ( self ) : <EOL> return None <EOL> def IsBlank ( self ) : <EOL> return self . message in [ '<STR_LIT>' , '<STR_LIT:\n>' ] <EOL> def IsComplete ( self ) : <EOL> return self . message and self . message [ - <NUM_LIT:1> ] == '<STR_LIT:\n>' <EOL> def __len__ ( self ) : <EOL> return len ( self . message ) <EOL> def __str__ ( self ) : <EOL> return self . message <EOL> def RecordFromLine ( line ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> created , level , unused_source_location , message = ( <EOL> _StrictParseLogEntry ( line , clean_message = False ) ) <EOL> message = Stripnl ( message ) <EOL> return LoggingRecord ( level , created , message , None ) <EOL> except ValueError : <EOL> return StderrRecord ( line ) </s>
