]> git.cworth.org Git - apitrace/blob - formatter.hpp
Standardize on 4 spaces.
[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 #ifndef _FORMATTER_HPP_
27 #define _FORMATTER_HPP_
28
29
30 #include <iostream>
31
32
33 namespace Formatter {
34
35 /*
36  * See also http://bytes.com/topic/c/answers/63822-design-question-little-c-header-colorizing-text-linux-comments-ideas
37  */
38
39 class Attribute {
40 public:
41     virtual ~Attribute() {}
42
43     virtual void apply(std::ostream &) const {}
44 };
45
46
47 enum Color {
48     RED,
49     GREEN,
50     BLUE,
51 };
52
53
54 class Formatter {
55 public:
56     virtual ~Formatter() {}
57
58     virtual Attribute *normal(void) const { return new Attribute; }
59     virtual Attribute *bold(void) const { return new Attribute; }
60     virtual Attribute *italic(void) const { return new Attribute; }
61     virtual Attribute *color(Color) const { return new Attribute; }
62 };
63
64
65 class AnsiAttribute : public Attribute {
66 protected:
67     const char *escape;
68 public:
69     AnsiAttribute(const char *_escape) : escape(_escape) {}
70     void apply(std::ostream& os) const {
71         os << "\33[" << escape;
72     }
73 };
74
75
76 /**
77  * Formatter for plain-text files which outputs ANSI escape codes. See
78  * http://en.wikipedia.org/wiki/ANSI_escape_code for more information
79  * concerning ANSI escape codes.
80  */
81 class AnsiFormatter : public Formatter {
82 protected:
83 public:
84     virtual Attribute *normal(void) const { return new AnsiAttribute("0m"); }
85     virtual Attribute *bold(void) const { return new AnsiAttribute("1m"); }
86     virtual Attribute *italic(void) const { return new AnsiAttribute("3m"); }
87     virtual Attribute *color(Color c) const { 
88         static const char *color_escapes[] = {
89             "31m", /* red */
90             "32m", /* green */
91             "34m", /* blue */
92         };
93         return new AnsiAttribute(color_escapes[c]); 
94     }
95 };
96
97
98 inline std::ostream& operator<<(std::ostream& os, const Attribute *attr) {
99     attr->apply(os);
100     return os;
101 }
102
103
104 #ifdef WIN32
105
106 #include <windows.h>
107
108 class WindowsAttribute : public Attribute {
109 protected:
110     WORD wAttributes;
111 public:
112     WindowsAttribute(WORD _wAttributes) : wAttributes(_wAttributes) {}
113     void apply(std::ostream& os) const {
114         DWORD nStdHandleOutput;
115         if (os == std::cout) {
116             nStdHandleOutput = STD_OUTPUT_HANDLE;
117         } else if (os == std::cerr) {
118             nStdHandleOutput = STD_ERROR_HANDLE;
119         } else {
120             return;
121         }
122         HANDLE hConsoleOutput = GetStdHandle(nStdHandleOutput);
123         if (hConsoleOutput == INVALID_HANDLE_VALUE) {
124             return;
125         }
126
127         SetConsoleTextAttribute(hConsoleOutput, wAttributes);
128     }
129 };
130
131
132 /**
133  * Formatter for the Windows Console.
134  */
135 class WindowsFormatter : public Formatter {
136 protected:
137 public:
138     virtual Attribute *normal(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); }
139     virtual Attribute *bold(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); }
140     virtual Attribute *italic(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); }
141     virtual Attribute *color(Color c) const { 
142         static const WORD color_escapes[] = {
143             FOREGROUND_RED | FOREGROUND_INTENSITY,
144             FOREGROUND_GREEN | FOREGROUND_INTENSITY,
145             FOREGROUND_BLUE | FOREGROUND_INTENSITY, 
146         };
147         return new WindowsAttribute(color_escapes[c]); 
148     }
149 };
150
151 #endif
152
153
154 inline Formatter *defaultFormatter(void) {
155 #ifdef WIN32
156     return new WindowsFormatter;
157 #else
158     return new AnsiFormatter;
159 #endif
160 }
161
162
163 } /* namespace Formatter */
164
165
166 #endif /* _FORMATTER_HPP_ */