]> git.cworth.org Git - apitrace/blob - format.py
Build D3D wrappers with CMake.
[apitrace] / format.py
1 #!/usr/bin/env python
2 ##########################################################################
3 #
4 # Copyright 2008-2009 VMware, Inc.
5 # All Rights Reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24 #
25 ##########################################################################/
26
27
28 import sys
29
30
31 class Formatter:
32     '''Plain formatter'''
33
34     def __init__(self, stream):
35         self.stream = stream
36
37     def text(self, text):
38         self.stream.write(text)
39
40     def newline(self):
41         self.text('\n')
42
43     def function(self, name):
44         self.text(name)
45
46     def variable(self, name):
47         self.text(name)
48
49     def literal(self, value):
50         self.text(str(value))
51
52     def address(self, addr):
53         self.text(str(addr))
54
55
56 class AnsiFormatter(Formatter):
57     '''Formatter for plain-text files which outputs ANSI escape codes. See
58     http://en.wikipedia.org/wiki/ANSI_escape_code for more information
59     concerning ANSI escape codes.
60     '''
61
62     _csi = '\33['
63
64     _normal = '0m'
65     _bold = '1m'
66     _italic = '3m'
67     _red = '31m'
68     _green = '32m'
69     _blue = '34m'
70
71     def _escape(self, code):
72         self.text(self._csi + code)
73
74     def function(self, name):
75         self._escape(self._bold)
76         Formatter.function(self, name)
77         self._escape(self._normal)
78
79     def variable(self, name):
80         self._escape(self._italic)
81         Formatter.variable(self, name)
82         self._escape(self._normal)
83
84     def literal(self, value):
85         self._escape(self._blue)
86         Formatter.literal(self, value)
87         self._escape(self._normal)
88
89     def address(self, value):
90         self._escape(self._green)
91         Formatter.address(self, value)
92         self._escape(self._normal)
93
94
95 class WindowsConsoleFormatter(Formatter):
96     '''Formatter for the Windows Console. See 
97     http://code.activestate.com/recipes/496901/ for more information.
98     '''
99
100     STD_INPUT_HANDLE  = -10
101     STD_OUTPUT_HANDLE = -11
102     STD_ERROR_HANDLE  = -12
103
104     FOREGROUND_BLUE      = 0x01
105     FOREGROUND_GREEN     = 0x02
106     FOREGROUND_RED       = 0x04
107     FOREGROUND_INTENSITY = 0x08
108     BACKGROUND_BLUE      = 0x10
109     BACKGROUND_GREEN     = 0x20
110     BACKGROUND_RED       = 0x40
111     BACKGROUND_INTENSITY = 0x80
112
113     _normal = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
114     _bold = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY
115     _italic = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
116     _red = FOREGROUND_RED | FOREGROUND_INTENSITY
117     _green = FOREGROUND_GREEN | FOREGROUND_INTENSITY
118     _blue = FOREGROUND_BLUE | FOREGROUND_INTENSITY
119
120     def __init__(self, stream):
121         Formatter.__init__(self, stream)
122
123         if stream is sys.stdin:
124             nStdHandle = self.STD_INPUT_HANDLE
125         elif stream is sys.stdout:
126             nStdHandle = self.STD_OUTPUT_HANDLE
127         elif stream is sys.stderr:
128             nStdHandle = self.STD_ERROR_HANDLE
129         else:
130             nStdHandle = None
131
132         if nStdHandle:
133             import ctypes
134             self.handle = ctypes.windll.kernel32.GetStdHandle(nStdHandle)
135         else:
136             self.handle = None
137
138     def _attribute(self, attr):
139         if self.handle:
140             import ctypes
141             ctypes.windll.kernel32.SetConsoleTextAttribute(self.handle, attr)
142
143     def function(self, name):
144         self._attribute(self._bold)
145         Formatter.function(self, name)
146         self._attribute(self._normal)
147
148     def variable(self, name):
149         self._attribute(self._italic)
150         Formatter.variable(self, name)
151         self._attribute(self._normal)
152
153     def literal(self, value):
154         self._attribute(self._blue)
155         Formatter.literal(self, value)
156         self._attribute(self._normal)
157
158     def address(self, value):
159         self._attribute(self._green)
160         Formatter.address(self, value)
161         self._attribute(self._normal)
162
163
164 def DefaultFormatter(stream):
165     if sys.platform in ('linux2', 'cygwin'):
166         return AnsiFormatter(stream)
167     elif sys.platform in ('win32',):
168         return WindowsConsoleFormatter(stream)
169     else:
170         return Formatter(stream)
171