def log_file(msg, filename='game.log'):
 def callback(filehandle, msg):
  'Writing to file and flushing result'
  msg = ('\n%s [-] %s' % (timeformat(), msg.strip()))
  filehandle.write(msg)
  filehandle.flush()
 def errback(failure):
  'Catching errors to normal log'
  log_trace()
 filehandle = _open_log_file(filename)
 if filehandle:
  deferToThread(callback, filehandle, msg).addErrback(errback)

def log_file ( msg , filename = 'game.log' ) : def callback ( filehandle , msg ) : msg = ( '\n%s [-] %s' % ( timeformat ( ) , msg . strip ( ) ) ) filehandle . write ( msg ) filehandle . flush ( ) def errback ( failure ) : log_trace ( ) filehandle = _open_log_file ( filename ) if filehandle : deferToThread ( callback , filehandle , msg ) . addErrback ( errback )
arbitrary file logger using threads .