2012-06-20 20:41:04 +00:00
|
|
|
# -*- coding: iso-8859-1 -*-
|
|
|
|
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
|
2013-02-10 17:25:21 +00:00
|
|
|
# Copyright (C) 2012-2013 Bastian Kleineidam
|
2012-06-20 19:58:13 +00:00
|
|
|
import time
|
2012-12-07 23:45:18 +00:00
|
|
|
import sys
|
2012-12-12 16:41:29 +00:00
|
|
|
import os
|
2012-12-09 17:12:41 +00:00
|
|
|
import threading
|
2013-03-25 18:39:37 +00:00
|
|
|
import traceback
|
2012-12-12 16:41:29 +00:00
|
|
|
from .ansicolor import Colorizer
|
2012-12-09 17:12:41 +00:00
|
|
|
|
|
|
|
lock = threading.Lock()
|
2012-06-20 19:58:13 +00:00
|
|
|
|
|
|
|
class Output(object):
|
2012-09-26 14:47:39 +00:00
|
|
|
"""Print output with context, indentation and optional timestamps."""
|
|
|
|
|
2012-12-12 16:41:29 +00:00
|
|
|
def __init__(self, stream=sys.stdout):
|
2012-09-26 14:47:39 +00:00
|
|
|
"""Initialize context and indentation."""
|
2012-06-20 19:58:13 +00:00
|
|
|
self.context = ''
|
|
|
|
self.level = 0
|
|
|
|
self.timestamps = False
|
2013-03-09 08:00:50 +00:00
|
|
|
self.setStream(stream)
|
|
|
|
|
|
|
|
def setStream(self, stream):
|
|
|
|
"""Initialize context and indentation."""
|
2012-12-12 16:41:29 +00:00
|
|
|
self.stream = Colorizer(stream)
|
2012-06-20 19:58:13 +00:00
|
|
|
|
2012-12-07 23:45:18 +00:00
|
|
|
def info(self, s, level=0):
|
2012-12-12 16:41:29 +00:00
|
|
|
"""Write an informational message."""
|
2012-12-07 23:45:18 +00:00
|
|
|
self.write(s, level=level)
|
|
|
|
|
2013-02-07 17:28:40 +00:00
|
|
|
def debug(self, s, level=2):
|
2012-12-12 16:41:29 +00:00
|
|
|
"""Write a debug message."""
|
2013-02-07 17:28:40 +00:00
|
|
|
self.write(s, level=level, color='white')
|
2012-12-07 23:45:18 +00:00
|
|
|
|
|
|
|
def warn(self, s):
|
2012-12-12 16:41:29 +00:00
|
|
|
"""Write a warning message."""
|
|
|
|
self.write("WARN: %s" % s, color='bold;yellow')
|
2012-12-07 23:45:18 +00:00
|
|
|
|
2013-03-25 18:39:37 +00:00
|
|
|
def error(self, s, tb=None):
|
2012-12-12 16:41:29 +00:00
|
|
|
"""Write an error message."""
|
|
|
|
self.write("ERROR: %s" % s, color='light;red')
|
2013-03-25 18:39:37 +00:00
|
|
|
#if tb is not None:
|
|
|
|
# self.write('Traceback (most recent call last):', 1)
|
|
|
|
|
|
|
|
def exception(self, s):
|
|
|
|
"""Write error message with traceback info."""
|
|
|
|
self.error(s)
|
|
|
|
type, value, tb = sys.exc_info()
|
|
|
|
self.writelines(traceback.format_stack(), 1)
|
|
|
|
self.writelines(traceback.format_tb(tb)[1:], 1)
|
|
|
|
self.writelines(traceback.format_exception_only(type, value), 1)
|
2012-12-07 23:45:18 +00:00
|
|
|
|
2012-12-12 16:41:29 +00:00
|
|
|
def write(self, s, level=0, color=None):
|
2012-09-26 14:47:39 +00:00
|
|
|
"""Write message with indentation, context and optional timestamp."""
|
2012-06-20 19:58:13 +00:00
|
|
|
if level > self.level:
|
|
|
|
return
|
|
|
|
if self.level > 1 or self.timestamps:
|
|
|
|
timestamp = time.strftime('%H:%M:%S ')
|
|
|
|
else:
|
|
|
|
timestamp = ''
|
2012-12-09 17:12:41 +00:00
|
|
|
with lock:
|
2013-03-09 08:00:50 +00:00
|
|
|
if self.context or timestamp:
|
|
|
|
self.stream.write('%s%s> ' % (timestamp, self.context))
|
2012-12-12 16:41:29 +00:00
|
|
|
self.stream.write('%s' % s, color=color)
|
|
|
|
self.stream.write(os.linesep)
|
|
|
|
self.stream.flush()
|
2012-06-20 19:58:13 +00:00
|
|
|
|
|
|
|
def writelines(self, lines, level=0):
|
2012-09-26 14:47:39 +00:00
|
|
|
"""Write multiple messages."""
|
2012-06-20 19:58:13 +00:00
|
|
|
for line in lines:
|
|
|
|
for line in line.rstrip('\n').split('\n'):
|
|
|
|
self.write(line.rstrip('\n'), level=level)
|
|
|
|
|
|
|
|
out = Output()
|