console.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #ifdef _WIN32
  5. #include <windows.h>
  6. #include <wincon.h>
  7. #endif
  8. #include "common/logging/backend.h"
  9. #include "yuzu/debugger/console.h"
  10. #include "yuzu/ui_settings.h"
  11. namespace Debugger {
  12. void ToggleConsole() {
  13. static bool console_shown = false;
  14. if (console_shown == UISettings::values.show_console) {
  15. return;
  16. } else {
  17. console_shown = UISettings::values.show_console;
  18. }
  19. #if defined(_WIN32) && !defined(_DEBUG)
  20. FILE* temp;
  21. if (UISettings::values.show_console) {
  22. if (AllocConsole()) {
  23. // The first parameter for freopen_s is a out parameter, so we can just ignore it
  24. freopen_s(&temp, "CONIN$", "r", stdin);
  25. freopen_s(&temp, "CONOUT$", "w", stdout);
  26. freopen_s(&temp, "CONOUT$", "w", stderr);
  27. Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
  28. }
  29. } else {
  30. if (FreeConsole()) {
  31. // In order to close the console, we have to also detach the streams on it.
  32. // Just redirect them to NUL if there is no console window
  33. Log::RemoveBackend(Log::ColorConsoleBackend::Name());
  34. freopen_s(&temp, "NUL", "r", stdin);
  35. freopen_s(&temp, "NUL", "w", stdout);
  36. freopen_s(&temp, "NUL", "w", stderr);
  37. }
  38. }
  39. #else
  40. if (UISettings::values.show_console) {
  41. Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
  42. } else {
  43. Log::RemoveBackend(Log::ColorConsoleBackend::Name());
  44. }
  45. #endif
  46. }
  47. } // namespace Debugger