Path: blob/21.2-virgl/src/gallium/tools/trace/format.py
4561 views
#!/usr/bin/env python31##########################################################################2#3# Copyright 2008 VMware, Inc.4# All Rights Reserved.5#6# Permission is hereby granted, free of charge, to any person obtaining a7# copy of this software and associated documentation files (the8# "Software"), to deal in the Software without restriction, including9# without limitation the rights to use, copy, modify, merge, publish,10# distribute, sub license, and/or sell copies of the Software, and to11# permit persons to whom the Software is furnished to do so, subject to12# the following conditions:13#14# The above copyright notice and this permission notice (including the15# next paragraph) shall be included in all copies or substantial portions16# of the Software.17#18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS19# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF20# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.21# IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR22# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,23# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE24# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.25#26##########################################################################272829import sys303132class Formatter:33'''Plain formatter'''3435def __init__(self, stream):36self.stream = stream3738def text(self, text):39self.stream.write(text)4041def newline(self):42self.text('\n')4344def function(self, name):45self.text(name)4647def variable(self, name):48self.text(name)4950def literal(self, value):51self.text(str(value))5253def address(self, addr):54self.text(str(addr))555657class AnsiFormatter(Formatter):58'''Formatter for plain-text files which outputs ANSI escape codes. See59http://en.wikipedia.org/wiki/ANSI_escape_code for more information60concerning ANSI escape codes.61'''6263_csi = '\33['6465_normal = '0m'66_bold = '1m'67_italic = '3m'68_red = '31m'69_green = '32m'70_blue = '34m'7172def _escape(self, code):73self.text(self._csi + code)7475def function(self, name):76self._escape(self._bold)77Formatter.function(self, name)78self._escape(self._normal)7980def variable(self, name):81self._escape(self._italic)82Formatter.variable(self, name)83self._escape(self._normal)8485def literal(self, value):86self._escape(self._blue)87Formatter.literal(self, value)88self._escape(self._normal)8990def address(self, value):91self._escape(self._green)92Formatter.address(self, value)93self._escape(self._normal)949596class WindowsConsoleFormatter(Formatter):97'''Formatter for the Windows Console. See98http://code.activestate.com/recipes/496901/ for more information.99'''100101STD_INPUT_HANDLE = -10102STD_OUTPUT_HANDLE = -11103STD_ERROR_HANDLE = -12104105FOREGROUND_BLUE = 0x01106FOREGROUND_GREEN = 0x02107FOREGROUND_RED = 0x04108FOREGROUND_INTENSITY = 0x08109BACKGROUND_BLUE = 0x10110BACKGROUND_GREEN = 0x20111BACKGROUND_RED = 0x40112BACKGROUND_INTENSITY = 0x80113114_normal = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED115_bold = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY116_italic = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED117_red = FOREGROUND_RED | FOREGROUND_INTENSITY118_green = FOREGROUND_GREEN | FOREGROUND_INTENSITY119_blue = FOREGROUND_BLUE | FOREGROUND_INTENSITY120121def __init__(self, stream):122Formatter.__init__(self, stream)123124if stream is sys.stdin:125nStdHandle = self.STD_INPUT_HANDLE126elif stream is sys.stdout:127nStdHandle = self.STD_OUTPUT_HANDLE128elif stream is sys.stderr:129nStdHandle = self.STD_ERROR_HANDLE130else:131nStdHandle = None132133if nStdHandle:134import ctypes135self.handle = ctypes.windll.kernel32.GetStdHandle(nStdHandle)136else:137self.handle = None138139def _attribute(self, attr):140if self.handle:141import ctypes142ctypes.windll.kernel32.SetConsoleTextAttribute(self.handle, attr)143144def function(self, name):145self._attribute(self._bold)146Formatter.function(self, name)147self._attribute(self._normal)148149def variable(self, name):150self._attribute(self._italic)151Formatter.variable(self, name)152self._attribute(self._normal)153154def literal(self, value):155self._attribute(self._blue)156Formatter.literal(self, value)157self._attribute(self._normal)158159def address(self, value):160self._attribute(self._green)161Formatter.address(self, value)162self._attribute(self._normal)163164165def DefaultFormatter(stream):166if sys.platform in ('linux2', 'linux', 'cygwin'):167return AnsiFormatter(stream)168elif sys.platform in ('win32', ):169return WindowsConsoleFormatter(stream)170else:171return Formatter(stream)172173174175