3
0

LogPanel.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #include "LogPanel.h"
  9. #include <QDateTime>
  10. #include <QTimer>
  11. #include <QTableView>
  12. #include "Editor/View/Widgets/ui_LogPanel.h"
  13. #include <Editor/View/Dialogs/SettingsDialog.h>
  14. #include <AzQtComponents/Components/Widgets/TabWidget.h>
  15. namespace ScriptCanvasEditor
  16. {
  17. namespace Widget
  18. {
  19. LogPanel::LogPanel(QWidget* parent /*= nullptr*/)
  20. : AzToolsFramework::LogPanel::BaseLogPanel(parent)
  21. {
  22. ScriptCanvasEditor::GeneralGraphEventBus::Handler::BusConnect();
  23. }
  24. LogPanel::~LogPanel()
  25. {
  26. ScriptCanvasEditor::GeneralGraphEventBus::Handler::BusDisconnect();
  27. }
  28. void LogPanel::OnBuildGameEntity(const AZStd::string& name, const AZ::EntityId& editGraphId, const ScriptCanvas::ScriptCanvasId& scriptCanvasId)
  29. {
  30. m_scriptCanvasId = scriptCanvasId;
  31. AZStd::intrusive_ptr<Settings> settings = AZ::UserSettings::CreateFind<Settings>(AZ::Crc32(editGraphId.ToString().c_str()), AZ::UserSettings::CT_LOCAL);
  32. if (settings->m_enableLogging)
  33. {
  34. AzToolsFramework::LogPanel::TabSettings settingsTab(name.c_str(), "Script Canvas", "", true, true, true, true);
  35. AddLogTab(settingsTab);
  36. }
  37. }
  38. QWidget* LogPanel::CreateTab(const AzToolsFramework::LogPanel::TabSettings& settings)
  39. {
  40. return new LogTab(this, m_scriptCanvasId, settings);
  41. }
  42. LogPanelWidget::LogPanelWidget(QWidget* parent)
  43. : AzQtComponents::StyledDockWidget(parent)
  44. , ui(new Ui::LogPanel())
  45. {
  46. ui->setupUi(this);
  47. setWindowTitle(tr("Log"));
  48. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  49. setMinimumWidth(200);
  50. setMinimumHeight(40);
  51. ui->layout->addWidget(new LogPanel(this));
  52. }
  53. LogTab::LogTab(QWidget* pParent, const ScriptCanvas::ScriptCanvasId& scriptCanvasId, [[maybe_unused]] const AzToolsFramework::LogPanel::TabSettings& in_settings)
  54. : AzToolsFramework::LogPanel::BaseLogView(pParent)
  55. {
  56. QAction* actionClear = new QAction(tr("Clear"), this);
  57. connect(actionClear, SIGNAL(triggered()), this, SLOT(Clear()));
  58. addAction(actionClear);
  59. m_alreadyQueuedDrainMessage = false;
  60. ConnectModelToView(new AzToolsFramework::LogPanel::RingBufferLogDataModel(m_ptrLogView));
  61. ScriptCanvas::LogNotificationBus::Handler::BusConnect(scriptCanvasId);
  62. Clear();
  63. }
  64. LogTab::~LogTab()
  65. {
  66. ScriptCanvas::LogNotificationBus::Handler::BusDisconnect();
  67. }
  68. void LogTab::LogMessage(const AZStd::string& message)
  69. {
  70. AzToolsFramework::Logging::LogLine line(AZStd::string::format("%s", message.c_str()).c_str(), "Log", AzToolsFramework::Logging::LogLine::TYPE_MESSAGE, QDateTime::currentMSecsSinceEpoch());
  71. m_bufferedLines.push(line);
  72. CommitAddedLines();
  73. }
  74. static int s_delayBetweenTraceprintfUpdates = 250; // milliseconds between pumping the traceprintf messages, lower will eat more performance but be more responsive
  75. void LogTab::CommitAddedLines()
  76. {
  77. bool wasQueued = m_alreadyQueuedDrainMessage.exchange(true, AZStd::memory_order_acq_rel);
  78. if (!wasQueued)
  79. {
  80. QTimer::singleShot(s_delayBetweenTraceprintfUpdates, this, &LogTab::DrainMessages);
  81. }
  82. }
  83. void LogTab::Clear()
  84. {
  85. AzToolsFramework::LogPanel::RingBufferLogDataModel* pModel = qobject_cast<AzToolsFramework::LogPanel::RingBufferLogDataModel*>(m_ptrLogView->model());
  86. if (pModel)
  87. {
  88. pModel->Clear();
  89. }
  90. }
  91. void LogTab::DrainMessages()
  92. {
  93. m_alreadyQueuedDrainMessage = false;
  94. bool wasAtMaxScroll = IsAtMaxScroll();
  95. AzToolsFramework::LogPanel::RingBufferLogDataModel* pModel = ((AzToolsFramework::LogPanel::RingBufferLogDataModel*)(m_ptrLogView->model()));
  96. AzToolsFramework::Logging::LogLine currentLine;
  97. bool openedQuery = false;
  98. bool foundLine = false;
  99. do
  100. {
  101. {
  102. if (m_bufferedLines.empty())
  103. {
  104. foundLine = false;
  105. }
  106. else
  107. {
  108. foundLine = true;
  109. currentLine = AZStd::move(m_bufferedLines.front());
  110. m_bufferedLines.pop();
  111. }
  112. }
  113. if (foundLine)
  114. {
  115. if (!openedQuery)
  116. {
  117. openedQuery = true;
  118. }
  119. pModel->AppendLine(currentLine);
  120. }
  121. } while (foundLine); // keep doing this as long as there's line in the buffer.
  122. if (openedQuery)
  123. {
  124. pModel->CommitAdd();
  125. if (wasAtMaxScroll)
  126. {
  127. m_ptrLogView->scrollToBottom();
  128. }
  129. }
  130. }
  131. #include <Editor/View/Widgets/moc_LogPanel.cpp>
  132. }
  133. }