blob: 5336df630d0570db4f6071396f602739c07f77ab [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
15#include "print.h"
16
Diego Novillod2938e42017-11-08 12:40:02 -050017#if defined(SPIRV_ANDROID) || defined(SPIRV_LINUX) || defined(SPIRV_MAC) || \
18 defined(SPIRV_FREEBSD)
David Neto01656362015-11-20 10:44:41 -050019namespace libspirv {
20
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
33} // namespace libspirv
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
David Neto01656362015-11-20 10:44:41 -050037namespace libspirv {
38
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*() {
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020057 SetConsoleForegroundColor(0xf);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010058 return "";
59}
60
Lei Zhang1a0334e2015-11-02 09:41:20 -050061clr::grey::operator const char*() {
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020062 SetConsoleForegroundColor(FOREGROUND_INTENSITY);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010063 return "";
64}
65
Lei Zhang1a0334e2015-11-02 09:41:20 -050066clr::red::operator const char*() {
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020067 SetConsoleForegroundColor(FOREGROUND_RED);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010068 return "";
69}
70
Lei Zhang1a0334e2015-11-02 09:41:20 -050071clr::green::operator const char*() {
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020072 SetConsoleForegroundColor(FOREGROUND_GREEN);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010073 return "";
74}
75
Lei Zhang1a0334e2015-11-02 09:41:20 -050076clr::yellow::operator const char*() {
Lukas Hermanns4fe8e382017-08-20 14:11:50 +020077 SetConsoleForegroundColor(FOREGROUND_RED | FOREGROUND_GREEN);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010078 return "";
79}
80
Lei Zhang1a0334e2015-11-02 09:41:20 -050081clr::blue::operator const char*() {
David Neto01677582017-08-24 10:34:00 -040082 // Blue all by itself is hard to see against a black background (the
83 // default on command shell), or a medium blue background (the default
84 // on PowerShell). So increase its intensity.
85 SetConsoleForegroundColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010086 return "";
87}
David Neto01656362015-11-20 10:44:41 -050088
89} // namespace libspirv
Ben Vanik01c8d7a2015-11-22 08:32:53 -080090#else
91namespace libspirv {
92
93clr::reset::operator const char*() { return ""; }
94
95clr::grey::operator const char*() { return ""; }
96
97clr::red::operator const char*() { return ""; }
98
99clr::green::operator const char*() { return ""; }
100
101clr::yellow::operator const char*() { return ""; }
102
103clr::blue::operator const char*() { return ""; }
104
105} // namespace libspirv
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100106#endif