EQEMuError.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifdef WIN32
  17. #include <WinSock2.h>
  18. #include <windows.h>
  19. #endif
  20. #include "EQEMuError.h"
  21. #include "linked_list.h"
  22. #include "Mutex.h"
  23. #include "MiscFunctions.h"
  24. #include <stdio.h>
  25. #include <string.h>
  26. #ifdef WIN32
  27. #include <conio.h>
  28. #endif
  29. void CatchSignal(int sig_num);
  30. const char* EQEMuErrorText[EQEMuError_MaxErrorID] = { "ErrorID# 0, No Error",
  31. "MySQL Error #1405 or #2001 means your mysql server rejected the username and password you presented it.",
  32. "MySQL Error #2003 means you were unable to connect to the mysql server.",
  33. "MySQL Error #2005 means you there are too many connections on the mysql server. The server is overloaded.",
  34. "MySQL Error #2007 means you the server is out of memory. The server is overloaded.",
  35. };
  36. LinkedList<char*>* EQEMuErrorList;
  37. Mutex* MEQEMuErrorList;
  38. AutoDelete< LinkedList<char*> > ADEQEMuErrorList(&EQEMuErrorList);
  39. AutoDelete<Mutex> ADMEQEMuErrorList(&MEQEMuErrorList);
  40. const char* GetErrorText(int32 iError) {
  41. if (iError >= EQEMuError_MaxErrorID)
  42. return "ErrorID# out of range";
  43. else
  44. return EQEMuErrorText[iError];
  45. }
  46. void AddEQEMuError(eEQEMuError iError, bool iExitNow) {
  47. if (!iError)
  48. return;
  49. if (!EQEMuErrorList) {
  50. EQEMuErrorList = new LinkedList<char*>;
  51. MEQEMuErrorList = new Mutex;
  52. }
  53. LockMutex lock(MEQEMuErrorList);
  54. LinkedListIterator<char*> iterator(*EQEMuErrorList);
  55. iterator.Reset();
  56. while (iterator.MoreElements()) {
  57. if (iterator.GetData()[0] == 1) {
  58. if (*((eEQEMuError*) &(iterator.GetData()[1])) == iError)
  59. return;
  60. }
  61. iterator.Advance();
  62. }
  63. char* tmp = new char[6];
  64. tmp[0] = 1;
  65. tmp[5] = 0;
  66. *((int32*) &tmp[1]) = iError;
  67. EQEMuErrorList->Append(tmp);
  68. if (iExitNow)
  69. CatchSignal(2);
  70. }
  71. void AddEQEMuError(char* iError, bool iExitNow) {
  72. if (!iError)
  73. return;
  74. if (!EQEMuErrorList) {
  75. EQEMuErrorList = new LinkedList<char*>;
  76. MEQEMuErrorList = new Mutex;
  77. }
  78. LockMutex lock(MEQEMuErrorList);
  79. char* tmp = strcpy(new char[strlen(iError) + 1], iError);
  80. EQEMuErrorList->Append(tmp);
  81. if (iExitNow)
  82. CatchSignal(2);
  83. }
  84. int32 CheckEQEMuError() {
  85. if (!EQEMuErrorList)
  86. return 0;
  87. int32 ret = 0;
  88. char* tmp = 0;
  89. bool HeaderPrinted = false;
  90. LockMutex lock(MEQEMuErrorList);
  91. while ((tmp = EQEMuErrorList->Pop() )) {
  92. if (!HeaderPrinted) {
  93. fprintf(stdout, "===============================\nRuntime errors:\n\n");
  94. HeaderPrinted = true;
  95. }
  96. if (tmp[0] == 1) {
  97. fprintf(stdout, "%s\n", GetErrorText(*((int32*) &tmp[1])));
  98. }
  99. else {
  100. fprintf(stdout, "%s\n\n", tmp);
  101. }
  102. safe_delete(tmp);
  103. ret++;
  104. }
  105. return ret;
  106. }
  107. void CheckEQEMuErrorAndPause() {
  108. if (CheckEQEMuError()) {
  109. fprintf(stdout, "Hit any key to exit\n");
  110. getchar();
  111. }
  112. }