<s> '''<STR_LIT>''' <EOL> import socket <EOL> import logging . handlers <EOL> import SocketServer <EOL> import struct <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> class Log ( object ) : <EOL> def __init__ ( self , name , default_level = logging . DEBUG ) : <EOL> self . logger = logging . getLogger ( name ) <EOL> self . logger . setLevel ( default_level ) <EOL> self . formatter = logging . Formatter ( <EOL> '<STR_LIT>' ) <EOL> def add_stream_log ( self , level = logging . DEBUG , format_ = False ) : <EOL> stream_handler = logging . StreamHandler ( ) <EOL> if format_ : <EOL> stream_handler . setFormatter ( self . formatter ) <EOL> stream_handler . setLevel ( level ) <EOL> self . logger . addHandler ( stream_handler ) <EOL> def add_file_log ( self , filename , level = logging . INFO ) : <EOL> handler = logging . FileHandler ( filename ) <EOL> handler . setFormatter ( self . formatter ) <EOL> handler . setLevel ( level ) <EOL> self . logger . addHandler ( handler ) <EOL> def add_remote_log ( self , server , level = logging . INFO ) : <EOL> if '<STR_LIT::>' in server : <EOL> server , port = tuple ( server . split ( '<STR_LIT::>' , <NUM_LIT:1> ) ) <EOL> port = int ( port ) <EOL> else : <EOL> port = logging . handlers . DEFAULT_TCP_LOGGING_PORT <EOL> socket_handler = logging . handlers . SocketHandler ( server , port ) <EOL> socket_handler . setLevel ( level ) <EOL> self . logger . addHandler ( socket_handler ) <EOL> def get_logger ( self ) : <EOL> return self . logger <EOL> def get_logger ( name = '<STR_LIT>' , filename = None , server = None , is_master = False , <EOL> basic_level = logging . DEBUG ) : <EOL> log = Log ( name , basic_level ) <EOL> log . add_stream_log ( basic_level ) <EOL> if filename is not None : <EOL> level = logging . INFO <EOL> if is_master : <EOL> level = logging . ERROR <EOL> log . add_file_log ( filename , level ) <EOL> if server is not None : <EOL> log . add_remote_log ( server , logging . INFO ) <EOL> return log . get_logger ( ) <EOL> def add_log_client ( logger , client ) : <EOL> if '<STR_LIT::>' in client : <EOL> client , port = tuple ( client . split ( '<STR_LIT::>' , <NUM_LIT:1> ) ) <EOL> port = int ( port ) <EOL> else : <EOL> port = logging . handlers . DEFAULT_TCP_LOGGING_PORT <EOL> socket_handler = logging . handlers . SocketHandler ( client , port ) <EOL> socket_handler . setLevel ( logging . INFO ) <EOL> logger . addHandler ( socket_handler ) <EOL> return socket_handler <EOL> class LogRecordStreamHandler ( SocketServer . StreamRequestHandler ) : <EOL> def handle ( self ) : <EOL> self . connection . setblocking ( <NUM_LIT:0> ) <EOL> while not self . server . abort : <EOL> try : <EOL> chunk = self . connection . recv ( <NUM_LIT:4> ) <EOL> if len ( chunk ) < <NUM_LIT:4> : <EOL> break <EOL> slen = struct . unpack ( '<STR_LIT>' , chunk ) [ <NUM_LIT:0> ] <EOL> chunk = self . connection . recv ( slen ) <EOL> while len ( chunk ) < slen : <EOL> chunk = chunk + self . connection . recv ( slen - len ( chunk ) ) <EOL> obj = self . unPickle ( chunk ) <EOL> record = logging . makeLogRecord ( obj ) <EOL> self . handleLogRecord ( record ) <EOL> except socket . error : <EOL> return <EOL> def unPickle ( self , data ) : <EOL> return pickle . loads ( data ) <EOL> def handleLogRecord ( self , record ) : <EOL> if self . server . logger is not None : <EOL> logger = self . server . logger <EOL> else : <EOL> logger = logging . getLogger ( record . name ) <EOL> logger . handle ( record ) <EOL> class LogRecordSocketReceiver ( SocketServer . ThreadingTCPServer ) : <EOL> allow_reuse_address = <NUM_LIT:1> <EOL> def __init__ ( self , logger = None , host = '<STR_LIT:localhost>' , <EOL> port = logging . handlers . DEFAULT_TCP_LOGGING_PORT , <EOL> handler = LogRecordStreamHandler ) : <EOL> SocketServer . ThreadingTCPServer . __init__ ( self , ( host , port ) , handler ) <EOL> self . abort = False <EOL> self . timeout = <NUM_LIT:1> <EOL> self . logger = logger <EOL> def shutdown ( self ) : <EOL> SocketServer . ThreadingTCPServer . shutdown ( self ) <EOL> self . abort = True </s>
