blob: 2418c5bc918b040455329030a1debeaec1006d71 [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) || \
Assiduous74e81052021-06-29 12:14:38 -070018 defined(SPIRV_IOS) || defined(SPIRV_TVOS) || defined(SPIRV_FREEBSD) || \
David Turner03957e82019-11-26 22:22:59 +010019 defined(SPIRV_EMSCRIPTEN) || defined(SPIRV_FUCHSIA)
dan sinclair3dad1cd2018-07-07 09:38:00 -040020namespace spvtools {
David Neto01656362015-11-20 10:44:41 -050021
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040022clr::reset::operator const char*() { return "\x1b[0m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010023
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040024clr::grey::operator const char*() { return "\x1b[1;30m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010025
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040026clr::red::operator const char*() { return "\x1b[31m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010027
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040028clr::green::operator const char*() { return "\x1b[32m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010029
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040030clr::yellow::operator const char*() { return "\x1b[33m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010031
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040032clr::blue::operator const char*() { return "\x1b[34m"; }
David Neto01656362015-11-20 10:44:41 -050033
dan sinclair3dad1cd2018-07-07 09:38:00 -040034} // namespace spvtools
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010035#elif defined(SPIRV_WINDOWS)
David Neto7c58c1d2016-02-29 01:01:04 -050036#include <windows.h>
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010037
dan sinclair3dad1cd2018-07-07 09:38:00 -040038namespace spvtools {
David Neto01656362015-11-20 10:44:41 -050039
Diego Novillod2938e42017-11-08 12:40:02 -050040static void SetConsoleForegroundColorPrimary(HANDLE hConsole, WORD color) {
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020041 // Get screen buffer information from console handle
42 CONSOLE_SCREEN_BUFFER_INFO bufInfo;
43 GetConsoleScreenBufferInfo(hConsole, &bufInfo);
44
45 // Get background color
Andrey Tuganov32cf85d2017-08-28 18:36:52 -040046 color = WORD(color | (bufInfo.wAttributes & 0xfff0));
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020047
48 // Set foreground color
49 SetConsoleTextAttribute(hConsole, color);
50}
51
Diego Novillod2938e42017-11-08 12:40:02 -050052static void SetConsoleForegroundColor(WORD color) {
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020053 SetConsoleForegroundColorPrimary(GetStdHandle(STD_OUTPUT_HANDLE), color);
54 SetConsoleForegroundColorPrimary(GetStdHandle(STD_ERROR_HANDLE), color);
55}
56
Lei Zhang1a0334e2015-11-02 09:41:20 -050057clr::reset::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050058 if (isPrint) {
59 SetConsoleForegroundColor(0xf);
60 return "";
61 }
62 return "\x1b[0m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010063}
64
Lei Zhang1a0334e2015-11-02 09:41:20 -050065clr::grey::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050066 if (isPrint) {
67 SetConsoleForegroundColor(FOREGROUND_INTENSITY);
68 return "";
69 }
70 return "\x1b[1;30m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010071}
72
Lei Zhang1a0334e2015-11-02 09:41:20 -050073clr::red::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050074 if (isPrint) {
75 SetConsoleForegroundColor(FOREGROUND_RED);
76 return "";
77 }
78 return "\x1b[31m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010079}
80
Lei Zhang1a0334e2015-11-02 09:41:20 -050081clr::green::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050082 if (isPrint) {
83 SetConsoleForegroundColor(FOREGROUND_GREEN);
84 return "";
85 }
86 return "\x1b[32m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010087}
88
Lei Zhang1a0334e2015-11-02 09:41:20 -050089clr::yellow::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050090 if (isPrint) {
91 SetConsoleForegroundColor(FOREGROUND_RED | FOREGROUND_GREEN);
92 return "";
93 }
94 return "\x1b[33m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010095}
96
Lei Zhang1a0334e2015-11-02 09:41:20 -050097clr::blue::operator const char*() {
David Neto01677582017-08-24 10:34:00 -040098 // Blue all by itself is hard to see against a black background (the
99 // default on command shell), or a medium blue background (the default
100 // on PowerShell). So increase its intensity.
Lei Zhang13795352017-11-30 16:35:22 -0500101
102 if (isPrint) {
103 SetConsoleForegroundColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
104 return "";
105 }
106 return "\x1b[94m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100107}
David Neto01656362015-11-20 10:44:41 -0500108
dan sinclair3dad1cd2018-07-07 09:38:00 -0400109} // namespace spvtools
Ben Vanik01c8d7a2015-11-22 08:32:53 -0800110#else
dan sinclair3dad1cd2018-07-07 09:38:00 -0400111namespace spvtools {
Ben Vanik01c8d7a2015-11-22 08:32:53 -0800112
113clr::reset::operator const char*() { return ""; }
114
115clr::grey::operator const char*() { return ""; }
116
117clr::red::operator const char*() { return ""; }
118
119clr::green::operator const char*() { return ""; }
120
121clr::yellow::operator const char*() { return ""; }
122
123clr::blue::operator const char*() { return ""; }
124
dan sinclair3dad1cd2018-07-07 09:38:00 -0400125} // namespace spvtools
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100126#endif