Author: Armin Rigo <ar...@tunes.org> Branch: remove-py-log Changeset: r82978:55c3c232ac56 Date: 2016-03-11 16:49 +0100 http://bitbucket.org/pypy/pypy/changeset/55c3c232ac56/
Log: Start diff --git a/rpython/tool/ansi_print.py b/rpython/tool/ansi_print.py --- a/rpython/tool/ansi_print.py +++ b/rpython/tool/ansi_print.py @@ -1,11 +1,44 @@ """ -A color print. +A simple color logger. """ import sys from py.io import ansi_print from rpython.tool.ansi_mandelbrot import Driver + +isatty = getattr(sys.stderr, 'isatty', lambda: False) +mandelbrot_driver = Driver() + + +class Logger(object): + + def __init__(self, name): + self.name = name + + def _make_method(subname, colors): + # + def logger_method(self, text): + text = "[%s%s] %s" % (self.name, subname, text) + if isatty(): + col = colors + else: + col = () + ansi_print(text, col) + # + return logger_method + + red = _make_method('', (31,)) + bold = _make_method('', (1,)) + WARNING = _make_method(':WARNING', (31,)) + event = _make_method('', (1,)) + ERROR = _make_method(':ERROR', (1, 31)) + Error = _make_method(':Error', (1, 31)) + info = _make_method(':info', (35,)) + stub = _make_method(':stub', (34,)) + __call__ = _make_method('', ()) + + class AnsiLog: wrote_dot = False # XXX sharing state with all instances @@ -70,5 +103,3 @@ for line in msg.content().splitlines(): ansi_print("[%s] %s" %(":".join(keywords), line), esc, file=self.file, newline=newline, flush=flush) - -ansi_log = AnsiLog() diff --git a/rpython/tool/test/test_ansi_print.py b/rpython/tool/test/test_ansi_print.py new file mode 100644 --- /dev/null +++ b/rpython/tool/test/test_ansi_print.py @@ -0,0 +1,39 @@ +from _pytest.monkeypatch import monkeypatch +from rpython.tool import ansi_print + + +class FakeOutput(object): + def __init__(self, tty=True): + self.monkey = monkeypatch() + self.tty = tty + self.output = [] + def __enter__(self, *args): + self.monkey.setattr(ansi_print, 'ansi_print', self._print) + self.monkey.setattr(ansi_print, 'isatty', self._isatty) + return self.output + def __exit__(self, *args): + self.monkey.undo() + + def _print(self, text, colors): + self.output.append((text, colors)) + def _isatty(self): + return self.tty + + +def test_simple(): + log = ansi_print.Logger('test') + with FakeOutput() as output: + log('Hello') + assert output == [('[test] Hello', ())] + +def test_bold(): + log = ansi_print.Logger('test') + with FakeOutput() as output: + log.bold('Hello') + assert output == [('[test] Hello', (1,))] + +def test_not_a_tty(): + log = ansi_print.Logger('test') + with FakeOutput(tty=False) as output: + log.bold('Hello') + assert output == [('[test] Hello', ())] _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit