print.cpp 3.4 KB

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