<s> """<STR_LIT>""" <EOL> from __pyjamas__ import debugger <EOL> from pyjamas . HTTPRequest import HTTPRequest <EOL> import logging <EOL> import urllib <EOL> try : <EOL> import codecs <EOL> except ImportError : <EOL> codecs = None <EOL> DEFAULT_TCP_LOGGING_PORT = <NUM_LIT> <EOL> DEFAULT_UDP_LOGGING_PORT = <NUM_LIT> <EOL> DEFAULT_HTTP_LOGGING_PORT = <NUM_LIT> <EOL> DEFAULT_SOAP_LOGGING_PORT = <NUM_LIT> <EOL> SYSLOG_UDP_PORT = <NUM_LIT> <EOL> _MIDNIGHT = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> class HTTPHandler ( logging . Handler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , url , method = "<STR_LIT:GET>" ) : <EOL> """<STR_LIT>""" <EOL> logging . Handler . __init__ ( self ) <EOL> method = method . upper ( ) <EOL> if method not in [ "<STR_LIT:GET>" , "<STR_LIT:POST>" ] : <EOL> raise ValueError , "<STR_LIT>" <EOL> self . host = host <EOL> self . url = url <EOL> self . method = method <EOL> self . httpRequest = HTTPRequest ( ) <EOL> def onCompletion ( self , response ) : <EOL> pass <EOL> def onError ( self , repsonse , status ) : <EOL> pass <EOL> def mapLogRecord ( self , record ) : <EOL> """<STR_LIT>""" <EOL> return record . toDict ( ) <EOL> def emit ( self , record ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> host = self . host <EOL> url = self . url <EOL> data = urllib . urlencode ( self . mapLogRecord ( record ) ) <EOL> if self . method == "<STR_LIT:GET>" : <EOL> if ( url . find ( '<STR_LIT:?>' ) >= <NUM_LIT:0> ) : <EOL> sep = '<STR_LIT:&>' <EOL> else : <EOL> sep = '<STR_LIT:?>' <EOL> url = url + "<STR_LIT>" % ( sep , data ) <EOL> i = host . find ( "<STR_LIT::>" ) <EOL> if i >= <NUM_LIT:0> : <EOL> host = host [ : i ] <EOL> headers = { } <EOL> headers [ "<STR_LIT>" ] = host <EOL> if self . method == "<STR_LIT:POST>" : <EOL> self . httpRequest . asyncPost ( url , data , self , <EOL> content_type = "<STR_LIT>" , <EOL> headers = headers ) <EOL> else : <EOL> self . httpRequest . asyncGet ( url , self ) <EOL> except : <EOL> self . handleError ( record ) <EOL> class BufferingHandler ( logging . Handler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , capacity ) : <EOL> """<STR_LIT>""" <EOL> logging . Handler . __init__ ( self ) <EOL> self . capacity = capacity <EOL> self . buffer = [ ] <EOL> def shouldFlush ( self , record ) : <EOL> """<STR_LIT>""" <EOL> return ( len ( self . buffer ) >= self . capacity ) <EOL> def emit ( self , record ) : <EOL> """<STR_LIT>""" <EOL> self . buffer . append ( record ) <EOL> if self . shouldFlush ( record ) : <EOL> self . flush ( ) <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> self . buffer = [ ] <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . flush ( ) <EOL> logging . Handler . close ( self ) <EOL> class MemoryHandler ( BufferingHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , capacity , flushLevel = logging . ERROR , target = None ) : <EOL> """<STR_LIT>""" <EOL> BufferingHandler . __init__ ( self , capacity ) <EOL> self . flushLevel = flushLevel <EOL> self . target = target <EOL> def shouldFlush ( self , record ) : <EOL> """<STR_LIT>""" <EOL> return ( len ( self . buffer ) >= self . capacity ) or ( record . levelno >= self . flushLevel ) <EOL> def setTarget ( self , target ) : <EOL> """<STR_LIT>""" <EOL> self . target = target <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . target : <EOL> for record in self . buffer : <EOL> self . target . handle ( record ) <EOL> self . buffer = [ ] <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . flush ( ) <EOL> self . target = None <EOL> BufferingHandler . close ( self ) </s>
