]> git.cworth.org Git - apitrace/blob - formatter.hpp
Update glext headers.
[apitrace] / formatter.hpp
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 /*
27  * Helpers for coloring output.
28  */
29
30 #ifndef _FORMATTER_HPP_
31 #define _FORMATTER_HPP_
32
33
34 #include <iostream>
35
36
37 namespace Formatter {
38
39 /*
40  * See also http://bytes.com/topic/c/answers/63822-design-question-little-c-header-colorizing-text-linux-comments-ideas
41  */
42
43 class Attribute {
44 public:
45     virtual ~Attribute() {}
46
47     virtual void apply(std::ostream &) const {}
48 };
49
50
51 enum Color {
52     RED,
53     GREEN,
54     BLUE,
55 };
56
57
58 class Formatter {
59 public:
60     virtual ~Formatter() {}
61
62     virtual Attribute *normal(void) const { return new Attribute; }
63     virtual Attribute *bold(void) const { return new Attribute; }
64     virtual Attribute *italic(void) const { return new Attribute; }
65     virtual Attribute *color(Color) const { return new Attribute; }
66 };
67
68
69 class AnsiAttribute : public Attribute {
70 protected:
71     const char *escape;
72 public:
73     AnsiAttribute(const char *_escape) : escape(_escape) {}
74     void apply(std::ostream& os) const {
75         os << "\33[" << escape;
76     }
77 };
78
79
80 /**
81  * Formatter for plain-text files which outputs ANSI escape codes. See
82  * http://en.wikipedia.org/wiki/ANSI_escape_code for more information
83  * concerning ANSI escape codes.
84  */
85 class AnsiFormatter : public Formatter {
86 protected:
87 public:
88     virtual Attribute *normal(void) const { return new AnsiAttribute("0m"); }
89     virtual Attribute *bold(void) const { return new AnsiAttribute("1m"); }
90     virtual Attribute *italic(void) const { return new AnsiAttribute("3m"); }
91     virtual Attribute *color(Color c) const { 
92         static const char *color_escapes[] = {
93             "31m", /* red */
94             "32m", /* green */
95             "34m", /* blue */
96         };
97         return new AnsiAttribute(color_escapes[c]); 
98     }
99 };
100
101
102 inline std::ostream& operator<<(std::ostream& os, const Attribute *attr) {
103     attr->apply(os);
104     return os;
105 }
106
107
108 #ifdef _WIN32
109
110 #include <windows.h>
111
112 class WindowsAttribute : public Attribute {
113 protected:
114     WORD wAttributes;
115 public:
116     WindowsAttribute(WORD _wAttributes) : wAttributes(_wAttributes) {}
117     void apply(std::ostream& os) const {
118         DWORD nStdHandleOutput;
119         if (os == std::cout) {
120             nStdHandleOutput = STD_OUTPUT_HANDLE;
121         } else if (os == std::cerr) {
122             nStdHandleOutput = STD_ERROR_HANDLE;
123         } else {
124             return;
125         }
126         HANDLE hConsoleOutput = GetStdHandle(nStdHandleOutput);
127         if (hConsoleOutput == INVALID_HANDLE_VALUE) {
128             return;
129         }
130
131         SetConsoleTextAttribute(hConsoleOutput, wAttributes);
132     }
133 };
134
135
136 /**
137  * Formatter for the Windows Console.
138  */
139 class WindowsFormatter : public Formatter {
140 protected:
141 public:
142     virtual Attribute *normal(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); }
143     virtual Attribute *bold(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); }
144     virtual Attribute *italic(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); }
145     virtual Attribute *color(Color c) const { 
146         static const WORD color_escapes[] = {
147             FOREGROUND_RED | FOREGROUND_INTENSITY,
148             FOREGROUND_GREEN | FOREGROUND_INTENSITY,
149             FOREGROUND_BLUE | FOREGROUND_INTENSITY, 
150         };
151         return new WindowsAttribute(color_escapes[c]); 
152     }
153 };
154
155 #endif
156
157
158 inline Formatter *defaultFormatter(void) {
159 #ifdef _WIN32
160     return new WindowsFormatter;
161 #else
162     return new AnsiFormatter;
163 #endif
164 }
165
166
167 } /* namespace Formatter */
168
169
170 #endif /* _FORMATTER_HPP_ */