ErrorReport.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "EditorDefs.h"
  9. #include "ErrorReport.h"
  10. // Editor
  11. #include "ErrorReportDialog.h"
  12. //////////////////////////////////////////////////////////////////////////
  13. QString CErrorRecord::GetErrorText() const
  14. {
  15. QString str = error.trimmed();
  16. const char* sModuleName = "";
  17. // Module name
  18. switch (module)
  19. {
  20. case VALIDATOR_MODULE_UNKNOWN:
  21. sModuleName = "";
  22. break;
  23. case VALIDATOR_MODULE_RENDERER:
  24. sModuleName = "Renderer";
  25. break;
  26. case VALIDATOR_MODULE_3DENGINE:
  27. sModuleName = "Engine";
  28. break;
  29. case VALIDATOR_MODULE_SYSTEM:
  30. sModuleName = "System";
  31. break;
  32. case VALIDATOR_MODULE_AUDIO:
  33. sModuleName = "Audio";
  34. break;
  35. case VALIDATOR_MODULE_MOVIE:
  36. sModuleName = "Movie";
  37. break;
  38. case VALIDATOR_MODULE_EDITOR:
  39. sModuleName = "Editor";
  40. break;
  41. case VALIDATOR_MODULE_NETWORK:
  42. sModuleName = "Network";
  43. break;
  44. case VALIDATOR_MODULE_PHYSICS:
  45. sModuleName = "Physics";
  46. break;
  47. case VALIDATOR_MODULE_FEATURETESTS:
  48. sModuleName = "FeatureTests";
  49. break;
  50. case VALIDATOR_MODULE_SHINE:
  51. sModuleName = "UI";
  52. break;
  53. }
  54. str = QStringLiteral("[%1]\t[%2]\t%3").arg(count, 2).arg(QString(sModuleName).leftJustified(6), error).trimmed();
  55. if (!file.isEmpty())
  56. {
  57. str += QString("\t") + file;
  58. }
  59. else
  60. {
  61. str += QString("\t ");
  62. }
  63. if (pObject)
  64. {
  65. str += QString("\t Object=\"") + pObject->GetName() + "\"";
  66. }
  67. return str;
  68. }
  69. //////////////////////////////////////////////////////////////////////////
  70. // CError Report.
  71. //////////////////////////////////////////////////////////////////////////
  72. CErrorReport::CErrorReport()
  73. {
  74. m_errors.reserve(100);
  75. m_bImmediateMode = true;
  76. m_bShowErrors = true;
  77. m_pObject = nullptr;
  78. m_pParticle = nullptr;
  79. }
  80. //////////////////////////////////////////////////////////////////////////
  81. void CErrorReport::ReportError(CErrorRecord& err)
  82. {
  83. static bool bNoRecurse = false;
  84. if (bNoRecurse)
  85. {
  86. return;
  87. }
  88. bNoRecurse = true;
  89. if (m_bImmediateMode)
  90. {
  91. if (err.module == VALIDATOR_MODULE_EDITOR && err.severity == static_cast<int>(VALIDATOR_ERROR))
  92. {
  93. Warning(err.error.toUtf8().data());
  94. }
  95. else
  96. {
  97. // Show dialog if first character of warning is !.
  98. if (!err.error.isEmpty() && err.error[0] == '!')
  99. {
  100. Warning(err.error.toUtf8().data());
  101. }
  102. else
  103. {
  104. m_errors.push_back(err);
  105. }
  106. }
  107. }
  108. else
  109. {
  110. if (err.pObject == nullptr && m_pObject != nullptr)
  111. {
  112. err.pObject = m_pObject;
  113. }
  114. m_errors.push_back(err);
  115. }
  116. bNoRecurse = false;
  117. }
  118. //////////////////////////////////////////////////////////////////////////
  119. void CErrorReport::Clear()
  120. {
  121. m_errors.clear();
  122. }
  123. //////////////////////////////////////////////////////////////////////////
  124. inline bool SortErrorsByModule(const CErrorRecord& e1, const CErrorRecord& e2)
  125. {
  126. if (e1.module == e2.module)
  127. {
  128. if (e1.error == e2.error)
  129. {
  130. return e1.file < e2.file;
  131. }
  132. else
  133. {
  134. return e1.error < e2.error;
  135. }
  136. }
  137. return e1.module < e2.module;
  138. }
  139. //////////////////////////////////////////////////////////////////////////
  140. void CErrorReport::Display()
  141. {
  142. if (m_errors.empty() || !m_bShowErrors)
  143. {
  144. SetImmediateMode(true);
  145. return;
  146. }
  147. // Sort by module.
  148. std::sort(m_errors.begin(), m_errors.end(), SortErrorsByModule);
  149. std::vector<CErrorRecord> errorList;
  150. errorList.swap(m_errors);
  151. m_errors.reserve(errorList.size());
  152. QString prevName = "";
  153. QString prevFile = "";
  154. for (int i = 0; i < errorList.size(); i++)
  155. {
  156. CErrorRecord& err = errorList[i];
  157. if (err.error != prevName || err.file != prevFile)
  158. {
  159. err.count = 1;
  160. m_errors.push_back(err);
  161. }
  162. else if (!m_errors.empty())
  163. {
  164. m_errors.back().count++;
  165. }
  166. prevName = err.error;
  167. prevFile = err.file;
  168. }
  169. // Log all errors.
  170. CryLogAlways("========================= Errors =========================");
  171. for (int i = 0; i < m_errors.size(); i++)
  172. {
  173. CErrorRecord& err = m_errors[i];
  174. QString str = err.GetErrorText();
  175. CryLogAlways("%3d) %s", i, str.toUtf8().data());
  176. }
  177. CryLogAlways("========================= End Errors =========================");
  178. ICVar* const noErrorReportWindowCVar = gEnv && gEnv->pConsole ? gEnv->pConsole->GetCVar("sys_no_error_report_window") : nullptr;
  179. if (noErrorReportWindowCVar && noErrorReportWindowCVar->GetIVal() == 0)
  180. {
  181. CErrorReportDialog::Open(this);
  182. }
  183. SetImmediateMode(true);
  184. }
  185. //////////////////////////////////////////////////////////////////////////
  186. bool CErrorReport::IsEmpty() const
  187. {
  188. return m_errors.empty();
  189. }
  190. //////////////////////////////////////////////////////////////////////////
  191. CErrorRecord& CErrorReport::GetError(int i)
  192. {
  193. assert(i >= 0 && i < m_errors.size());
  194. return m_errors[i];
  195. };
  196. //////////////////////////////////////////////////////////////////////////
  197. void CErrorReport::SetImmediateMode(bool bEnable)
  198. {
  199. if (bEnable != m_bImmediateMode)
  200. {
  201. Clear();
  202. m_bImmediateMode = bEnable;
  203. }
  204. }
  205. //////////////////////////////////////////////////////////////////////////
  206. void CErrorReport::SetCurrentValidatorObject(CBaseObject* pObject)
  207. {
  208. m_pObject = pObject;
  209. }
  210. //////////////////////////////////////////////////////////////////////////
  211. void CErrorReport::SetCurrentFile(const QString& file)
  212. {
  213. m_currentFilename = file;
  214. }