<s> import logging <EOL> import sys <EOL> import time <EOL> try : <EOL> import curses <EOL> except ImportError : <EOL> curses = None <EOL> def _stderr_supports_color ( ) : <EOL> color = False <EOL> if curses and sys . stderr . isatty ( ) : <EOL> try : <EOL> curses . setupterm ( ) <EOL> if curses . tigetnum ( "<STR_LIT>" ) > <NUM_LIT:0> : <EOL> color = True <EOL> except Exception : <EOL> pass <EOL> return color <EOL> class LogFormatter ( logging . Formatter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , color = True , * args , ** kwargs ) : <EOL> logging . Formatter . __init__ ( self , * args , ** kwargs ) <EOL> self . _color = color and _stderr_supports_color ( ) <EOL> if self . _color : <EOL> fg_color = ( curses . tigetstr ( "<STR_LIT>" ) or <EOL> curses . tigetstr ( "<STR_LIT>" ) or "<STR_LIT>" ) <EOL> self . _colors = { <EOL> logging . DEBUG : curses . tparm ( fg_color , <NUM_LIT:4> ) , <EOL> logging . INFO : curses . tparm ( fg_color , <NUM_LIT:2> ) , <EOL> logging . WARNING : curses . tparm ( fg_color , <NUM_LIT:3> ) , <EOL> logging . ERROR : curses . tparm ( fg_color , <NUM_LIT:1> ) , <EOL> logging . CRITICAL : curses . tparm ( fg_color , <NUM_LIT:5> ) , <EOL> } <EOL> self . _normal = curses . tigetstr ( "<STR_LIT>" ) <EOL> def format ( self , record ) : <EOL> try : <EOL> record . message = record . getMessage ( ) <EOL> except Exception as e : <EOL> record . message = "<STR_LIT>" % ( e , record . __dict__ ) <EOL> record . asctime = time . strftime ( <EOL> "<STR_LIT>" , self . converter ( record . created ) ) <EOL> prefix = '<STR_LIT>' % record . __dict__ <EOL> if self . _color : <EOL> prefix = ( self . _colors . get ( record . levelno , self . _normal ) + <EOL> prefix + self . _normal ) <EOL> def safe_unicode ( s ) : <EOL> try : <EOL> return unicode ( s ) <EOL> except UnicodeDecodeError : <EOL> return repr ( s ) <EOL> formatted = prefix + "<STR_LIT:U+0020>" + safe_unicode ( record . message ) <EOL> if record . exc_info : <EOL> if not record . exc_text : <EOL> record . exc_text = self . formatException ( record . exc_info ) <EOL> if record . exc_text : <EOL> lines = [ formatted . rstrip ( ) ] <EOL> lines . extend ( safe_unicode ( ln ) for ln in record . exc_text . split ( '<STR_LIT:\n>' ) ) <EOL> formatted = '<STR_LIT:\n>' . join ( lines ) <EOL> return formatted . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) <EOL> def init ( ) : <EOL> logger = logging . getLogger ( ) <EOL> logger . setLevel ( logging . DEBUG ) <EOL> channel = logging . StreamHandler ( ) <EOL> channel . setFormatter ( LogFormatter ( ) ) <EOL> logger . addHandler ( channel ) </s>
