<s> """<STR_LIT>""" <EOL> import pickle <EOL> import select <EOL> import socket <EOL> import struct <EOL> import logging <EOL> import threading <EOL> from logging import DEBUG , INFO , WARNING , ERROR , CRITICAL <EOL> from logging . handlers import SocketHandler , DEFAULT_TCP_LOGGING_PORT , DEFAULT_UDP_LOGGING_PORT <EOL> from socketserver import ( ThreadingUDPServer , DatagramRequestHandler , <EOL> ThreadingTCPServer , StreamRequestHandler ) <EOL> from stringparser import Parser <EOL> class _LogRecord ( logging . LogRecord ) : <EOL> def getMessage ( self ) : <EOL> """<STR_LIT>""" <EOL> msg = str ( self . msg ) <EOL> if self . args : <EOL> msg = msg . format ( * self . args ) <EOL> return msg <EOL> def _makeRecord ( name , level , fn , lno , msg , args , exc_info , <EOL> func = None , extra = None , sinfo = None ) : <EOL> """<STR_LIT>""" <EOL> rv = _LogRecord ( name , level , fn , lno , msg , args , exc_info , func , <EOL> sinfo ) <EOL> if extra is not None : <EOL> for key in extra : <EOL> if ( key in [ "<STR_LIT:message>" , "<STR_LIT>" ] ) or ( key in rv . __dict__ ) : <EOL> raise KeyError ( "<STR_LIT>" % key ) <EOL> rv . __dict__ [ key ] = extra [ key ] <EOL> return rv <EOL> def get_logger ( name , add_NullHandler = True , patch_makeRecord = True ) : <EOL> """<STR_LIT>""" <EOL> logger = logging . getLogger ( name ) <EOL> if add_NullHandler : <EOL> logger . addHandler ( logging . NullHandler ( ) ) <EOL> if patch_makeRecord : <EOL> logger . makeRecord = _makeRecord <EOL> return logger <EOL> LOGGER = get_logger ( '<STR_LIT>' ) <EOL> class ColorizingFormatter ( logging . Formatter ) : <EOL> """<STR_LIT>""" <EOL> SPLIT_COLOR = Parser ( '<STR_LIT>' ) <EOL> SCHEME = { '<STR_LIT>' : { logging . DEBUG : '<STR_LIT>' , <EOL> logging . INFO : '<STR_LIT>' , <EOL> logging . WARNING : '<STR_LIT>' , <EOL> logging . ERROR : '<STR_LIT>' , <EOL> logging . CRITICAL : '<STR_LIT>' } <EOL> } <EOL> @ classmethod <EOL> def add_color_schemes ( cls , style , fore , back ) : <EOL> cls . format = cls . color_format <EOL> cls . SCHEME . update ( bright = { DEBUG : style . NORMAL , <EOL> INFO : style . NORMAL , <EOL> WARNING : style . BRIGHT , <EOL> ERROR : style . BRIGHT , <EOL> CRITICAL : style . BRIGHT } , <EOL> simple = { DEBUG : fore . BLUE + style . BRIGHT , <EOL> INFO : back . WHITE + fore . BLACK , <EOL> WARNING : fore . YELLOW + style . BRIGHT , <EOL> ERROR : fore . RED + style . BRIGHT , <EOL> CRITICAL : back . RED + fore . WHITE + style . BRIGHT } , <EOL> whitebg = { DEBUG : fore . BLUE + style . BRIGHT , <EOL> INFO : back . WHITE + fore . BLACK , <EOL> WARNING : fore . YELLOW + style . BRIGHT , <EOL> ERROR : fore . RED + style . BRIGHT , <EOL> CRITICAL : back . RED + fore . WHITE + style . BRIGHT } , <EOL> blackbg = { DEBUG : fore . BLUE + style . BRIGHT , <EOL> INFO : fore . GREEN , <EOL> WARNING : fore . YELLOW + style . BRIGHT , <EOL> ERROR : fore . RED + style . BRIGHT , <EOL> CRITICAL : back . RED + fore . WHITE + style . BRIGHT } <EOL> ) <EOL> def __init__ ( self , fmt , datefmt = '<STR_LIT>' , style = '<STR_LIT:%>' , scheme = '<STR_LIT>' ) : <EOL> super ( ) . __init__ ( fmt , datefmt , style ) <EOL> self . scheme = scheme <EOL> @ property <EOL> def scheme ( self ) : <EOL> return self . scheme <EOL> @ scheme . setter <EOL> def scheme ( self , value ) : <EOL> if isinstance ( value , str ) : <EOL> self . _scheme = self . SCHEME [ value ] <EOL> else : <EOL> self . _scheme = value <EOL> def colorize ( self , message , record ) : <EOL> """<STR_LIT>""" <EOL> if record . levelno in self . _scheme : <EOL> color = self . _scheme [ record . levelno ] <EOL> return color + message + Style . RESET_ALL <EOL> return message <EOL> def color_format ( self , record ) : <EOL> """<STR_LIT>""" <EOL> message = super ( ) . format ( record ) <EOL> parts = message . split ( '<STR_LIT:\n>' , <NUM_LIT:1> ) <EOL> if '<STR_LIT>' in parts [ <NUM_LIT:0> ] and '<STR_LIT>' in parts [ <NUM_LIT:0> ] : <EOL> bef , dur , aft = self . SPLIT_COLOR ( parts [ <NUM_LIT:0> ] ) <EOL> parts [ <NUM_LIT:0> ] = bef + self . colorize ( dur , record ) + aft <EOL> message = '<STR_LIT:\n>' . join ( parts ) <EOL> return message <EOL> def init_colorama ( ) : <EOL> try : <EOL> from colorama import Fore , Back , Style , init as colorama_init <EOL> colorama_init ( ) <EOL> colorama = True <EOL> DEFAULT_FMT = Style . NORMAL + '<STR_LIT>' + Style . RESET_ALL + '<STR_LIT>' <EOL> ColorizingFormatter . add_color_schemes ( Style , Fore , Back ) <EOL> except Exception as e : <EOL> LOGGER . info ( '<STR_LIT>' , e ) <EOL> colorama = False <EOL> DEFAULT_FMT = '<STR_LIT>' <EOL> return colorama , DEFAULT_FMT <EOL> colorama , DEFAULT_FMT = init_colorama ( ) <EOL> class BaseServer ( object ) : <EOL> """<STR_LIT>""" <EOL> allow_reuse_address = True <EOL> def __init__ ( self , handler , timeout ) : <EOL> self . _record_handler = handler <EOL> self . _stop = threading . Event ( ) <EOL> self . timeout = timeout <EOL> def handle_record ( self , record ) : <EOL> self . _record_handler ( record ) <EOL> def serve_until_stopped ( self ) : <EOL> while not self . _stop . isSet ( ) : <EOL> rd , wr , ex = self . select ( ) <EOL> if rd : <EOL> self . handle_request ( ) <EOL> self . server_close ( ) <EOL> def select ( self ) : <EOL> return select . select ( [ self . socket . fileno ( ) ] , [ ] , [ ] , self . timeout ) <EOL> def stop ( self ) : <EOL> self . _stop . set ( ) <EOL> class LogRecordStreamHandler ( StreamRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self ) : <EOL> """<STR_LIT>""" <EOL> while True : <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 = pickle . loads ( chunk ) <EOL> record = _LogRecord ( None , None , "<STR_LIT>" , <NUM_LIT:0> , "<STR_LIT>" , ( ) , None , None ) <EOL> record . __dict__ . update ( obj ) <EOL> self . server . handle_record ( record ) <EOL> except socket . error as e : <EOL> if not isinstance ( e . args , tuple ) : <EOL> raise e <EOL> else : <EOL> if e . args [ <NUM_LIT:0> ] != logging . RESET_ERROR : <EOL> raise e <EOL> break <EOL> class LoggingTCPServer ( ThreadingTCPServer , BaseServer ) : <EOL> """<STR_LIT>""" <EOL> allow_reuse_address = True <EOL> def __init__ ( self , addr , handler , timeout = <NUM_LIT:1> ) : <EOL> ThreadingTCPServer . __init__ ( self , addr , LogRecordStreamHandler ) <EOL> BaseServer . __init__ ( self , handler , timeout ) <EOL> self . socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> class LogRecordDatagramHandler ( DatagramRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self ) : <EOL> chunk = self . packet <EOL> slen = struct . unpack ( "<STR_LIT>" , chunk [ : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] <EOL> chunk = chunk [ <NUM_LIT:4> : ] <EOL> assert len ( chunk ) == slen <EOL> obj = pickle . loads ( chunk ) <EOL> record = _LogRecord ( None , None , "<STR_LIT>" , <NUM_LIT:0> , "<STR_LIT>" , ( ) , None , None ) <EOL> record . __dict__ . update ( obj ) <EOL> self . server . handle_record ( record ) <EOL> def finish ( self ) : <EOL> pass <EOL> class LoggingUDPServer ( ThreadingUDPServer , BaseServer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , addr , handler , timeout = <NUM_LIT:1> ) : <EOL> ThreadingUDPServer . __init__ ( self , addr , LogRecordDatagramHandler ) <EOL> BaseServer . __init__ ( self , handler , timeout ) <EOL> class SocketListener ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tcphost , udphost ) : <EOL> self . tcp_addr = get_address ( tcphost ) <EOL> self . udp_addr = get_address ( udphost , DEFAULT_UDP_LOGGING_PORT ) <EOL> self . start ( ) <EOL> def start ( self ) : <EOL> self . _lock = threading . RLock ( ) <EOL> s = LoggingTCPServer ( self . tcp_addr , self . on_record , <NUM_LIT:1> ) <EOL> self . tcp_server = s <EOL> self . tcp_thread = t = threading . Thread ( target = s . serve_until_stopped ) <EOL> t . setDaemon ( True ) <EOL> t . start ( ) <EOL> s = LoggingUDPServer ( self . udp_addr , self . on_record , <NUM_LIT:1> ) <EOL> self . udp_server = s <EOL> self . udp_thread = t = threading . Thread ( target = s . serve_until_stopped ) <EOL> t . setDaemon ( True ) <EOL> t . start ( ) <EOL> def stop ( self ) : <EOL> self . tcp_server . stop ( ) <EOL> self . tcp_thread . join ( ) <EOL> self . udp_server . stop ( ) <EOL> self . udp_thread . join ( ) <EOL> def on_record ( self , record ) : <EOL> pass <EOL> def log_to_socket ( level = logging . INFO , host = '<STR_LIT:localhost>' , <EOL> port = DEFAULT_TCP_LOGGING_PORT ) : <EOL> """<STR_LIT>""" <EOL> handler = SocketHandler ( host , port ) <EOL> handler . setLevel ( level ) <EOL> LOGGER . addHandler ( handler ) <EOL> if LOGGER . getEffectiveLevel ( ) > level : <EOL> LOGGER . setLevel ( level ) <EOL> return LOGGER <EOL> def log_to_screen ( level = logging . INFO , scheme = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> handler = logging . StreamHandler ( ) <EOL> handler . setLevel ( level ) <EOL> if not colorama : <EOL> scheme = '<STR_LIT>' <EOL> handler . setFormatter ( ColorizingFormatter ( fmt = DEFAULT_FMT , scheme = scheme , style = '<STR_LIT:{>' ) ) <EOL> LOGGER . addHandler ( handler ) <EOL> if LOGGER . getEffectiveLevel ( ) > level : <EOL> LOGGER . setLevel ( level ) <EOL> return LOGGER <EOL> def get_address ( value , default_port = DEFAULT_TCP_LOGGING_PORT ) : <EOL> """<STR_LIT>""" <EOL> value = value . strip ( ) <EOL> if '<STR_LIT::>' not in value [ : - <NUM_LIT:1> ] : <EOL> result = value , default_port <EOL> else : <EOL> h , p = value . split ( '<STR_LIT::>' ) <EOL> result = h , int ( p ) <EOL> return result </s>
