ErrorHandler.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * author: Patrick-Christopher Mattulat
  3. * dev-tools: JetBrains CLion
  4. * created: 19.08.2018
  5. * last updated: 18.10.2018
  6. */
  7. #include <SDL.h>
  8. #include <sstream>
  9. #include "ErrorHandler.h"
  10. #include "../Global.h"
  11. #include "ErrorMessage.h"
  12. #include "ErrorList.h"
  13. #include "ErrorCodes.h"
  14. //////////////////////////////////////////////////
  15. //////////////////////////////////////////////////
  16. //// constructor / destructor
  17. //////////////////////////////////////////////////
  18. //////////////////////////////////////////////////
  19. ErrorHandler::ErrorHandler(std::string& fileName, std::shared_ptr<LogFileHandler>& engineLogFileHandler)
  20. {
  21. this->logFilePath = fileName;
  22. this->logFileHandler = engineLogFileHandler;
  23. }
  24. ErrorHandler::~ErrorHandler() = default;
  25. //////////////////////////////////////////////////
  26. //////////////////////////////////////////////////
  27. //// private
  28. //////////////////////////////////////////////////
  29. //////////////////////////////////////////////////
  30. void ErrorHandler::writeToLogFile(int errorCode, const std::string& title, const std::string& message)
  31. {
  32. std::stringstream lineStream;
  33. std::string line;
  34. if(!errorCode) {
  35. lineStream << " <p class=\"message noError\">" << title << ": " << message << "</p>";
  36. }
  37. else {
  38. lineStream << " <p class=\"message error\">" << title << ": " << message << "</p>";
  39. }
  40. line = lineStream.str();
  41. this->logFileHandler->writeToLogFile(this->logFilePath, 29, line);
  42. }
  43. //////////////////////////////////////////////////
  44. //////////////////////////////////////////////////
  45. //// public
  46. //////////////////////////////////////////////////
  47. //////////////////////////////////////////////////
  48. void ErrorHandler::output(int errorCode, const std::string& title)
  49. {
  50. ErrorList errorList;
  51. ErrorMessage errorMessage = errorList.getError(errorCode);
  52. std::string buildMode = BUILD_MODE;
  53. if(buildMode != BUILD_RELEASE)
  54. {
  55. this->writeToLogFile(errorCode, title, errorMessage.message);
  56. if(buildMode == BUILD_DEBUG && errorCode != LE_NO_ERROR) {
  57. MessageBox::display(SDL_MESSAGEBOX_ERROR, title, errorMessage.message);
  58. }
  59. }
  60. }
  61. void ErrorHandler::output(int errorCode, const std::string& title, const std::string& additionalMessage)
  62. {
  63. ErrorList errorList;
  64. ErrorMessage errorMessage = errorList.getError(errorCode);
  65. std::string buildMode = BUILD_MODE;
  66. std::stringstream combinedMessages;
  67. combinedMessages << errorMessage.message << additionalMessage;
  68. std::string message = combinedMessages.str();
  69. if(buildMode != BUILD_RELEASE)
  70. {
  71. this->writeToLogFile(errorCode, title, message);
  72. if(buildMode == BUILD_DEBUG && errorCode != LE_NO_ERROR) {
  73. MessageBox::display(SDL_MESSAGEBOX_ERROR, title, message);
  74. }
  75. }
  76. }