<s> from __future__ import print_function , unicode_literals , absolute_import <EOL> import socket , logging , logging . handlers <EOL> from pyreadline . unicode_helper import ensure_str <EOL> host = "<STR_LIT:localhost>" <EOL> port = logging . handlers . DEFAULT_TCP_LOGGING_PORT <EOL> pyreadline_logger = logging . getLogger ( '<STR_LIT>' ) <EOL> pyreadline_logger . setLevel ( logging . DEBUG ) <EOL> pyreadline_logger . propagate = False <EOL> formatter = logging . Formatter ( str ( '<STR_LIT>' ) ) <EOL> file_handler = None <EOL> class NULLHandler ( logging . Handler ) : <EOL> def emit ( self , s ) : <EOL> pass <EOL> class SocketStream ( object ) : <EOL> def __init__ ( self , host , port ) : <EOL> self . logsocket = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM ) <EOL> def write ( self , s ) : <EOL> self . logsocket . sendto ( ensure_str ( s ) , ( host , port ) ) <EOL> def flush ( self ) : <EOL> pass <EOL> def close ( self ) : <EOL> pass <EOL> socket_handler = None <EOL> pyreadline_logger . addHandler ( NULLHandler ( ) ) <EOL> def start_socket_log ( ) : <EOL> global socket_handler <EOL> socket_handler = logging . StreamHandler ( SocketStream ( host , port ) ) <EOL> socket_handler . setFormatter ( formatter ) <EOL> pyreadline_logger . addHandler ( socket_handler ) <EOL> def stop_socket_log ( ) : <EOL> global socket_handler <EOL> if socket_handler : <EOL> pyreadline_logger . removeHandler ( socket_handler ) <EOL> socket_handler = None <EOL> def start_file_log ( filename ) : <EOL> global file_handler <EOL> file_handler = logging . FileHandler ( filename , "<STR_LIT:w>" ) <EOL> pyreadline_logger . addHandler ( file_handler ) <EOL> def stop_file_log ( ) : <EOL> global file_handler <EOL> if file_handler : <EOL> pyreadline_logger . removeHandler ( file_handler ) <EOL> file_handler . close ( ) <EOL> file_handler = None <EOL> def stop_logging ( ) : <EOL> log ( "<STR_LIT>" ) <EOL> stop_file_log ( ) <EOL> stop_socket_log ( ) <EOL> def log ( s ) : <EOL> s = ensure_str ( s ) <EOL> pyreadline_logger . debug ( s ) </s>
