<s> import os <EOL> import time <EOL> try : <EOL> from . import shared as G <EOL> assert G <EOL> unicode = str <EOL> from . exc_fmt import str_e <EOL> python2 = False <EOL> except ImportError : <EOL> python2 = True <EOL> from exc_fmt import str_e <EOL> import shared as G <EOL> LOG_LEVELS = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:4> , <EOL> } <EOL> LOG_LEVEL = LOG_LEVELS [ '<STR_LIT>' ] <EOL> LOG_FILE = os . path . join ( G . BASE_DIR , '<STR_LIT>' ) <EOL> try : <EOL> fd = open ( LOG_FILE , '<STR_LIT:w>' ) <EOL> fd . close ( ) <EOL> except Exception as e : <EOL> pass <EOL> def safe_print ( msg ) : <EOL> try : <EOL> print ( msg ) <EOL> except UnicodeEncodeError : <EOL> print ( msg . encode ( '<STR_LIT:utf-8>' ) ) <EOL> def editor_log ( msg ) : <EOL> safe_print ( msg ) <EOL> def floobits_log ( msg ) : <EOL> try : <EOL> fd = open ( LOG_FILE , '<STR_LIT>' ) <EOL> fmsg = msg <EOL> try : <EOL> fmsg = fmsg . encode ( '<STR_LIT:utf-8>' ) <EOL> except Exception : <EOL> pass <EOL> fd . write ( fmsg ) <EOL> fd . write ( b'<STR_LIT:\n>' ) <EOL> fd . close ( ) <EOL> except Exception as e : <EOL> safe_print ( str_e ( e ) ) <EOL> class MSG ( object ) : <EOL> def __init__ ( self , msg , timestamp = None , username = None , level = LOG_LEVELS [ '<STR_LIT>' ] ) : <EOL> self . msg = msg <EOL> self . timestamp = timestamp or time . time ( ) <EOL> self . username = username <EOL> self . level = level <EOL> def display ( self ) : <EOL> if self . level < LOG_LEVEL : <EOL> return <EOL> msg = unicode ( self ) <EOL> if G . LOG_TO_CONSOLE or G . CHAT_VIEW is None : <EOL> floobits_log ( msg ) <EOL> safe_print ( msg ) <EOL> else : <EOL> editor_log ( msg ) <EOL> def __str__ ( self ) : <EOL> if python2 : <EOL> return self . __unicode__ ( ) . encode ( '<STR_LIT:utf-8>' ) <EOL> return self . __unicode__ ( ) <EOL> def __unicode__ ( self ) : <EOL> if self . username : <EOL> msg = '<STR_LIT>' <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> try : <EOL> return unicode ( msg ) . format ( user = self . username , time = time . ctime ( self . timestamp ) , msg = self . msg ) <EOL> except UnicodeEncodeError : <EOL> return unicode ( msg ) . format ( user = self . username , time = time . ctime ( self . timestamp ) , msg = self . msg . encode ( <EOL> '<STR_LIT:utf-8>' ) ) <EOL> def msg_format ( message , * args , ** kwargs ) : <EOL> try : <EOL> message = unicode ( message ) <EOL> except UnicodeEncodeError : <EOL> message = str ( message ) <EOL> for arg in args : <EOL> try : <EOL> message += unicode ( arg ) <EOL> except UnicodeEncodeError : <EOL> message += arg <EOL> if kwargs : <EOL> message = message . format ( ** kwargs ) <EOL> return message <EOL> def _log ( message , level , * args , ** kwargs ) : <EOL> if level >= LOG_LEVEL : <EOL> MSG ( msg_format ( message , * args , ** kwargs ) , level = level ) . display ( ) <EOL> def debug ( message , * args , ** kwargs ) : <EOL> _log ( message , LOG_LEVELS [ '<STR_LIT>' ] , * args , ** kwargs ) <EOL> def log ( message , * args , ** kwargs ) : <EOL> _log ( message , LOG_LEVELS [ '<STR_LIT>' ] , * args , ** kwargs ) <EOL> def warn ( message , * args , ** kwargs ) : <EOL> _log ( message , LOG_LEVELS [ '<STR_LIT>' ] , * args , ** kwargs ) <EOL> def error ( message , * args , ** kwargs ) : <EOL> _log ( message , LOG_LEVELS [ '<STR_LIT>' ] , * args , ** kwargs ) </s>
