20 from contextlib
import contextmanager
25 from ctypes
import windll, Structure, c_short, c_ushort, byref
30 from robot.utils import console_encode, isatty, WINDOWS
40 options = {
'AUTO': Highlighter
if isatty(stream)
else NoHighlighting,
42 'OFF': NoHighlighting,
43 'ANSI': AnsiHighlighter}
45 highlighter = options[colors.upper()]
47 raise DataError(
"Invalid console color value '%s'. Available "
48 "'AUTO', 'ON', 'OFF' and 'ANSI'." % colors)
49 return highlighter(stream)
51 def write(self, text, flush=True):
62 except IOError
as err:
63 if not (WINDOWS
and err.errno == 0
and retry > 0):
65 self.
_write_write(text, retry-1)
69 _suppress_broken_pipe_error = property
74 except IOError
as err:
75 if err.errno
not in (errno.EPIPE, errno.EINVAL, errno.EBADF):
87 self.
writewrite(text, flush)
93 return WINDOWS
and not isinstance(self.
_highlighter_highlighter, NoHighlighting)
95 def error(self, message, level):
96 self.
writewrite(
'[ ', flush=
False)
97 self.
highlighthighlight(level, flush=
False)
98 self.
writewrite(
' ] %s\n' % message)
103 start = {
'PASS': highlighter.green,
104 'FAIL': highlighter.red,
105 'ERROR': highlighter.red,
106 'WARN': highlighter.yellow,
107 'SKIP': highlighter.yellow}[status]
125 _ANSI_GREEN =
'\033[32m'
129 _ANSI_RED =
'\033[31m'
133 _ANSI_YELLOW =
'\033[33m'
137 _ANSI_RESET =
'\033[0m'
164 class DosHighlighter:
168 _FOREGROUND_GREEN = 0x2
172 _FOREGROUND_RED = 0x4
176 _FOREGROUND_YELLOW = 0x6
180 _FOREGROUND_GREY = 0x7
184 _FOREGROUND_INTENSITY = 0x8
188 _BACKGROUND_MASK = 0xF0
217 if stream
is sys.__stdout__
else self.
_STDERR_HANDLE_STDERR_HANDLE
218 return windll.kernel32.GetStdHandle(handle)
222 ok = windll.kernel32.GetConsoleScreenBufferInfo(self.
_handle_handle, byref(csbi))
225 return csbi.wAttributes
231 windll.kernel32.SetConsoleTextAttribute(self.
_handle_handle, colors)
240 _fields_ = [(
"X", c_short),
247 _fields_ = [(
"Left", c_short),
256 _fields_ = [(
"dwSize", _COORD),
257 (
"dwCursorPosition", _COORD),
258 (
"wAttributes", c_ushort),
259 (
"srWindow", _SMALL_RECT),
260 (
"dwMaximumWindowSize", _COORD)]
def __init__(self, stream)
def _set_color(self, color)
def _set_foreground_colors(self, colors)
int _FOREGROUND_INTENSITY
def _set_colors(self, colors)
def __init__(self, stream)
def _get_std_handle(self, stream)
def highlight(self, text, status=None, flush=True)
def _highlighting(self, status)
def _suppress_broken_pipe_error(self)
def _get_highlighter(self, stream, colors)
def error(self, message, level)
def _must_flush_before_and_after_highlighting(self)
def _write(self, text, retry=5)
def __init__(self, stream, colors='AUTO')
def write(self, text, flush=True)
_suppress_broken_pipe_error
def _set_color(self, color)
def write(msg, level='INFO', html=False)
Writes the message to the log file using the given level.
def console_encode(string, encoding=None, errors='replace', stream=sys.__stdout__, force=False)
Encodes the given string so that it can be used in the console.