def log_completion(host, port, output, exception=None): # Increment the count of complete ops. This will # drain the queue, causing subsequent calls of this # method to block. n = completed.get() + 1 try: tstamp = time.asctime().split()[3] # Current time if color.has_colors(sys.stdout): progress = color.c("[%s]" % color.B(n)) success = color.g("[%s]" % color.B("SUCCESS")) failure = color.r("[%s]" % color.B("FAILURE")) exc = color.r(color.B(str(exception))) else: progress = "[%s]" % n success = "[SUCCESS]" failure = "[FAILURE]" exc = str(exception) if exception is not None: print progress, tstamp, failure, host, port, exc else: print progress, tstamp, success, host, port if output: print output, sys.stdout.flush() finally: # Update the count of complete ops. This will re-fill # the queue, allowing other threads to continue with # output. completed.put(n)
def output(term, strings): ''' A wrapper for print with color @param term: sys.stdout or sys.stderr @param strings: the str you want to print ''' if not term.isatty(): print strings else: if term == sys.stderr: if color.has_colors(term): print >> term, color.r(color.B(strings)) else: print >> term, strings else: print >> term, strings