blob: 6c94e2b7fc879801f99e2da54f6660c50b194f4f [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) || \
Brad Smith5f45f792022-09-02 15:30:09 -040019 defined(SPIRV_OPENBSD) || defined(SPIRV_EMSCRIPTEN) || \
20 defined(SPIRV_FUCHSIA) || defined(SPIRV_GNU)
dan sinclair3dad1cd2018-07-07 09:38:00 -040021namespace spvtools {
David Neto01656362015-11-20 10:44:41 -050022
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040023clr::reset::operator const char*() { return "\x1b[0m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010024
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040025clr::grey::operator const char*() { return "\x1b[1;30m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010026
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040027clr::red::operator const char*() { return "\x1b[31m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010028
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040029clr::green::operator const char*() { return "\x1b[32m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010030
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040031clr::yellow::operator const char*() { return "\x1b[33m"; }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010032
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -040033clr::blue::operator const char*() { return "\x1b[34m"; }
David Neto01656362015-11-20 10:44:41 -050034
dan sinclair3dad1cd2018-07-07 09:38:00 -040035} // namespace spvtools
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010036#elif defined(SPIRV_WINDOWS)
David Neto7c58c1d2016-02-29 01:01:04 -050037#include <windows.h>
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010038
dan sinclair3dad1cd2018-07-07 09:38:00 -040039namespace spvtools {
David Neto01656362015-11-20 10:44:41 -050040
Diego Novillod2938e42017-11-08 12:40:02 -050041static void SetConsoleForegroundColorPrimary(HANDLE hConsole, WORD color) {
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020042 // Get screen buffer information from console handle
43 CONSOLE_SCREEN_BUFFER_INFO bufInfo;
44 GetConsoleScreenBufferInfo(hConsole, &bufInfo);
45
46 // Get background color
Andrey Tuganov32cf85d2017-08-28 18:36:52 -040047 color = WORD(color | (bufInfo.wAttributes & 0xfff0));
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020048
49 // Set foreground color
50 SetConsoleTextAttribute(hConsole, color);
51}
52
Diego Novillod2938e42017-11-08 12:40:02 -050053static void SetConsoleForegroundColor(WORD color) {
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020054 SetConsoleForegroundColorPrimary(GetStdHandle(STD_OUTPUT_HANDLE), color);
55 SetConsoleForegroundColorPrimary(GetStdHandle(STD_ERROR_HANDLE), color);
56}
57
Lei Zhang1a0334e2015-11-02 09:41:20 -050058clr::reset::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050059 if (isPrint) {
60 SetConsoleForegroundColor(0xf);
61 return "";
62 }
63 return "\x1b[0m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010064}
65
Lei Zhang1a0334e2015-11-02 09:41:20 -050066clr::grey::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050067 if (isPrint) {
68 SetConsoleForegroundColor(FOREGROUND_INTENSITY);
69 return "";
70 }
71 return "\x1b[1;30m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010072}
73
Lei Zhang1a0334e2015-11-02 09:41:20 -050074clr::red::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050075 if (isPrint) {
76 SetConsoleForegroundColor(FOREGROUND_RED);
77 return "";
78 }
79 return "\x1b[31m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010080}
81
Lei Zhang1a0334e2015-11-02 09:41:20 -050082clr::green::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050083 if (isPrint) {
84 SetConsoleForegroundColor(FOREGROUND_GREEN);
85 return "";
86 }
87 return "\x1b[32m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010088}
89
Lei Zhang1a0334e2015-11-02 09:41:20 -050090clr::yellow::operator const char*() {
Lei Zhang13795352017-11-30 16:35:22 -050091 if (isPrint) {
92 SetConsoleForegroundColor(FOREGROUND_RED | FOREGROUND_GREEN);
93 return "";
94 }
95 return "\x1b[33m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010096}
97
Lei Zhang1a0334e2015-11-02 09:41:20 -050098clr::blue::operator const char*() {
David Neto01677582017-08-24 10:34:00 -040099 // 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.
Lei Zhang13795352017-11-30 16:35:22 -0500102
103 if (isPrint) {
104 SetConsoleForegroundColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
105 return "";
106 }
107 return "\x1b[94m";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100108}
David Neto01656362015-11-20 10:44:41 -0500109
dan sinclair3dad1cd2018-07-07 09:38:00 -0400110} // namespace spvtools
Ben Vanik01c8d7a2015-11-22 08:32:53 -0800111#else
dan sinclair3dad1cd2018-07-07 09:38:00 -0400112namespace spvtools {
Ben Vanik01c8d7a2015-11-22 08:32:53 -0800113
114clr::reset::operator const char*() { return ""; }
115
116clr::grey::operator const char*() { return ""; }
117
118clr::red::operator const char*() { return ""; }
119
120clr::green::operator const char*() { return ""; }
121
122clr::yellow::operator const char*() { return ""; }
123
124clr::blue::operator const char*() { return ""; }
125
dan sinclair3dad1cd2018-07-07 09:38:00 -0400126} // namespace spvtools
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100127#endif