blob: f75e2d4573577cfaa2cae0aca5c8c2812f273cf2 [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01002//
David Neto9fc86582016-09-01 15:33:59 -04003// 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
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01008//
David Neto9fc86582016-09-01 15:33:59 -04009// 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.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010014
dan sinclaireda2cfb2018-08-03 15:06:09 -040015#include "source/print.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010016
Diego Novillod2938e42017-11-08 12:40:02 -050017#if defined(SPIRV_ANDROID) || defined(SPIRV_LINUX) || defined(SPIRV_MAC) || \
18 defined(SPIRV_FREEBSD)
dan sinclair3dad1cd2018-07-07 09:38:00 -040019namespace spvtools {
David Neto01656362015-11-20 10:44:41 -050020
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040021clr::reset::operator const char*() { return "\x1b[0m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010022
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040023clr::grey::operator const char*() { return "\x1b[1;30m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010024
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040025clr::red::operator const char*() { return "\x1b[31m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010026
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040027clr::green::operator const char*() { return "\x1b[32m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010028
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040029clr::yellow::operator const char*() { return "\x1b[33m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010030
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040031clr::blue::operator const char*() { return "\x1b[34m"; }
David Neto01656362015-11-20 10:44:41 -050032
dan sinclair3dad1cd2018-07-07 09:38:00 -040033} // namespace spvtools
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010034#elif defined(SPIRV_WINDOWS)
David Neto7c58c1d2016-02-29 01:01:04 -050035#include <windows.h>
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010036
dan sinclair3dad1cd2018-07-07 09:38:00 -040037namespace spvtools {
David Neto01656362015-11-20 10:44:41 -050038
Diego Novillod2938e42017-11-08 12:40:02 -050039static void SetConsoleForegroundColorPrimary(HANDLE hConsole, WORD color) {
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020040 // Get screen buffer information from console handle
41 CONSOLE_SCREEN_BUFFER_INFO bufInfo;
42 GetConsoleScreenBufferInfo(hConsole, &bufInfo);
43
44 // Get background color
Andrey Tuganov32cf85d2017-08-28 18:36:52 -040045 color = WORD(color | (bufInfo.wAttributes & 0xfff0));
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020046
47 // Set foreground color
48 SetConsoleTextAttribute(hConsole, color);
49}
50
Diego Novillod2938e42017-11-08 12:40:02 -050051static void SetConsoleForegroundColor(WORD color) {
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020052 SetConsoleForegroundColorPrimary(GetStdHandle(STD_OUTPUT_HANDLE), color);
53 SetConsoleForegroundColorPrimary(GetStdHandle(STD_ERROR_HANDLE), color);
54}
55
Lei Zhang1a0334e2015-11-02 09:41:20 -050056clr::reset::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050057 if (isPrint) {
58 SetConsoleForegroundColor(0xf);
59 return "";
60 }
61 return "\x1b[0m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010062}
63
Lei Zhang1a0334e2015-11-02 09:41:20 -050064clr::grey::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050065 if (isPrint) {
66 SetConsoleForegroundColor(FOREGROUND_INTENSITY);
67 return "";
68 }
69 return "\x1b[1;30m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010070}
71
Lei Zhang1a0334e2015-11-02 09:41:20 -050072clr::red::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050073 if (isPrint) {
74 SetConsoleForegroundColor(FOREGROUND_RED);
75 return "";
76 }
77 return "\x1b[31m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010078}
79
Lei Zhang1a0334e2015-11-02 09:41:20 -050080clr::green::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050081 if (isPrint) {
82 SetConsoleForegroundColor(FOREGROUND_GREEN);
83 return "";
84 }
85 return "\x1b[32m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010086}
87
Lei Zhang1a0334e2015-11-02 09:41:20 -050088clr::yellow::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050089 if (isPrint) {
90 SetConsoleForegroundColor(FOREGROUND_RED | FOREGROUND_GREEN);
91 return "";
92 }
93 return "\x1b[33m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010094}
95
Lei Zhang1a0334e2015-11-02 09:41:20 -050096clr::blue::operator const char*() {
David Neto01677582017-08-24 10:34:00 -040097 // Blue all by itself is hard to see against a black background (the
98 // default on command shell), or a medium blue background (the default
99 // on PowerShell). So increase its intensity.
Lei Zhang13795352017-11-30 16:35:22 -0500100
101 if (isPrint) {
102 SetConsoleForegroundColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
103 return "";
104 }
105 return "\x1b[94m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100106}
David Neto01656362015-11-20 10:44:41 -0500107
dan sinclair3dad1cd2018-07-07 09:38:00 -0400108} // namespace spvtools
Ben Vanik01c8d7a2015-11-22 08:32:53 -0800109#else
dan sinclair3dad1cd2018-07-07 09:38:00 -0400110namespace spvtools {
Ben Vanik01c8d7a2015-11-22 08:32:53 -0800111
112clr::reset::operator const char*() { return ""; }
113
114clr::grey::operator const char*() { return ""; }
115
116clr::red::operator const char*() { return ""; }
117
118clr::green::operator const char*() { return ""; }
119
120clr::yellow::operator const char*() { return ""; }
121
122clr::blue::operator const char*() { return ""; }
123
dan sinclair3dad1cd2018-07-07 09:38:00 -0400124} // namespace spvtools
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100125#endif