<s> """<STR_LIT>""" <EOL> import select <EOL> import os , sys , string , struct , types , cPickle , cStringIO <EOL> import socket , tempfile , threading , time <EOL> import logging , logging . handlers , logging . config <EOL> from test . test_support import run_with_locale <EOL> BANNER = "<STR_LIT>" <EOL> FINISH_UP = "<STR_LIT>" <EOL> TIMEOUT = <NUM_LIT:10> <EOL> from SocketServer import ThreadingTCPServer , StreamRequestHandler <EOL> class LogRecordStreamHandler ( StreamRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self ) : <EOL> """<STR_LIT>""" <EOL> while <NUM_LIT:1> : <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 : <EOL> raise <EOL> def unPickle ( self , data ) : <EOL> return cPickle . loads ( data ) <EOL> def handleLogRecord ( self , record ) : <EOL> logname = "<STR_LIT>" + record . name <EOL> if record . msg == FINISH_UP : <EOL> self . server . abort = <NUM_LIT:1> <EOL> record . msg = record . msg + "<STR_LIT>" + logname + "<STR_LIT:)>" <EOL> logger = logging . getLogger ( logname ) <EOL> logger . handle ( record ) <EOL> socketDataProcessed = threading . Event ( ) <EOL> class LogRecordSocketReceiver ( ThreadingTCPServer ) : <EOL> """<STR_LIT>""" <EOL> allow_reuse_address = <NUM_LIT:1> <EOL> def __init__ ( self , host = '<STR_LIT:localhost>' , <EOL> port = logging . handlers . DEFAULT_TCP_LOGGING_PORT , <EOL> handler = LogRecordStreamHandler ) : <EOL> ThreadingTCPServer . __init__ ( self , ( host , port ) , handler ) <EOL> self . abort = <NUM_LIT:0> <EOL> self . timeout = <NUM_LIT:1> <EOL> def serve_until_stopped ( self ) : <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> self . socket . setblocking ( <NUM_LIT:0> ) <EOL> while not self . abort : <EOL> rd , wr , ex = select . select ( [ self . socket . fileno ( ) ] , [ ] , [ ] , <EOL> self . timeout ) <EOL> if rd : <EOL> self . handle_request ( ) <EOL> socketDataProcessed . set ( ) <EOL> self . server_close ( ) <EOL> def process_request ( self , request , client_address ) : <EOL> t = threading . Thread ( target = self . finish_request , <EOL> args = ( request , client_address ) ) <EOL> t . start ( ) <EOL> def runTCP ( tcpserver ) : <EOL> tcpserver . serve_until_stopped ( ) <EOL> msgcount = <NUM_LIT:0> <EOL> def nextmessage ( ) : <EOL> global msgcount <EOL> rv = "<STR_LIT>" % msgcount <EOL> msgcount = msgcount + <NUM_LIT:1> <EOL> return rv <EOL> def test0 ( ) : <EOL> ERR = logging . getLogger ( "<STR_LIT>" ) <EOL> ERR . setLevel ( logging . ERROR ) <EOL> INF = logging . getLogger ( "<STR_LIT>" ) <EOL> INF . setLevel ( logging . INFO ) <EOL> INF_ERR = logging . getLogger ( "<STR_LIT>" ) <EOL> INF_ERR . setLevel ( logging . ERROR ) <EOL> DEB = logging . getLogger ( "<STR_LIT>" ) <EOL> DEB . setLevel ( logging . DEBUG ) <EOL> INF_UNDEF = logging . getLogger ( "<STR_LIT>" ) <EOL> INF_ERR_UNDEF = logging . getLogger ( "<STR_LIT>" ) <EOL> UNDEF = logging . getLogger ( "<STR_LIT>" ) <EOL> GRANDCHILD = logging . getLogger ( "<STR_LIT>" ) <EOL> CHILD = logging . getLogger ( "<STR_LIT>" ) <EOL> ERR . log ( logging . FATAL , nextmessage ( ) ) <EOL> ERR . error ( nextmessage ( ) ) <EOL> INF . log ( logging . FATAL , nextmessage ( ) ) <EOL> INF . error ( nextmessage ( ) ) <EOL> INF . warn ( nextmessage ( ) ) <EOL> INF . info ( nextmessage ( ) ) <EOL> INF_UNDEF . log ( logging . FATAL , nextmessage ( ) ) <EOL> INF_UNDEF . error ( nextmessage ( ) ) <EOL> INF_UNDEF . warn ( nextmessage ( ) ) <EOL> INF_UNDEF . info ( nextmessage ( ) ) <EOL> INF_ERR . log ( logging . FATAL , nextmessage ( ) ) <EOL> INF_ERR . error ( nextmessage ( ) ) <EOL> INF_ERR_UNDEF . log ( logging . FATAL , nextmessage ( ) ) <EOL> INF_ERR_UNDEF . error ( nextmessage ( ) ) <EOL> DEB . log ( logging . FATAL , nextmessage ( ) ) <EOL> DEB . error ( nextmessage ( ) ) <EOL> DEB . warn ( nextmessage ( ) ) <EOL> DEB . info ( nextmessage ( ) ) <EOL> DEB . debug ( nextmessage ( ) ) <EOL> UNDEF . log ( logging . FATAL , nextmessage ( ) ) <EOL> UNDEF . error ( nextmessage ( ) ) <EOL> UNDEF . warn ( nextmessage ( ) ) <EOL> UNDEF . info ( nextmessage ( ) ) <EOL> GRANDCHILD . log ( logging . FATAL , nextmessage ( ) ) <EOL> CHILD . log ( logging . FATAL , nextmessage ( ) ) <EOL> ERR . warn ( nextmessage ( ) ) <EOL> ERR . info ( nextmessage ( ) ) <EOL> ERR . debug ( nextmessage ( ) ) <EOL> INF . debug ( nextmessage ( ) ) <EOL> INF_UNDEF . debug ( nextmessage ( ) ) <EOL> INF_ERR . warn ( nextmessage ( ) ) <EOL> INF_ERR . info ( nextmessage ( ) ) <EOL> INF_ERR . debug ( nextmessage ( ) ) <EOL> INF_ERR_UNDEF . warn ( nextmessage ( ) ) <EOL> INF_ERR_UNDEF . info ( nextmessage ( ) ) <EOL> INF_ERR_UNDEF . debug ( nextmessage ( ) ) <EOL> INF . info ( FINISH_UP ) <EOL> SILENT = <NUM_LIT:10> <EOL> TACITURN = <NUM_LIT:9> <EOL> TERSE = <NUM_LIT:8> <EOL> EFFUSIVE = <NUM_LIT:7> <EOL> SOCIABLE = <NUM_LIT:6> <EOL> VERBOSE = <NUM_LIT:5> <EOL> TALKATIVE = <NUM_LIT:4> <EOL> GARRULOUS = <NUM_LIT:3> <EOL> CHATTERBOX = <NUM_LIT:2> <EOL> BORING = <NUM_LIT:1> <EOL> LEVEL_RANGE = range ( BORING , SILENT + <NUM_LIT:1> ) <EOL> my_logging_levels = { <EOL> SILENT : '<STR_LIT>' , <EOL> TACITURN : '<STR_LIT>' , <EOL> TERSE : '<STR_LIT>' , <EOL> EFFUSIVE : '<STR_LIT>' , <EOL> SOCIABLE : '<STR_LIT>' , <EOL> VERBOSE : '<STR_LIT>' , <EOL> TALKATIVE : '<STR_LIT>' , <EOL> GARRULOUS : '<STR_LIT>' , <EOL> CHATTERBOX : '<STR_LIT>' , <EOL> BORING : '<STR_LIT>' , <EOL> } <EOL> class SpecificLevelFilter ( logging . Filter ) : <EOL> def __init__ ( self , lvl ) : <EOL> self . level = lvl <EOL> def filter ( self , record ) : <EOL> return self . level != record . levelno <EOL> class GarrulousFilter ( SpecificLevelFilter ) : <EOL> def __init__ ( self ) : <EOL> SpecificLevelFilter . __init__ ( self , GARRULOUS ) <EOL> class VerySpecificFilter ( logging . Filter ) : <EOL> def filter ( self , record ) : <EOL> return record . levelno not in [ SOCIABLE , TACITURN ] <EOL> def message ( s ) : <EOL> sys . stdout . write ( "<STR_LIT>" % s ) <EOL> SHOULD1 = "<STR_LIT>" <EOL> def test1 ( ) : <EOL> for lvl in my_logging_levels . keys ( ) : <EOL> logging . addLevelName ( lvl , my_logging_levels [ lvl ] ) <EOL> def doLog ( log ) : <EOL> for lvl in LEVEL_RANGE : <EOL> log . log ( lvl , SHOULD1 , logging . getLevelName ( lvl ) ) <EOL> log = logging . getLogger ( "<STR_LIT>" ) <EOL> hdlr = log . handlers [ <NUM_LIT:0> ] <EOL> for lvl in LEVEL_RANGE : <EOL> message ( "<STR_LIT>" % <EOL> logging . getLevelName ( lvl ) ) <EOL> log . setLevel ( lvl ) <EOL> doLog ( log ) <EOL> hdlr . setLevel ( SOCIABLE ) <EOL> message ( "<STR_LIT>" ) <EOL> for lvl in LEVEL_RANGE : <EOL> message ( "<STR_LIT>" % <EOL> logging . getLevelName ( lvl ) ) <EOL> log . setLevel ( lvl ) <EOL> doLog ( log ) <EOL> hdlr . setLevel ( <NUM_LIT:0> ) <EOL> garr = GarrulousFilter ( ) <EOL> hdlr . addFilter ( garr ) <EOL> message ( "<STR_LIT>" ) <EOL> for lvl in LEVEL_RANGE : <EOL> message ( "<STR_LIT>" % <EOL> logging . getLevelName ( lvl ) ) <EOL> log . setLevel ( lvl ) <EOL> doLog ( log ) <EOL> spec = VerySpecificFilter ( ) <EOL> log . addFilter ( spec ) <EOL> message ( "<STR_LIT>" ) <EOL> for lvl in LEVEL_RANGE : <EOL> message ( "<STR_LIT>" % <EOL> logging . getLevelName ( lvl ) ) <EOL> log . setLevel ( lvl ) <EOL> doLog ( log ) <EOL> log . removeFilter ( spec ) <EOL> hdlr . removeFilter ( garr ) <EOL> logging . addLevelName ( logging . DEBUG , "<STR_LIT>" ) <EOL> MSG = "<STR_LIT>" <EOL> def test2 ( ) : <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> sh = logger . handlers [ <NUM_LIT:0> ] <EOL> sh . close ( ) <EOL> logger . removeHandler ( sh ) <EOL> mh = logging . handlers . MemoryHandler ( <NUM_LIT:10> , logging . WARNING , sh ) <EOL> logger . setLevel ( logging . DEBUG ) <EOL> logger . addHandler ( mh ) <EOL> message ( "<STR_LIT>" ) <EOL> logger . debug ( "<STR_LIT>" ) <EOL> message ( "<STR_LIT>" ) <EOL> logger . info ( "<STR_LIT>" ) <EOL> message ( "<STR_LIT>" ) <EOL> logger . warn ( "<STR_LIT>" ) <EOL> for i in xrange ( <NUM_LIT> ) : <EOL> message ( MSG % i ) <EOL> logger . info ( "<STR_LIT>" , i ) <EOL> mh . close ( ) <EOL> logger . removeHandler ( mh ) <EOL> logger . addHandler ( sh ) <EOL> FILTER = "<STR_LIT>" <EOL> def doLog3 ( ) : <EOL> logging . getLogger ( "<STR_LIT:a>" ) . info ( "<STR_LIT>" ) <EOL> logging . getLogger ( "<STR_LIT>" ) . info ( "<STR_LIT>" ) <EOL> logging . getLogger ( "<STR_LIT>" ) . info ( "<STR_LIT>" ) <EOL> logging . getLogger ( "<STR_LIT>" ) . info ( "<STR_LIT>" ) <EOL> logging . getLogger ( "<STR_LIT>" ) . info ( "<STR_LIT>" ) <EOL> logging . getLogger ( "<STR_LIT>" ) . info ( "<STR_LIT>" ) <EOL> logging . getLogger ( "<STR_LIT:b>" ) . info ( "<STR_LIT>" ) <EOL> logging . getLogger ( "<STR_LIT>" ) . info ( "<STR_LIT>" ) <EOL> logging . getLogger ( "<STR_LIT>" ) . info ( "<STR_LIT>" ) <EOL> logging . getLogger ( "<STR_LIT>" ) . info ( "<STR_LIT>" ) <EOL> def test3 ( ) : <EOL> root = logging . getLogger ( ) <EOL> root . setLevel ( logging . DEBUG ) <EOL> hand = root . handlers [ <NUM_LIT:0> ] <EOL> message ( "<STR_LIT>" ) <EOL> doLog3 ( ) <EOL> message ( "<STR_LIT>" % FILTER ) <EOL> filt = logging . Filter ( FILTER ) <EOL> hand . addFilter ( filt ) <EOL> doLog3 ( ) <EOL> hand . removeFilter ( filt ) <EOL> config0 = """<STR_LIT>""" <EOL> config1 = """<STR_LIT>""" <EOL> config2 = string . replace ( config1 , "<STR_LIT>" , "<STR_LIT>" ) <EOL> config3 = string . replace ( <EOL> config1 , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def test4 ( ) : <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> conf = globals ( ) [ '<STR_LIT>' % i ] <EOL> sys . stdout . write ( '<STR_LIT>' % i ) <EOL> loggerDict = logging . getLogger ( ) . manager . loggerDict <EOL> logging . _acquireLock ( ) <EOL> try : <EOL> saved_handlers = logging . _handlers . copy ( ) <EOL> saved_handler_list = logging . _handlerList [ : ] <EOL> saved_loggers = loggerDict . copy ( ) <EOL> finally : <EOL> logging . _releaseLock ( ) <EOL> try : <EOL> fn = tempfile . mktemp ( "<STR_LIT>" ) <EOL> f = open ( fn , "<STR_LIT:w>" ) <EOL> f . write ( conf ) <EOL> f . close ( ) <EOL> try : <EOL> logging . config . fileConfig ( fn ) <EOL> logging . config . fileConfig ( fn ) <EOL> except : <EOL> t = sys . exc_info ( ) [ <NUM_LIT:0> ] <EOL> message ( str ( t ) ) <EOL> else : <EOL> message ( '<STR_LIT>' ) <EOL> os . remove ( fn ) <EOL> finally : <EOL> logging . _acquireLock ( ) <EOL> try : <EOL> logging . _handlers . clear ( ) <EOL> logging . _handlers . update ( saved_handlers ) <EOL> logging . _handlerList [ : ] = saved_handler_list <EOL> loggerDict = logging . getLogger ( ) . manager . loggerDict <EOL> loggerDict . clear ( ) <EOL> loggerDict . update ( saved_loggers ) <EOL> finally : <EOL> logging . _releaseLock ( ) <EOL> test5_config = """<STR_LIT>""" <EOL> class FriendlyFormatter ( logging . Formatter ) : <EOL> def formatException ( self , ei ) : <EOL> return "<STR_LIT>" % str ( ei [ <NUM_LIT:0> ] ) <EOL> def test5 ( ) : <EOL> loggerDict = logging . getLogger ( ) . manager . loggerDict <EOL> logging . _acquireLock ( ) <EOL> try : <EOL> saved_handlers = logging . _handlers . copy ( ) <EOL> saved_handler_list = logging . _handlerList [ : ] <EOL> saved_loggers = loggerDict . copy ( ) <EOL> finally : <EOL> logging . _releaseLock ( ) <EOL> try : <EOL> fn = tempfile . mktemp ( "<STR_LIT>" ) <EOL> f = open ( fn , "<STR_LIT:w>" ) <EOL> f . write ( test5_config ) <EOL> f . close ( ) <EOL> logging . config . fileConfig ( fn ) <EOL> try : <EOL> raise KeyError <EOL> except KeyError : <EOL> logging . exception ( "<STR_LIT>" ) <EOL> os . remove ( fn ) <EOL> hdlr = logging . getLogger ( ) . handlers [ <NUM_LIT:0> ] <EOL> logging . getLogger ( ) . handlers . remove ( hdlr ) <EOL> finally : <EOL> logging . _acquireLock ( ) <EOL> try : <EOL> logging . _handlers . clear ( ) <EOL> logging . _handlers . update ( saved_handlers ) <EOL> logging . _handlerList [ : ] = saved_handler_list <EOL> loggerDict = logging . getLogger ( ) . manager . loggerDict <EOL> loggerDict . clear ( ) <EOL> loggerDict . update ( saved_loggers ) <EOL> finally : <EOL> logging . _releaseLock ( ) <EOL> def banner ( nm , typ ) : <EOL> sep = BANNER % ( nm , typ ) <EOL> sys . stdout . write ( sep ) <EOL> sys . stdout . flush ( ) <EOL> def test_main_inner ( ) : <EOL> rootLogger = logging . getLogger ( "<STR_LIT>" ) <EOL> rootLogger . setLevel ( logging . DEBUG ) <EOL> hdlr = logging . StreamHandler ( sys . stdout ) <EOL> fmt = logging . Formatter ( logging . BASIC_FORMAT ) <EOL> hdlr . setFormatter ( fmt ) <EOL> rootLogger . addHandler ( hdlr ) <EOL> port = logging . handlers . DEFAULT_TCP_LOGGING_PORT <EOL> while port < logging . handlers . DEFAULT_TCP_LOGGING_PORT + <NUM_LIT:100> : <EOL> try : <EOL> tcpserver = LogRecordSocketReceiver ( port = port ) <EOL> except socket . error : <EOL> port += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> else : <EOL> raise ImportError , "<STR_LIT>" <EOL> shdlr = logging . handlers . SocketHandler ( '<STR_LIT:localhost>' , port ) <EOL> sockOut = cStringIO . StringIO ( ) <EOL> sockLogger = logging . getLogger ( "<STR_LIT>" ) <EOL> sockLogger . setLevel ( logging . DEBUG ) <EOL> sockhdlr = logging . StreamHandler ( sockOut ) <EOL> sockhdlr . setFormatter ( logging . Formatter ( <EOL> "<STR_LIT>" ) ) <EOL> sockLogger . addHandler ( sockhdlr ) <EOL> sockLogger . propagate = <NUM_LIT:0> <EOL> threads = [ ] <EOL> threads . append ( threading . Thread ( target = runTCP , args = ( tcpserver , ) ) ) <EOL> for thread in threads : <EOL> thread . start ( ) <EOL> try : <EOL> banner ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> rootLogger . addHandler ( shdlr ) <EOL> test0 ( ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> shdlr . close ( ) <EOL> rootLogger . removeHandler ( shdlr ) <EOL> banner ( "<STR_LIT>" , "<STR_LIT:end>" ) <EOL> for t in range ( <NUM_LIT:1> , <NUM_LIT:6> ) : <EOL> banner ( "<STR_LIT>" % t , "<STR_LIT>" ) <EOL> globals ( ) [ '<STR_LIT>' % t ] ( ) <EOL> banner ( "<STR_LIT>" % t , "<STR_LIT:end>" ) <EOL> finally : <EOL> socketDataProcessed . wait ( ) <EOL> tcpserver . abort = <NUM_LIT:1> <EOL> for thread in threads : <EOL> thread . join ( <NUM_LIT> ) <EOL> banner ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> sys . stdout . write ( sockOut . getvalue ( ) ) <EOL> sockOut . close ( ) <EOL> sockLogger . removeHandler ( sockhdlr ) <EOL> sockhdlr . close ( ) <EOL> banner ( "<STR_LIT>" , "<STR_LIT:end>" ) <EOL> sys . stdout . flush ( ) <EOL> try : <EOL> hdlr . close ( ) <EOL> except : <EOL> pass <EOL> rootLogger . removeHandler ( hdlr ) <EOL> @ run_with_locale ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_main ( ) : <EOL> root_logger = logging . getLogger ( "<STR_LIT>" ) <EOL> original_logging_level = root_logger . getEffectiveLevel ( ) <EOL> try : <EOL> test_main_inner ( ) <EOL> finally : <EOL> root_logger . setLevel ( original_logging_level ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . stdout . write ( "<STR_LIT>" ) <EOL> test_main ( ) </s>
