2014-03-19 23:07:30 -04:00
|
|
|
import sys
|
|
|
|
from ..util.singleton import Singleton
|
|
|
|
from .color import Color
|
|
|
|
from .level import Level
|
|
|
|
|
|
|
|
class Messenger(object):
|
|
|
|
__metaclass__ = Singleton
|
|
|
|
|
|
|
|
def __init__(self, level = Level.LOWINFO):
|
|
|
|
self.set_level(level)
|
|
|
|
|
|
|
|
def set_level(self, level):
|
|
|
|
self._level = level
|
|
|
|
|
|
|
|
def log(self, level, message):
|
|
|
|
if (level >= self._level):
|
2014-06-15 02:56:33 -04:00
|
|
|
print('%s%s%s' % (self._color(level), message, self._reset()))
|
2014-03-19 23:07:30 -04:00
|
|
|
|
|
|
|
def debug(self, message):
|
|
|
|
self.log(Level.DEBUG, message)
|
|
|
|
|
|
|
|
def lowinfo(self, message):
|
|
|
|
self.log(Level.LOWINFO, message)
|
|
|
|
|
|
|
|
def info(self, message):
|
|
|
|
self.log(Level.INFO, message)
|
|
|
|
|
|
|
|
def warning(self, message):
|
|
|
|
self.log(Level.WARNING, message)
|
|
|
|
|
|
|
|
def error(self, message):
|
|
|
|
self.log(Level.ERROR, message)
|
2014-10-17 10:05:47 -04:00
|
|
|
|
2014-03-19 23:07:30 -04:00
|
|
|
def _color(self, level):
|
|
|
|
'''
|
|
|
|
Get a color (terminal escape sequence) according to a level.
|
|
|
|
'''
|
|
|
|
if not sys.stdout.isatty():
|
|
|
|
return ''
|
|
|
|
elif level < Level.DEBUG:
|
|
|
|
return ''
|
|
|
|
elif Level.DEBUG <= level < Level.LOWINFO:
|
|
|
|
return Color.YELLOW
|
|
|
|
elif Level.LOWINFO <= level < Level.INFO:
|
|
|
|
return Color.BLUE
|
|
|
|
elif Level.INFO <= level < Level.WARNING:
|
|
|
|
return Color.GREEN
|
|
|
|
elif Level.WARNING <= level < Level.ERROR:
|
|
|
|
return Color.MAGENTA
|
|
|
|
elif Level.ERROR <= level:
|
|
|
|
return Color.RED
|
|
|
|
|
|
|
|
def _reset(self):
|
|
|
|
'''
|
|
|
|
Get a reset color (terminal escape sequence).
|
|
|
|
'''
|
|
|
|
if not sys.stdout.isatty():
|
|
|
|
return ''
|
|
|
|
else:
|
|
|
|
return Color.RESET
|