StartupTraceHandler.h 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. namespace SandboxEditor
  11. {
  12. /// Class to display errors during startup.
  13. /// This is a system lifted from CryEdit.cpp and made thread safe.
  14. /// It existed to handle errors that occur during editor startup, before
  15. /// the regular error handler is loaded and available.
  16. class StartupTraceHandler
  17. : public AZ::Debug::TraceMessageBus::Handler
  18. {
  19. public:
  20. StartupTraceHandler();
  21. ~StartupTraceHandler();
  22. //////////////////////////////////////////////////////////////////////
  23. // AZ::Debug::TraceMessageBus::Handler overrides
  24. //////////////////////////////////////////////////////////////////////
  25. bool OnPreAssert(const char* fileName, int line, const char* func, const char* message) override;
  26. bool OnException(const char* message) override;
  27. bool OnPreError(const char* window, const char* fileName, int line, const char* func, const char* message) override;
  28. bool OnPreWarning(const char* window, const char* fileName, int line, const char* func, const char* message) override;
  29. //////////////////////////////////////////////////////////////////////
  30. // Public interface
  31. //////////////////////////////////////////////////////////////////////
  32. //! Tells the trace handler to start collecting messages, instead of displaying them as they occur.
  33. //! Connects to the message bus to make sure collection can occur.
  34. void StartCollection();
  35. //! Ends collection, and displays all collected messages in one dialog.
  36. void EndCollectionAndShowCollectedErrors();
  37. //! @return if any errors occured during level load.
  38. bool HasAnyErrors() const { return m_errors.size() > 0; }
  39. //! Connects the trace handler to the trace message bus.
  40. void ConnectToMessageBus();
  41. //! Disconnects the trace handler from the trace message bus.
  42. void DisconnectFromMessageBus();
  43. //! @return if the trace handler is connected to the message bus.
  44. bool IsConnectedToMessageBus() const;
  45. //! Sets whether to display the error window or not
  46. void SetShowWindow(bool showWindow) { m_showWindow = showWindow; }
  47. private:
  48. /// The display behavior for messages.
  49. /// Some, like warnings, are only showed in a shared message dialog.
  50. /// Others, like Exceptions, are likely fatal and need to be displayed immediately. The program
  51. /// state after a fatal error may not be recoverable, so these messages can't be collected
  52. /// to be displayed later.
  53. enum class MessageDisplayBehavior
  54. {
  55. AlwaysShow, ///< Always show this message, when collecting or not collecting messages.
  56. ShowWhenNotCollecting, ///< Only show this message when collecting is not active, otherwise
  57. ///< the collection system will show this later.
  58. OnlyCollect, ///< Only collect this message, don't show it if not collecting.
  59. };
  60. // Returns whether or not the message was handled
  61. bool OnMessage(
  62. const char *message,
  63. AZStd::list<QString>* messageList,
  64. MessageDisplayBehavior messageDisplayBehavior);
  65. void ShowMessageBox(const QString& message);
  66. mutable AZStd::recursive_mutex m_mutex; ///< Messages can come in from multiple threads, so use a mutex to lock things when necessary.
  67. AZStd::list<QString> m_errors; ///< The list of errors that occured while collecting.
  68. AZStd::list<QString> m_warnings; ///< The list of warnings that occured while collecting.
  69. bool m_isCollecting = false; ///< Tracks if the trace handler is collecting messages or displaying them as they occur.
  70. bool m_showWindow = true; ///< Whether it should display the window
  71. ///< The list of messages that occured on a thread that can't display a Qt popup.
  72. ///< These are intended for 1 at a time popups.
  73. AZStd::list<QString> m_mainThreadMessages;
  74. };
  75. }