<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import os <EOL> import time <EOL> from datetime import datetime <EOL> from traceback import format_exc <EOL> from twisted . python import log <EOL> from twisted . internet . threads import deferToThread <EOL> _LOGDIR = None <EOL> _TIMEZONE = None <EOL> def timeformat ( when = None ) : <EOL> """<STR_LIT>""" <EOL> when = when if when else time . time ( ) <EOL> tz_offset = datetime . utcfromtimestamp ( when ) - datetime . fromtimestamp ( when ) <EOL> tz_offset = tz_offset . days * <NUM_LIT> + tz_offset . seconds <EOL> when = datetime . utcfromtimestamp ( when - tz_offset ) <EOL> tz_hour = abs ( int ( tz_offset // <NUM_LIT> ) ) <EOL> tz_mins = abs ( int ( tz_offset // <NUM_LIT> % <NUM_LIT> ) ) <EOL> tz_sign = "<STR_LIT:->" if tz_offset >= <NUM_LIT:0> else "<STR_LIT:+>" <EOL> return '<STR_LIT>' % ( <EOL> when . year , when . month , when . day , <EOL> when . hour , when . minute , when . second , <EOL> tz_sign , tz_hour , tz_mins ) <EOL> def log_trace ( errmsg = None ) : <EOL> """<STR_LIT>""" <EOL> tracestring = format_exc ( ) <EOL> try : <EOL> if tracestring : <EOL> for line in tracestring . splitlines ( ) : <EOL> log . msg ( '<STR_LIT>' % line ) <EOL> if errmsg : <EOL> try : <EOL> errmsg = str ( errmsg ) <EOL> except Exception as e : <EOL> errmsg = str ( e ) <EOL> for line in errmsg . splitlines ( ) : <EOL> log . msg ( '<STR_LIT>' % line ) <EOL> except Exception : <EOL> log . msg ( '<STR_LIT>' % errmsg ) <EOL> log_tracemsg = log_trace <EOL> def log_err ( errmsg ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> errmsg = str ( errmsg ) <EOL> except Exception as e : <EOL> errmsg = str ( e ) <EOL> for line in errmsg . splitlines ( ) : <EOL> log . msg ( '<STR_LIT>' % line ) <EOL> log_errmsg = log_err <EOL> def log_warn ( warnmsg ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> warnmsg = str ( warnmsg ) <EOL> except Exception as e : <EOL> warnmsg = str ( e ) <EOL> for line in warnmsg . splitlines ( ) : <EOL> log . msg ( '<STR_LIT>' % line ) <EOL> log_warnmsg = log_warn <EOL> def log_info ( infomsg ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> infomsg = str ( infomsg ) <EOL> except Exception as e : <EOL> infomsg = str ( e ) <EOL> for line in infomsg . splitlines ( ) : <EOL> log . msg ( '<STR_LIT>' % line ) <EOL> log_infomsg = log_info <EOL> def log_dep ( depmsg ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> depmsg = str ( depmsg ) <EOL> except Exception as e : <EOL> depmsg = str ( e ) <EOL> for line in depmsg . splitlines ( ) : <EOL> log . msg ( '<STR_LIT>' % line ) <EOL> log_depmsg = log_dep <EOL> LOG_FILE_HANDLES = { } <EOL> def log_file ( msg , filename = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> global LOG_FILE_HANDLES , _LOGDIR , _TIMEZONE <EOL> if not _LOGDIR : <EOL> from django . conf import settings <EOL> _LOGDIR = settings . LOG_DIR <EOL> if not _TIMEZONE : <EOL> from django . utils import timezone as _TIMEZONE <EOL> def callback ( filehandle , msg ) : <EOL> "<STR_LIT>" <EOL> msg = "<STR_LIT>" % ( _TIMEZONE . now ( ) , msg . strip ( ) ) <EOL> filehandle . write ( msg ) <EOL> filehandle . flush ( ) <EOL> def errback ( failure ) : <EOL> "<STR_LIT>" <EOL> log_trace ( ) <EOL> filename = os . path . join ( _LOGDIR , filename ) <EOL> if filename in LOG_FILE_HANDLES : <EOL> filehandle = LOG_FILE_HANDLES [ filename ] <EOL> else : <EOL> try : <EOL> filehandle = open ( filename , "<STR_LIT:a>" ) <EOL> LOG_FILE_HANDLES [ filename ] = filehandle <EOL> except IOError : <EOL> log_trace ( ) <EOL> return <EOL> deferToThread ( callback , filehandle , msg ) . addErrback ( errback ) </s>
