| 1 |
""" |
| 2 |
A simple color logger. |
| 3 |
""" |
| 4 |
|
| 5 |
import sys |
| 6 |
from py.io import ansi_print |
| 7 |
from rpython.tool.ansi_mandelbrot import Driver |
| 8 |
|
| 9 |
|
| •10 |
isatty = lambda: True |
| 11 |
mandelbrot_driver = Driver() |
| 12 |
wrote_dot = False |
| 13 |
|
| 14 |
|
| 15 |
def _make_method(subname, colors): |
| 16 |
|
| 17 |
def logger_method(self, text): |
| 18 |
global wrote_dot |
| 19 |
if self.output_disabled: |
| 20 |
return |
| 21 |
text = "[%s%s] %s" % (self.name, subname, text) |
| 22 |
if isatty(): |
| 23 |
col = colors |
| 24 |
else: |
| 25 |
col = () |
| 26 |
if wrote_dot: |
| 27 |
text = '\n' + text |
| 28 |
ansi_print(text, col) |
| 29 |
wrote_dot = False |
| 30 |
|
| 31 |
return logger_method |
| 32 |
|
| 33 |
|
| 34 |
class AnsiLogger(object): |
| 35 |
output_disabled = False |
| 36 |
|
| 37 |
def __init__(self, name): |
| 38 |
self.name = name |
| 39 |
|
| 40 |
|
| 41 |
red = _make_method('', (31,)) |
| 42 |
bold = _make_method('', (1,)) |
| 43 |
WARNING = _make_method(':WARNING', (31,)) |
| 44 |
event = _make_method('', (1,)) |
| 45 |
ERROR = _make_method(':ERROR', (1, 31)) |
| 46 |
Error = _make_method(':Error', (1, 31)) |
| 47 |
info = _make_method(':info', (35,)) |
| 48 |
stub = _make_method(':stub', (34,)) |
| 49 |
|
| 50 |
|
| 51 |
call = _make_method(':call', (34,)) |
| 52 |
result = _make_method(':result', (34,)) |
| 53 |
exception = _make_method(':exception', (34,)) |
| 54 |
vpath = _make_method(':vpath', (35,)) |
| 55 |
timeout = _make_method('', (1, 31)) |
| 56 |
|
| 57 |
|
| 58 |
__call__ = _make_method('', ()) |
| 59 |
|
| 60 |
|
| 61 |
def __getattr__(self, name): |
| 62 |
if name[0].isalpha(): |
| 63 |
method = _make_method(':' + name, ()) |
| 64 |
setattr(self.__class__, name, method) |
| 65 |
return getattr(self, name) |
| 66 |
raise AttributeError(name) |
| 67 |
|
| 68 |
def dot(self): |
| 69 |
"""Output a mandelbrot dot to the terminal.""" |
| 70 |
if not isatty(): |
| 71 |
return |
| 72 |
global wrote_dot |
| 73 |
if not wrote_dot: |
| 74 |
mandelbrot_driver.reset() |
| 75 |
wrote_dot = True |
| 76 |
mandelbrot_driver.dot() |
| 77 |
|
| 78 |
def debug(self, info): |
| 79 |
"""For messages that are dropped. Can be monkeypatched in tests.""" |