/src/spirv-tools/source/print.cpp
Line | Count | Source |
1 | | // Copyright (c) 2015-2016 The Khronos Group Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "source/print.h" |
16 | | |
17 | | #if defined(SPIRV_ANDROID) || defined(SPIRV_LINUX) || defined(SPIRV_MAC) || \ |
18 | | defined(SPIRV_IOS) || defined(SPIRV_TVOS) || defined(SPIRV_FREEBSD) || \ |
19 | | defined(SPIRV_OPENBSD) || defined(SPIRV_EMSCRIPTEN) || \ |
20 | | defined(SPIRV_FUCHSIA) || defined(SPIRV_GNU) |
21 | | namespace spvtools { |
22 | | |
23 | 46.4M | clr::reset::operator const char*() { return "\x1b[0m"; } |
24 | | |
25 | 4.46M | clr::grey::operator const char*() { return "\x1b[1;30m"; } |
26 | | |
27 | 4.65M | clr::red::operator const char*() { return "\x1b[31m"; } |
28 | | |
29 | 2.69M | clr::green::operator const char*() { return "\x1b[32m"; } |
30 | | |
31 | 19.8M | clr::yellow::operator const char*() { return "\x1b[33m"; } |
32 | | |
33 | 3.92M | clr::blue::operator const char*() { return "\x1b[34m"; } |
34 | | |
35 | | } // namespace spvtools |
36 | | #elif defined(SPIRV_WINDOWS) |
37 | | #include <windows.h> |
38 | | |
39 | | namespace spvtools { |
40 | | |
41 | | static void SetConsoleForegroundColorPrimary(HANDLE hConsole, WORD color) { |
42 | | // Get screen buffer information from console handle |
43 | | CONSOLE_SCREEN_BUFFER_INFO bufInfo; |
44 | | GetConsoleScreenBufferInfo(hConsole, &bufInfo); |
45 | | |
46 | | // Get background color |
47 | | color = WORD(color | (bufInfo.wAttributes & 0xfff0)); |
48 | | |
49 | | // Set foreground color |
50 | | SetConsoleTextAttribute(hConsole, color); |
51 | | } |
52 | | |
53 | | static void SetConsoleForegroundColor(WORD color) { |
54 | | SetConsoleForegroundColorPrimary(GetStdHandle(STD_OUTPUT_HANDLE), color); |
55 | | SetConsoleForegroundColorPrimary(GetStdHandle(STD_ERROR_HANDLE), color); |
56 | | } |
57 | | |
58 | | clr::reset::operator const char*() { |
59 | | if (isPrint) { |
60 | | SetConsoleForegroundColor(0xf); |
61 | | return ""; |
62 | | } |
63 | | return "\x1b[0m"; |
64 | | } |
65 | | |
66 | | clr::grey::operator const char*() { |
67 | | if (isPrint) { |
68 | | SetConsoleForegroundColor(FOREGROUND_INTENSITY); |
69 | | return ""; |
70 | | } |
71 | | return "\x1b[1;30m"; |
72 | | } |
73 | | |
74 | | clr::red::operator const char*() { |
75 | | if (isPrint) { |
76 | | SetConsoleForegroundColor(FOREGROUND_RED); |
77 | | return ""; |
78 | | } |
79 | | return "\x1b[31m"; |
80 | | } |
81 | | |
82 | | clr::green::operator const char*() { |
83 | | if (isPrint) { |
84 | | SetConsoleForegroundColor(FOREGROUND_GREEN); |
85 | | return ""; |
86 | | } |
87 | | return "\x1b[32m"; |
88 | | } |
89 | | |
90 | | clr::yellow::operator const char*() { |
91 | | if (isPrint) { |
92 | | SetConsoleForegroundColor(FOREGROUND_RED | FOREGROUND_GREEN); |
93 | | return ""; |
94 | | } |
95 | | return "\x1b[33m"; |
96 | | } |
97 | | |
98 | | clr::blue::operator const char*() { |
99 | | // Blue all by itself is hard to see against a black background (the |
100 | | // default on command shell), or a medium blue background (the default |
101 | | // on PowerShell). So increase its intensity. |
102 | | |
103 | | if (isPrint) { |
104 | | SetConsoleForegroundColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY); |
105 | | return ""; |
106 | | } |
107 | | return "\x1b[94m"; |
108 | | } |
109 | | |
110 | | } // namespace spvtools |
111 | | #else |
112 | | namespace spvtools { |
113 | | |
114 | | clr::reset::operator const char*() { return ""; } |
115 | | |
116 | | clr::grey::operator const char*() { return ""; } |
117 | | |
118 | | clr::red::operator const char*() { return ""; } |
119 | | |
120 | | clr::green::operator const char*() { return ""; } |
121 | | |
122 | | clr::yellow::operator const char*() { return ""; } |
123 | | |
124 | | clr::blue::operator const char*() { return ""; } |
125 | | |
126 | | } // namespace spvtools |
127 | | #endif |