text_formatter.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <array>
  4. #include <cstdio>
  5. #ifdef _WIN32
  6. #include <windows.h>
  7. #endif
  8. #include "common/assert.h"
  9. #include "common/logging/filter.h"
  10. #include "common/logging/log.h"
  11. #include "common/logging/log_entry.h"
  12. #include "common/logging/text_formatter.h"
  13. namespace Common::Log {
  14. std::string FormatLogMessage(const Entry& entry) {
  15. unsigned int time_seconds = static_cast<unsigned int>(entry.timestamp.count() / 1000000);
  16. unsigned int time_fractional = static_cast<unsigned int>(entry.timestamp.count() % 1000000);
  17. const char* class_name = GetLogClassName(entry.log_class);
  18. const char* level_name = GetLevelName(entry.log_level);
  19. return fmt::format("[{:4d}.{:06d}] {} <{}> {}:{}:{}: {}", time_seconds, time_fractional,
  20. class_name, level_name, entry.filename, entry.function, entry.line_num,
  21. entry.message);
  22. }
  23. void PrintMessage(const Entry& entry) {
  24. const auto str = FormatLogMessage(entry).append(1, '\n');
  25. fputs(str.c_str(), stderr);
  26. }
  27. void PrintColoredMessage(const Entry& entry) {
  28. #ifdef _WIN32
  29. HANDLE console_handle = GetStdHandle(STD_ERROR_HANDLE);
  30. if (console_handle == INVALID_HANDLE_VALUE) {
  31. return;
  32. }
  33. CONSOLE_SCREEN_BUFFER_INFO original_info = {};
  34. GetConsoleScreenBufferInfo(console_handle, &original_info);
  35. WORD color = 0;
  36. switch (entry.log_level) {
  37. case Level::Trace: // Grey
  38. color = FOREGROUND_INTENSITY;
  39. break;
  40. case Level::Debug: // Cyan
  41. color = FOREGROUND_GREEN | FOREGROUND_BLUE;
  42. break;
  43. case Level::Info: // Bright gray
  44. color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
  45. break;
  46. case Level::Warning: // Bright yellow
  47. color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
  48. break;
  49. case Level::Error: // Bright red
  50. color = FOREGROUND_RED | FOREGROUND_INTENSITY;
  51. break;
  52. case Level::Critical: // Bright magenta
  53. color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
  54. break;
  55. case Level::Count:
  56. UNREACHABLE();
  57. }
  58. SetConsoleTextAttribute(console_handle, color);
  59. #else
  60. #define ESC "\x1b"
  61. const char* color = "";
  62. switch (entry.log_level) {
  63. case Level::Trace: // Grey
  64. color = ESC "[1;30m";
  65. break;
  66. case Level::Debug: // Cyan
  67. color = ESC "[0;36m";
  68. break;
  69. case Level::Info: // Bright gray
  70. color = ESC "[0;37m";
  71. break;
  72. case Level::Warning: // Bright yellow
  73. color = ESC "[1;33m";
  74. break;
  75. case Level::Error: // Bright red
  76. color = ESC "[1;31m";
  77. break;
  78. case Level::Critical: // Bright magenta
  79. color = ESC "[1;35m";
  80. break;
  81. case Level::Count:
  82. UNREACHABLE();
  83. }
  84. fputs(color, stderr);
  85. #endif
  86. PrintMessage(entry);
  87. #ifdef _WIN32
  88. SetConsoleTextAttribute(console_handle, original_info.wAttributes);
  89. #else
  90. fputs(ESC "[0m", stderr);
  91. #undef ESC
  92. #endif
  93. }
  94. } // namespace Common::Log