<s> import functools <EOL> from oslo_config import cfg <EOL> from oslo_log import handlers <EOL> from oslo_log import log as oslogging <EOL> from rally . common . i18n import _ <EOL> log = __import__ ( "<STR_LIT>" ) <EOL> DEBUG_OPTS = [ cfg . BoolOpt ( <EOL> "<STR_LIT>" , <EOL> default = False , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) ] <EOL> CONF = cfg . CONF <EOL> CONF . register_cli_opts ( DEBUG_OPTS ) <EOL> oslogging . register_options ( CONF ) <EOL> log . RDEBUG = log . DEBUG + <NUM_LIT:1> <EOL> log . addLevelName ( log . RDEBUG , "<STR_LIT>" ) <EOL> CRITICAL = log . CRITICAL <EOL> DEBUG = log . DEBUG <EOL> ERROR = log . ERROR <EOL> FATAL = log . FATAL <EOL> INFO = log . INFO <EOL> NOTSET = log . NOTSET <EOL> RDEBUG = log . RDEBUG <EOL> WARN = log . WARN <EOL> WARNING = log . WARNING <EOL> def setup ( product_name , version = "<STR_LIT>" ) : <EOL> dbg_color = handlers . ColorHandler . LEVEL_COLORS [ log . DEBUG ] <EOL> handlers . ColorHandler . LEVEL_COLORS [ log . RDEBUG ] = dbg_color <EOL> oslogging . setup ( CONF , product_name , version ) <EOL> if CONF . rally_debug : <EOL> oslogging . getLogger ( <EOL> project = product_name ) . logger . setLevel ( log . RDEBUG ) <EOL> class RallyContextAdapter ( oslogging . KeywordArgumentAdapter ) : <EOL> def debug ( self , msg , * args , ** kwargs ) : <EOL> self . log ( log . RDEBUG , msg , * args , ** kwargs ) <EOL> def getLogger ( name = "<STR_LIT>" , version = "<STR_LIT>" ) : <EOL> if name not in oslogging . _loggers : <EOL> oslogging . _loggers [ name ] = RallyContextAdapter ( log . getLogger ( name ) , <EOL> { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:version>" : version } ) <EOL> return oslogging . _loggers [ name ] <EOL> LOG = getLogger ( __name__ ) <EOL> class ExceptionLogger ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , logger , warn = None ) : <EOL> self . logger = logger <EOL> self . warn = warn <EOL> self . exception = None <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , type_ , value , traceback ) : <EOL> if value : <EOL> self . exception = value <EOL> if self . warn : <EOL> self . logger . warning ( self . warn ) <EOL> self . logger . debug ( value ) <EOL> if is_debug ( ) : <EOL> self . logger . exception ( value ) <EOL> return True <EOL> class CatcherHandler ( log . handlers . BufferingHandler ) : <EOL> def __init__ ( self ) : <EOL> log . handlers . BufferingHandler . __init__ ( self , <NUM_LIT:0> ) <EOL> def shouldFlush ( self ) : <EOL> return False <EOL> def emit ( self , record ) : <EOL> self . buffer . append ( record ) <EOL> class LogCatcher ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , logger ) : <EOL> self . logger = getattr ( logger , "<STR_LIT>" , logger ) <EOL> self . handler = CatcherHandler ( ) <EOL> def __enter__ ( self ) : <EOL> self . logger . addHandler ( self . handler ) <EOL> return self <EOL> def __exit__ ( self , type_ , value , traceback ) : <EOL> self . logger . removeHandler ( self . handler ) <EOL> def assertInLogs ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> in_logs = [ record . msg <EOL> for record in self . handler . buffer if msg in record . msg ] <EOL> if not in_logs : <EOL> raise AssertionError ( "<STR_LIT>" % msg ) <EOL> return in_logs <EOL> def fetchLogRecords ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . handler . buffer <EOL> def fetchLogs ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ record . msg for record in self . handler . buffer ] <EOL> def _log_wrapper ( obj , log_function , msg , ** kw ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( f ) : <EOL> @ functools . wraps ( f ) <EOL> def wrapper ( self , * args , ** kwargs ) : <EOL> params = { "<STR_LIT>" : msg % kw , "<STR_LIT>" : obj . title ( ) , <EOL> "<STR_LIT>" : getattr ( self , obj ) [ "<STR_LIT>" ] } <EOL> log_function ( _ ( "<STR_LIT>" ) % <EOL> params ) <EOL> result = f ( self , * args , ** kwargs ) <EOL> log_function ( _ ( "<STR_LIT>" ) % <EOL> params ) <EOL> return result <EOL> return wrapper <EOL> return decorator <EOL> def log_task_wrapper ( log_function , msg , ** kw ) : <EOL> return _log_wrapper ( "<STR_LIT>" , log_function , msg , ** kw ) <EOL> def log_deploy_wrapper ( log_function , msg , ** kw ) : <EOL> return _log_wrapper ( "<STR_LIT>" , log_function , msg , ** kw ) <EOL> def log_verification_wrapper ( log_function , msg , ** kw ) : <EOL> return _log_wrapper ( "<STR_LIT>" , log_function , msg , ** kw ) <EOL> def log_deprecated ( message , rally_version , log_function = None , once = False ) : <EOL> """<STR_LIT>""" <EOL> log_function = log_function or LOG . warning <EOL> def decorator ( f ) : <EOL> @ functools . wraps ( f ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> if ( not once ) or ( not getattr ( f , "<STR_LIT>" , False ) ) : <EOL> log_function ( "<STR_LIT>" <EOL> "<STR_LIT>" % { "<STR_LIT>" : message , <EOL> "<STR_LIT:version>" : rally_version , <EOL> "<STR_LIT>" : f . __name__ } ) <EOL> setattr ( f , "<STR_LIT>" , once ) <EOL> return f ( * args , ** kwargs ) <EOL> return wrapper <EOL> return decorator <EOL> def log_deprecated_args ( message , rally_version , deprecated_args , <EOL> log_function = None , once = False ) : <EOL> """<STR_LIT>""" <EOL> log_function = log_function or LOG . warning <EOL> def decorator ( f ) : <EOL> @ functools . wraps ( f ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> if ( not once ) or ( not getattr ( f , "<STR_LIT>" , False ) ) : <EOL> deprecated = "<STR_LIT:U+002CU+0020>" . join ( [ <EOL> "<STR_LIT>" % x for x in deprecated_args if x in kwargs ] ) <EOL> if deprecated : <EOL> log_function ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> { "<STR_LIT>" : message , "<STR_LIT:version>" : rally_version , <EOL> "<STR_LIT:args>" : deprecated } ) <EOL> setattr ( f , "<STR_LIT>" , once ) <EOL> result = f ( * args , ** kwargs ) <EOL> return result <EOL> return wrapper <EOL> return decorator <EOL> def is_debug ( ) : <EOL> return CONF . debug or CONF . rally_debug </s>
