1import traceback 2import sys 3try: 4 import StringIO 5except: 6 import io as StringIO #Python 3.0 7 8 9class Log: 10 11 def __init__(self): 12 self._contents = [] 13 14 def add_content(self, *content): 15 self._contents.append(' '.join(content)) 16 17 def add_exception(self): 18 s = StringIO.StringIO() 19 exc_info = sys.exc_info() 20 traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], limit=None, file=s) 21 self._contents.append(s.getvalue()) 22 23 24 def get_contents(self): 25 return '\n'.join(self._contents) 26 27 def clear_log(self): 28 del self._contents[:]