<s> import os <EOL> import logging <EOL> import argparse <EOL> from memsql_loader . util import paths <EOL> _debug = False <EOL> _disable_stdout = False <EOL> _log_path = os . path . join ( paths . get_data_dir ( ) , '<STR_LIT>' ) <EOL> _file_handler = None <EOL> _stream_handler = None <EOL> def setup ( log_path = None , stdout_enabled = True ) : <EOL> global _stream_handler , _file_handler <EOL> if stdout_enabled : <EOL> _stream_handler = logging . StreamHandler ( ) <EOL> format_str = "<STR_LIT>" <EOL> _stream_handler . setFormatter ( logging . Formatter ( format_str ) ) <EOL> else : <EOL> _stream_handler = False <EOL> if log_path is not None : <EOL> try : <EOL> with open ( log_path , '<STR_LIT:a>' ) as logfile : <EOL> logfile . write ( '<STR_LIT>' % os . getpid ( ) ) <EOL> _file_handler = logging . FileHandler ( filename = log_path ) <EOL> format_str = "<STR_LIT>" <EOL> formatter = logging . Formatter ( format_str ) <EOL> _file_handler . setFormatter ( formatter ) <EOL> except IOError : <EOL> pass <EOL> def update_verbosity ( debug = False , extra_verbose = False ) : <EOL> stdout_level = logging . DEBUG if ( debug or extra_verbose ) else logging . INFO <EOL> if _stream_handler : <EOL> _stream_handler . setLevel ( stdout_level ) <EOL> if extra_verbose : <EOL> logging . setLoggerClass ( Logger ) <EOL> class _SetDebug ( argparse . Action ) : <EOL> def __call__ ( self , parser , namespace , value , option_string = None ) : <EOL> global _debug <EOL> _debug = True <EOL> class _SetStdout ( argparse . Action ) : <EOL> def __call__ ( self , parser , namespace , value , option_string = None ) : <EOL> global _disable_stdout <EOL> _disable_stdout = True <EOL> class _SetLogPath ( argparse . Action ) : <EOL> def __call__ ( self , parser , namespace , value , option_string = None ) : <EOL> global _log_path <EOL> _log_path = value <EOL> setattr ( namespace , self . dest , _log_path ) <EOL> def configure ( parser ) : <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> default = False , <EOL> action = _SetDebug , <EOL> nargs = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = False , <EOL> action = _SetStdout , <EOL> nargs = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = _log_path , <EOL> action = _SetLogPath , <EOL> help = "<STR_LIT>" ) <EOL> def get_logger ( name , level = None ) : <EOL> return Logger ( name , level or logging . DEBUG ) <EOL> class Formatter ( logging . Formatter ) : <EOL> def format ( self , record ) : <EOL> msg = super ( Formatter , self ) . format ( record ) <EOL> if record . levelno == logging . INFO : <EOL> msg = '<STR_LIT>' + msg <EOL> elif record . levelno in ( logging . ERROR , logging . CRITICAL , logging . FATAL , logging . WARN ) : <EOL> msg = '<STR_LIT>' + msg <EOL> elif record . levelno == logging . DEBUG : <EOL> msg = '<STR_LIT>' + msg <EOL> return msg <EOL> class Logger ( logging . Logger ) : <EOL> def __init__ ( self , name , level = logging . DEBUG ) : <EOL> logging . Logger . __init__ ( self , name , level = level ) <EOL> self . propagate = False <EOL> if _stream_handler is None : <EOL> setup ( log_path = _log_path , stdout_enabled = ( not _disable_stdout ) ) <EOL> update_verbosity ( debug = _debug ) <EOL> if _stream_handler is not False : <EOL> self . addHandler ( _stream_handler ) <EOL> if _file_handler is not None : <EOL> self . addHandler ( _file_handler ) </s>
