print.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include "source/print.h"
  15. #if defined(SPIRV_WINDOWS)
  16. #include <windows.h>
  17. namespace spvtools {
  18. static void SetConsoleForegroundColorPrimary(HANDLE hConsole, WORD color) {
  19. // Get screen buffer information from console handle
  20. CONSOLE_SCREEN_BUFFER_INFO bufInfo;
  21. GetConsoleScreenBufferInfo(hConsole, &bufInfo);
  22. // Get background color
  23. color = WORD(color | (bufInfo.wAttributes & 0xfff0));
  24. // Set foreground color
  25. SetConsoleTextAttribute(hConsole, color);
  26. }
  27. static void SetConsoleForegroundColor(WORD color) {
  28. SetConsoleForegroundColorPrimary(GetStdHandle(STD_OUTPUT_HANDLE), color);
  29. SetConsoleForegroundColorPrimary(GetStdHandle(STD_ERROR_HANDLE), color);
  30. }
  31. clr::reset::operator const char*() {
  32. if (isPrint) {
  33. SetConsoleForegroundColor(0xf);
  34. return "";
  35. }
  36. return "\x1b[0m";
  37. }
  38. clr::grey::operator const char*() {
  39. if (isPrint) {
  40. SetConsoleForegroundColor(FOREGROUND_INTENSITY);
  41. return "";
  42. }
  43. return "\x1b[1;30m";
  44. }
  45. clr::red::operator const char*() {
  46. if (isPrint) {
  47. SetConsoleForegroundColor(FOREGROUND_RED);
  48. return "";
  49. }
  50. return "\x1b[31m";
  51. }
  52. clr::green::operator const char*() {
  53. if (isPrint) {
  54. SetConsoleForegroundColor(FOREGROUND_GREEN);
  55. return "";
  56. }
  57. return "\x1b[32m";
  58. }
  59. clr::yellow::operator const char*() {
  60. if (isPrint) {
  61. SetConsoleForegroundColor(FOREGROUND_RED | FOREGROUND_GREEN);
  62. return "";
  63. }
  64. return "\x1b[33m";
  65. }
  66. clr::blue::operator const char*() {
  67. // Blue all by itself is hard to see against a black background (the
  68. // default on command shell), or a medium blue background (the default
  69. // on PowerShell). So increase its intensity.
  70. if (isPrint) {
  71. SetConsoleForegroundColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
  72. return "";
  73. }
  74. return "\x1b[94m";
  75. }
  76. } // namespace spvtools
  77. #else
  78. namespace spvtools {
  79. clr::reset::operator const char*() { return "\x1b[0m"; }
  80. clr::grey::operator const char*() { return "\x1b[1;30m"; }
  81. clr::red::operator const char*() { return "\x1b[31m"; }
  82. clr::green::operator const char*() { return "\x1b[32m"; }
  83. clr::yellow::operator const char*() { return "\x1b[33m"; }
  84. clr::blue::operator const char*() { return "\x1b[34m"; }
  85. } // namespace spvtools
  86. #endif