<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import time <EOL> import logging <EOL> import logging . handlers <EOL> class LazyFormat ( object ) : <EOL> def __init__ ( self , prefix , format , message ) : <EOL> self . prefix = prefix <EOL> self . format = format <EOL> self . message = message <EOL> def __str__ ( self ) : <EOL> if self . format : <EOL> return self . prefix + self . format ( self . message ) <EOL> return self . prefix + self . message <EOL> def split ( self , c ) : <EOL> return str ( self ) . split ( c ) <EOL> class _Logger ( object ) : <EOL> _instance = None <EOL> _syslog = None <EOL> _history = [ ] <EOL> _max_history = <NUM_LIT:20> <EOL> _config = '<STR_LIT>' <EOL> _pid = os . getpid ( ) <EOL> def config ( self , config = None ) : <EOL> if config is not None : <EOL> self . _config = config <EOL> return self . _config <EOL> def history ( self ) : <EOL> return "<STR_LIT:\n>" . join ( self . _format ( * _ ) for _ in self . _history ) <EOL> def _record ( self , timestamp , level , source , message ) : <EOL> if len ( self . _history ) > self . _max_history : <EOL> self . _history . pop ( <NUM_LIT:0> ) <EOL> self . _history . append ( ( timestamp , level , source , message ) ) <EOL> def _format ( self , timestamp , level , source , message ) : <EOL> now = time . strftime ( '<STR_LIT>' , timestamp ) <EOL> return "<STR_LIT>" % ( now , level , self . _pid , source , message ) <EOL> def _prefixed ( self , level , source , message ) : <EOL> ts = time . localtime ( ) <EOL> self . _record ( ts , level , source , message ) <EOL> return self . _format ( ts , level , source , message ) <EOL> def __init__ ( self ) : <EOL> if os . environ . get ( '<STR_LIT>' , '<STR_LIT:1>' ) in [ '<STR_LIT:1>' , '<STR_LIT:yes>' , '<STR_LIT>' , '<STR_LIT>' ] : self . _supervisor = True <EOL> else : self . _supervisor = False <EOL> if os . environ . get ( '<STR_LIT>' , '<STR_LIT:1>' ) in [ '<STR_LIT:1>' , '<STR_LIT:yes>' , '<STR_LIT>' , '<STR_LIT>' ] : self . _daemon = True <EOL> else : self . _daemon = False <EOL> if os . environ . get ( '<STR_LIT>' , '<STR_LIT:1>' ) in [ '<STR_LIT:1>' , '<STR_LIT:yes>' , '<STR_LIT>' , '<STR_LIT>' ] : self . _processes = True <EOL> else : self . _processes = False <EOL> if os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) == '<STR_LIT:0>' : self . _configuration = False <EOL> else : self . _configuration = True <EOL> if os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) == '<STR_LIT:0>' : self . _wire = False <EOL> else : self . _wire = True <EOL> if os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) in [ '<STR_LIT:1>' , '<STR_LIT:yes>' , '<STR_LIT>' , '<STR_LIT>' ] : self . _message = True <EOL> else : self . _message = False <EOL> if os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) == '<STR_LIT:0>' : self . _rib = False <EOL> else : self . _rib = True <EOL> if os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) == '<STR_LIT:0>' : self . _timer = False <EOL> else : self . _timer = True <EOL> if os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) == '<STR_LIT:0>' : self . _routes = False <EOL> else : self . _routes = True <EOL> if os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) == '<STR_LIT:0>' : self . _routes = False <EOL> else : self . _routes = True <EOL> if os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) == '<STR_LIT:0>' : self . _parser = False <EOL> else : self . _parser = True <EOL> if not os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) == '<STR_LIT:0>' : <EOL> self . _supervisor = True <EOL> self . _daemon = True <EOL> self . _processes = True <EOL> self . _configuration = True <EOL> self . _wire = True <EOL> self . _message = True <EOL> self . _rib = True <EOL> self . _timer = True <EOL> self . _routes = True <EOL> self . _parser = True <EOL> if not os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) == '<STR_LIT:0>' : <EOL> self . _supervisor = True <EOL> self . _daemon = True <EOL> self . _processes = True <EOL> self . _message = True <EOL> self . _timer = True <EOL> self . _routes = True <EOL> self . _parser = False <EOL> destination = os . environ . get ( '<STR_LIT>' , None ) <EOL> if destination is None : <EOL> return <EOL> try : <EOL> if destination == '<STR_LIT>' : <EOL> if sys . platform == "<STR_LIT>" : <EOL> address = '<STR_LIT>' <EOL> else : <EOL> address = '<STR_LIT>' <EOL> if not os . path . exists ( address ) : <EOL> address = ( '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> handler = logging . handlers . SysLogHandler ( address ) <EOL> elif destination . lower ( ) . startswith ( '<STR_LIT>' ) : <EOL> address = ( destination [ <NUM_LIT:5> : ] . strip ( ) , <NUM_LIT> ) <EOL> handler = logging . handlers . SysLogHandler ( address ) <EOL> else : <EOL> handler = logging . handlers . RotatingFileHandler ( destination , maxBytes = <NUM_LIT:5> * <NUM_LIT> * <NUM_LIT> , backupCount = <NUM_LIT:5> ) <EOL> self . _syslog = logging . getLogger ( ) <EOL> self . _syslog . setLevel ( logging . DEBUG ) <EOL> self . _syslog . addHandler ( handler ) <EOL> except IOError : <EOL> self . critical ( '<STR_LIT>' ) <EOL> def debug ( self , message , source = '<STR_LIT>' , level = '<STR_LIT>' ) : <EOL> for line in message . split ( '<STR_LIT:\n>' ) : <EOL> if self . _syslog : <EOL> self . _syslog . debug ( self . _prefixed ( level , source , line ) ) <EOL> else : <EOL> print self . _prefixed ( level , source , line ) <EOL> sys . stdout . flush ( ) <EOL> def info ( self , message , source = '<STR_LIT>' , level = '<STR_LIT>' ) : <EOL> for line in message . split ( '<STR_LIT:\n>' ) : <EOL> if self . _syslog : <EOL> self . _syslog . info ( self . _prefixed ( level , source , line ) ) <EOL> else : <EOL> print self . _prefixed ( level , source , line ) <EOL> sys . stdout . flush ( ) <EOL> def warning ( self , message , source = '<STR_LIT>' , level = '<STR_LIT>' ) : <EOL> for line in message . split ( '<STR_LIT:\n>' ) : <EOL> if self . _syslog : <EOL> self . _syslog . warning ( self . _prefixed ( level , source , line ) ) <EOL> else : <EOL> print self . _prefixed ( level , source , line ) <EOL> sys . stdout . flush ( ) <EOL> def error ( self , message , source = '<STR_LIT>' , level = '<STR_LIT>' ) : <EOL> for line in message . split ( '<STR_LIT:\n>' ) : <EOL> if self . _syslog : <EOL> self . _syslog . error ( self . _prefixed ( level , source , line ) ) <EOL> else : <EOL> print self . _prefixed ( level , source , line ) <EOL> sys . stdout . flush ( ) <EOL> def critical ( self , message , source = '<STR_LIT>' , level = '<STR_LIT>' ) : <EOL> for line in message . split ( '<STR_LIT:\n>' ) : <EOL> if self . _syslog : <EOL> self . _syslog . critical ( self . _prefixed ( level , source , line ) ) <EOL> else : <EOL> print self . _prefixed ( level , source , line ) <EOL> sys . stdout . flush ( ) <EOL> def wire ( self , message ) : <EOL> if self . _wire : <EOL> self . debug ( message , '<STR_LIT>' ) <EOL> else : <EOL> self . _record ( time . localtime ( ) , '<STR_LIT>' , '<STR_LIT>' , message ) <EOL> def message ( self , message ) : <EOL> if self . _message : <EOL> self . info ( message , '<STR_LIT:message>' ) <EOL> else : <EOL> self . _record ( time . localtime ( ) , '<STR_LIT:message>' , '<STR_LIT:info>' , message ) <EOL> def configuration ( self , message ) : <EOL> if self . _configuration : <EOL> self . info ( message , '<STR_LIT>' ) <EOL> else : <EOL> self . _record ( time . localtime ( ) , '<STR_LIT>' , '<STR_LIT:info>' , message ) <EOL> def supervisor ( self , message ) : <EOL> if self . _supervisor : <EOL> self . info ( message , '<STR_LIT>' ) <EOL> else : <EOL> self . _record ( time . localtime ( ) , '<STR_LIT>' , '<STR_LIT:info>' , message ) <EOL> def rib ( self , message ) : <EOL> if self . _rib : <EOL> self . info ( message , '<STR_LIT>' ) <EOL> else : <EOL> self . _record ( time . localtime ( ) , '<STR_LIT>' , '<STR_LIT:info>' , message ) <EOL> def timers ( self , message ) : <EOL> if self . _timer : <EOL> self . info ( message , '<STR_LIT>' ) <EOL> else : <EOL> self . _record ( time . localtime ( ) , '<STR_LIT>' , '<STR_LIT:info>' , message ) <EOL> def daemon ( self , message ) : <EOL> if self . _daemon : <EOL> self . info ( message , '<STR_LIT>' ) <EOL> else : <EOL> self . _record ( time . localtime ( ) , '<STR_LIT>' , '<STR_LIT:info>' , message ) <EOL> def processes ( self , message ) : <EOL> if self . _processes : <EOL> self . info ( message , '<STR_LIT>' ) <EOL> else : <EOL> self . _record ( time . localtime ( ) , '<STR_LIT>' , '<STR_LIT:info>' , message ) <EOL> def routes ( self , message ) : <EOL> if self . _routes : <EOL> self . info ( message , '<STR_LIT>' ) <EOL> else : <EOL> self . _record ( time . localtime ( ) , '<STR_LIT>' , '<STR_LIT:info>' , message ) <EOL> def parser ( self , message ) : <EOL> if self . _parser : <EOL> self . info ( message , '<STR_LIT>' ) <EOL> def Logger ( ) : <EOL> if _Logger . _instance : <EOL> return _Logger . _instance <EOL> instance = _Logger ( ) <EOL> _Logger . _instance = instance <EOL> return instance <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> logger = Logger ( ) <EOL> logger . wire ( '<STR_LIT>' ) <EOL> logger . message ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' ) </s>
