<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> import atexit <EOL> import logging . config <EOL> import logging . handlers <EOL> import os <EOL> import os . path <EOL> import select <EOL> import socket <EOL> import SocketServer <EOL> import sys <EOL> import threading <EOL> import datetime <EOL> from cPickle import loads <EOL> from struct import unpack <EOL> LOG_DEBUG = logging . DEBUG <EOL> LOG_INFO = logging . INFO <EOL> LOG_WARNING = logging . WARNING <EOL> LOG_ERROR = logging . ERROR <EOL> LOG_CRITICAL = logging . CRITICAL <EOL> LOG_DEBUG2 = logging . DEBUG - <NUM_LIT:2> <EOL> LOG_DEBUG3 = logging . DEBUG - <NUM_LIT:3> <EOL> logging . addLevelName ( logging . NOTSET , '<STR_LIT:N>' ) <EOL> logging . addLevelName ( logging . DEBUG , '<STR_LIT:D>' ) <EOL> logging . addLevelName ( logging . INFO , '<STR_LIT:I>' ) <EOL> logging . addLevelName ( logging . WARNING , '<STR_LIT>' ) <EOL> logging . addLevelName ( logging . ERROR , '<STR_LIT:E>' ) <EOL> logging . addLevelName ( logging . CRITICAL , '<STR_LIT:C>' ) <EOL> logging . addLevelName ( LOG_DEBUG2 , '<STR_LIT>' ) <EOL> logging . addLevelName ( LOG_DEBUG3 , '<STR_LIT>' ) <EOL> def _configure_root ( ) : <EOL> """<STR_LIT>""" <EOL> filename = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> try : <EOL> with open ( filename , '<STR_LIT:w>' ) : <EOL> pass <EOL> except IOError : <EOL> filename = '<STR_LIT>' % os . getpid ( ) <EOL> msg_fmt = '<STR_LIT>' <EOL> date_fmt = '<STR_LIT>' <EOL> formatter = logging . Formatter ( msg_fmt , date_fmt ) <EOL> handler = logging . handlers . RotatingFileHandler ( filename ) <EOL> handler . setFormatter ( formatter ) <EOL> handler . setLevel ( <NUM_LIT:0> ) <EOL> logging . getLogger ( ) . addHandler ( handler ) <EOL> logging . _acquireLock ( ) <EOL> try : <EOL> logger = logging . getLogger ( ) <EOL> if not logger . handlers : <EOL> _configure_root ( ) <EOL> finally : <EOL> logging . _releaseLock ( ) <EOL> if os . path . exists ( '<STR_LIT>' ) : <EOL> logging . config . fileConfig ( '<STR_LIT>' ) <EOL> def getLogger ( name ) : <EOL> """<STR_LIT>""" <EOL> return logging . getLogger ( name ) <EOL> CONSOLE = None <EOL> def enable_console ( level = logging . WARNING ) : <EOL> """<STR_LIT>""" <EOL> global CONSOLE <EOL> if CONSOLE is None : <EOL> CONSOLE = logging . StreamHandler ( ) <EOL> formatter = logging . Formatter ( '<STR_LIT>' ) <EOL> CONSOLE . setFormatter ( formatter ) <EOL> CONSOLE . setLevel ( level ) <EOL> logger . addHandler ( CONSOLE ) <EOL> def disable_console ( ) : <EOL> """<STR_LIT>""" <EOL> logger . removeHandler ( CONSOLE ) <EOL> if int ( os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) ) : <EOL> enable_console ( ) <EOL> TRACER = None <EOL> def enable_trace ( stream = None ) : <EOL> """<STR_LIT>""" <EOL> global TRACER <EOL> if TRACER is None : <EOL> TRACER = logging . getLogger ( '<STR_LIT>' ) <EOL> TRACER . propagate = False <EOL> formatter = logging . Formatter ( '<STR_LIT>' ) <EOL> if stream is None : <EOL> stream = sys . stderr <EOL> elif isinstance ( stream , basestring ) : <EOL> stream = open ( stream , '<STR_LIT:w>' ) <EOL> handler = logging . StreamHandler ( stream ) <EOL> handler . setFormatter ( formatter ) <EOL> TRACER . addHandler ( handler ) <EOL> TRACER . setLevel ( logging . DEBUG ) <EOL> def disable_trace ( ) : <EOL> """<STR_LIT>""" <EOL> if TRACER is not None : <EOL> TRACER . setLevel ( logging . CRITICAL ) <EOL> if int ( os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) ) : <EOL> enable_trace ( ) <EOL> class Logger ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , level = None ) : <EOL> self . _name = name <EOL> self . _logger = logging . getLogger ( name ) <EOL> self . _level = None <EOL> if level is None : <EOL> self . level = self . _logger . getEffectiveLevel ( ) <EOL> else : <EOL> self . level = level <EOL> def __eq__ ( self , other ) : <EOL> try : <EOL> if self . _name == other . _name and self . _logger == other . _logger and self . _level == other . _level : <EOL> return True <EOL> except AttributeError : <EOL> pass <EOL> return False <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> state = self . __dict__ . copy ( ) <EOL> state [ '<STR_LIT>' ] = None <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> """<STR_LIT>""" <EOL> self . __dict__ = state <EOL> self . _logger = logging . getLogger ( self . _name ) <EOL> self . level = self . _level <EOL> def _get_level ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _level <EOL> def _set_level ( self , level ) : <EOL> """<STR_LIT>""" <EOL> self . _level = level <EOL> self . _logger . setLevel ( level ) <EOL> level = property ( _get_level , _set_level , doc = '<STR_LIT>' ) <EOL> def rename ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . _name = name <EOL> self . _logger = logging . getLogger ( name ) <EOL> self . _logger . setLevel ( self . _level ) <EOL> def debug ( self , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _logger . debug ( msg , * args , ** kwargs ) <EOL> def info ( self , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _logger . info ( msg , * args , ** kwargs ) <EOL> def warning ( self , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _logger . warning ( msg , * args , ** kwargs ) <EOL> def error ( self , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _logger . error ( msg , * args , ** kwargs ) <EOL> def exception ( self , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _logger . exception ( msg , * args , ** kwargs ) <EOL> def critical ( self , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _logger . critical ( msg , * args , ** kwargs ) <EOL> def log ( self , level , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _logger . log ( level , msg , * args , ** kwargs ) <EOL> class NullLogger ( object ) : <EOL> """<STR_LIT>""" <EOL> def debug ( self , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def info ( self , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def warning ( self , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def error ( self , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def exception ( self , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def critical ( self , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def log ( self , level , msg , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> _REMOTE_HANDLERS = { } <EOL> def install_remote_handler ( host , port , prefix = None ) : <EOL> """<STR_LIT>""" <EOL> if prefix is None : <EOL> prefix = '<STR_LIT>' % ( os . getpid ( ) , socket . gethostname ( ) ) <EOL> try : <EOL> sock = socket . create_connection ( ( host , port ) ) <EOL> except Exception as exc : <EOL> logging . error ( "<STR_LIT>" , <EOL> host , port , exc ) <EOL> return False <EOL> sock . close ( ) <EOL> handler = _RemoteHandler ( host , port , prefix ) <EOL> root = logging . getLogger ( ) <EOL> root . addHandler ( handler ) <EOL> my_pid = os . getpid ( ) <EOL> if my_pid not in _REMOTE_HANDLERS : <EOL> for pid , handlers in _REMOTE_HANDLERS . items ( ) : <EOL> for handler in handlers : <EOL> try : <EOL> root . removeHandler ( handler ) <EOL> handler . close ( ) <EOL> except KeyError : <EOL> pass <EOL> except Exception as exc : <EOL> logging . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" , str ( exc ) or repr ( exc ) ) <EOL> del _REMOTE_HANDLERS [ pid ] <EOL> _REMOTE_HANDLERS [ my_pid ] = [ ] <EOL> _REMOTE_HANDLERS [ my_pid ] . append ( handler ) <EOL> return True <EOL> def remove_remote_handlers ( ) : <EOL> """<STR_LIT>""" <EOL> root = logging . getLogger ( ) <EOL> for pid , handlers in _REMOTE_HANDLERS . items ( ) : <EOL> if pid == os . getpid ( ) : <EOL> for handler in handlers : <EOL> root . removeHandler ( handler ) <EOL> handler . close ( ) <EOL> del _REMOTE_HANDLERS [ pid ] <EOL> class _RemoteHandler ( logging . handlers . SocketHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port , prefix ) : <EOL> self . prefix = prefix <EOL> logging . handlers . SocketHandler . __init__ ( self , host , port ) <EOL> def handle ( self , record ) : <EOL> """<STR_LIT>""" <EOL> record . prefix = self . prefix <EOL> record . msg = record . getMessage ( ) <EOL> record . args = None <EOL> logging . handlers . SocketHandler . handle ( self , record ) <EOL> def logging_port ( server_host , client_host ) : <EOL> """<STR_LIT>""" <EOL> return _LogListener . server_port ( server_host , client_host ) <EOL> class _LogListener ( object ) : <EOL> """<STR_LIT>""" <EOL> _lock = threading . Lock ( ) <EOL> _listeners = { } <EOL> def __init__ ( self , hostname ) : <EOL> self . _server = None <EOL> self . _port = None <EOL> self . _started = threading . Event ( ) <EOL> self . _thread = threading . Thread ( name = hostname + '<STR_LIT>' , <EOL> target = self . _listen , args = ( hostname , ) ) <EOL> self . _thread . daemon = True <EOL> self . _thread . start ( ) <EOL> self . _started . wait ( <NUM_LIT:10> ) <EOL> if self . _port is None : <EOL> raise RuntimeError ( '<STR_LIT>' % hostname ) <EOL> @ property <EOL> def port ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _port <EOL> def _listen ( self , hostname ) : <EOL> """<STR_LIT>""" <EOL> self . _server = _LogServer ( ( hostname , <NUM_LIT:0> ) , _LogHandler ) <EOL> self . _port = self . _server . server_address [ <NUM_LIT:1> ] <EOL> logging . info ( '<STR_LIT>' , <EOL> hostname , self . _port ) <EOL> atexit . register ( self . _stop , os . getpid ( ) ) <EOL> self . _started . set ( ) <EOL> self . _server . service_loop ( ) <EOL> def _stop ( self , pid ) : <EOL> """<STR_LIT>""" <EOL> if os . getpid ( ) == pid and self . _thread . is_alive ( ) : <EOL> self . _server . stop ( ) <EOL> self . _thread . join ( <NUM_LIT:2> ) <EOL> @ staticmethod <EOL> def server_port ( server_host , client_host ) : <EOL> """<STR_LIT>""" <EOL> with _LogListener . _lock : <EOL> listener = _LogListener . _listeners . get ( server_host , None ) <EOL> if listener is None : <EOL> listener = _LogListener ( server_host ) <EOL> _LogListener . _listeners [ server_host ] = listener <EOL> port = listener . port <EOL> _LogServer . add_client ( client_host ) <EOL> return port <EOL> class _LogServer ( SocketServer . ThreadingTCPServer ) : <EOL> """<STR_LIT>""" <EOL> daemon_threads = True <EOL> _hosts = set ( ) <EOL> @ staticmethod <EOL> def add_client ( hostname ) : <EOL> """<STR_LIT>""" <EOL> client_addr = socket . gethostbyname ( hostname ) <EOL> _LogServer . _hosts . add ( client_addr ) <EOL> if client_addr . startswith ( '<STR_LIT>' ) and '<STR_LIT:127.0.0.1>' not in _LogServer . _hosts : <EOL> _LogServer . _hosts . add ( '<STR_LIT:127.0.0.1>' ) <EOL> def verify_request ( self , request , client_address ) : <EOL> """<STR_LIT>""" <EOL> host , port = client_address <EOL> if host in self . _hosts : <EOL> return True <EOL> logging . warning ( '<STR_LIT>' , <EOL> host , self . _hosts ) <EOL> return False <EOL> def service_loop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _is_shut_down = threading . Event ( ) <EOL> self . _shutdown_request = False <EOL> fd = self . socket . fileno ( ) <EOL> try : <EOL> while not self . _shutdown_request : <EOL> rd , wr , ex = select . select ( [ fd ] , [ ] , [ ] , <NUM_LIT:0.5> ) <EOL> if fd in rd : <EOL> self . _handle_request_noblock ( ) <EOL> finally : <EOL> self . _is_shut_down . set ( ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _shutdown_request = True <EOL> self . _is_shut_down . wait ( <NUM_LIT:2> ) <EOL> class _LogHandler ( SocketServer . StreamRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self ) : <EOL> """<STR_LIT>""" <EOL> makeLogRecord = logging . makeLogRecord <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> conn = self . connection <EOL> recv = conn . recv <EOL> peer = None <EOL> while True : <EOL> try : <EOL> data = recv ( <NUM_LIT:4> ) <EOL> except Exception : <EOL> return <EOL> if len ( data ) < <NUM_LIT:4> : <EOL> break <EOL> if peer is None : <EOL> host , port = conn . getpeername ( ) <EOL> try : <EOL> host , aliases , addrs = socket . gethostbyaddr ( host ) <EOL> except Exception as exc : <EOL> logger . debug ( '<STR_LIT>' , <EOL> host , str ( exc ) or repr ( exc ) ) <EOL> peer = '<STR_LIT>' % ( host , port ) <EOL> logger . info ( '<STR_LIT>' , peer ) <EOL> slen = unpack ( '<STR_LIT>' , data ) [ <NUM_LIT:0> ] <EOL> data = recv ( slen ) <EOL> slen -= len ( data ) <EOL> msg = data <EOL> while slen : <EOL> data = recv ( slen ) <EOL> slen -= len ( data ) <EOL> msg = '<STR_LIT>' . join ( ( msg , data ) ) <EOL> try : <EOL> obj = loads ( msg ) <EOL> record = makeLogRecord ( obj ) <EOL> except Exception as exc : <EOL> logger . exception ( "<STR_LIT>" , <EOL> peer , exc ) <EOL> else : <EOL> prefix = record . prefix <EOL> name = record . name <EOL> if name != prefix : <EOL> record . name = '<STR_LIT>' % ( prefix , name ) <EOL> else : <EOL> record . name = '<STR_LIT>' % prefix <EOL> logger . handle ( record ) <EOL> conn . close ( ) <EOL> if peer is not None : <EOL> logger . info ( '<STR_LIT>' , peer ) </s>
