<s> from __future__ import unicode_literals <EOL> import logging <EOL> import sys <EOL> import threading <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def black ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def red ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def green ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def yellow ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def blue ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def magenta ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def cyan ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def white ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def bright_black ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def bright_red ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def bright_green ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def bright_yellow ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def bright_blue ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def bright_magenta ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def bright_cyan ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> def bright_white ( s ) : <EOL> return "<STR_LIT>" . format ( s ) <EOL> class ColourFormatter ( logging . Formatter ) : <EOL> def format ( self , record ) : <EOL> s = super ( ColourFormatter , self ) . format ( record ) <EOL> if record . levelno == logging . CRITICAL : <EOL> return bright_red ( s ) <EOL> elif record . levelno == logging . ERROR : <EOL> return bright_yellow ( s ) <EOL> elif record . levelno == logging . WARNING : <EOL> return yellow ( s ) <EOL> elif record . levelno == logging . INFO : <EOL> return cyan ( s ) <EOL> elif record . levelno == logging . DEBUG : <EOL> return blue ( s ) <EOL> else : <EOL> return s <EOL> class Watcher ( threading . local ) : <EOL> handlers = { } <EOL> def __init__ ( self , logger_name ) : <EOL> super ( Watcher , self ) . __init__ ( ) <EOL> self . logger_name = logger_name <EOL> self . logger = logging . getLogger ( self . logger_name ) <EOL> self . formatter = ColourFormatter ( ) <EOL> def watch ( self , level = None , out = sys . stdout ) : <EOL> try : <EOL> self . logger . removeHandler ( self . handlers [ self . logger_name ] ) <EOL> except KeyError : <EOL> pass <EOL> handler = logging . StreamHandler ( out ) <EOL> handler . setFormatter ( self . formatter ) <EOL> self . handlers [ self . logger_name ] = handler <EOL> self . logger . addHandler ( handler ) <EOL> if level is None : <EOL> level = logging . DEBUG if __debug__ else logging . INFO <EOL> self . logger . setLevel ( level ) <EOL> def watch ( logger_name , level = logging . INFO , out = sys . stdout ) : <EOL> watcher = Watcher ( logger_name ) <EOL> watcher . watch ( level , out ) </s>
