print.cpp 3.2 KB

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