PythonTraceMessageSink.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Debug/TraceMessageBus.h>
  10. #include <AzToolsFramework/API/EditorPythonConsoleBus.h>
  11. namespace UnitTest
  12. {
  13. /** Trace message handler to track messages during tests
  14. */
  15. struct PythonTraceMessageSink final
  16. : public AZ::Debug::TraceMessageBus::Handler
  17. , public AzToolsFramework::EditorPythonConsoleNotificationBus::Handler
  18. {
  19. PythonTraceMessageSink()
  20. {
  21. AZ::Debug::TraceMessageBus::Handler::BusConnect();
  22. AzToolsFramework::EditorPythonConsoleNotificationBus::Handler::BusConnect();
  23. }
  24. ~PythonTraceMessageSink()
  25. {
  26. AZ::Debug::TraceMessageBus::Handler::BusDisconnect();
  27. AzToolsFramework::EditorPythonConsoleNotificationBus::Handler::BusDisconnect();
  28. }
  29. // returns an index-tag for a message type that will be counted inside of m_evaluationMap
  30. using EvaluateMessageFunc = AZStd::function<int(const char* window, const char* message)>;
  31. EvaluateMessageFunc m_evaluateMessage;
  32. using EvaluationMap = AZStd::unordered_map<int, int>; // tag to count
  33. EvaluationMap m_evaluationMap;
  34. AZStd::mutex m_lock;
  35. void CleanUp()
  36. {
  37. AZStd::lock_guard<decltype(m_lock)> lock(m_lock);
  38. m_evaluateMessage = {};
  39. m_evaluationMap.clear();
  40. }
  41. //////////////////////////////////////////////////////////////////////////
  42. // TraceMessageBus
  43. bool OnPrintf(const char* window, const char* message) override
  44. {
  45. return OnOutput(window, message);
  46. }
  47. bool OnOutput(const char* window, const char* message) override
  48. {
  49. AZStd::lock_guard<decltype(m_lock)> lock(m_lock);
  50. if (m_evaluateMessage)
  51. {
  52. int key = m_evaluateMessage(window, message);
  53. if (key != 0)
  54. {
  55. auto entryIt = m_evaluationMap.find(key);
  56. if (m_evaluationMap.end() == entryIt)
  57. {
  58. m_evaluationMap[key] = 1;
  59. }
  60. else
  61. {
  62. m_evaluationMap[key] = m_evaluationMap[key] + 1;
  63. }
  64. }
  65. }
  66. return false;
  67. }
  68. //////////////////////////////////////////////////////////////////////////
  69. // AzToolsFramework::EditorPythonConsoleNotificationBus
  70. void OnTraceMessage([[maybe_unused]] AZStd::string_view message) override
  71. {
  72. AZ_TracePrintf("python", "%.*s", static_cast<int>(message.size()), message.data());
  73. }
  74. void OnErrorMessage([[maybe_unused]] AZStd::string_view message) override
  75. {
  76. AZ_Error("python", false, "%.*s", static_cast<int>(message.size()), message.data());
  77. }
  78. void OnExceptionMessage([[maybe_unused]] AZStd::string_view message) override
  79. {
  80. AZ_Error("python", false, "EXCEPTION: %.*s", static_cast<int>(message.size()), message.data());
  81. }
  82. };
  83. }