debug.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #ifndef EQDEBUG_H
  17. #define EQDEBUG_H
  18. // Debug Levels
  19. /*
  20. 1 = Normal
  21. 3 = Some extended debug info
  22. 5 = Light DETAIL info
  23. 7 = Heavy DETAIL info
  24. 9 = DumpPacket/PrintPacket
  25. You should use even numbers too, to define any subset of the above basic template
  26. */
  27. #ifndef EQDEBUG
  28. #define EQDEBUG 1
  29. #endif
  30. #if defined(DEBUG) && defined(WIN32)
  31. //#ifndef _CRTDBG_MAP_ALLOC
  32. #include <stdlib.h>
  33. #include <crtdbg.h>
  34. #if (_MSC_VER < 1300)
  35. #include <new>
  36. #include <memory>
  37. #define _CRTDBG_MAP_ALLOC
  38. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  39. #define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
  40. #endif
  41. //#endif
  42. #endif
  43. #ifndef ThrowError
  44. void CatchSignal(int);
  45. #if defined(CATCH_CRASH) || defined(_EQDEBUG)
  46. #define ThrowError(errstr) { cout << "Fatal error: " << errstr << " (" << __FILE__ << ", line " << __LINE__ << ")" << endl; LogWrite(WORLD__ERROR, 0, "Debug", "Thrown Error: %s (%s:%i)", errstr, __FILE__, __LINE__); throw errstr; }
  47. #else
  48. #define ThrowError(errstr) { cout << "Fatal error: " << errstr << " (" << __FILE__ << ", line " << __LINE__ << ")" << endl; LogWrite(WORLD__ERROR, 0, "Debug", "Thrown Error: %s (%s:%i)", errstr, __FILE__, __LINE__); CatchSignal(0); }
  49. #endif
  50. #endif
  51. #ifdef WIN32
  52. // VS6 doesn't like the length of STL generated names: disabling
  53. #pragma warning(disable:4786)
  54. #endif
  55. #ifndef WIN32
  56. #define DebugBreak() if(0) {}
  57. #endif
  58. #ifdef WIN32
  59. #include <WinSock2.h>
  60. #include <windows.h>
  61. #endif
  62. #include "../common/Mutex.h"
  63. #include <stdio.h>
  64. #include <stdarg.h>
  65. class EQEMuLog {
  66. public:
  67. EQEMuLog();
  68. ~EQEMuLog();
  69. enum LogIDs {
  70. Status = 0, //this must stay the first entry in this list
  71. Normal,
  72. Error,
  73. Debug,
  74. Quest,
  75. Commands,
  76. MaxLogID
  77. };
  78. //these are callbacks called for each
  79. typedef void (* msgCallbackBuf)(LogIDs id, const char *buf, int8 size, int32 count);
  80. typedef void (* msgCallbackFmt)(LogIDs id, const char *fmt, va_list ap);
  81. void SetAllCallbacks(msgCallbackFmt proc);
  82. void SetAllCallbacks(msgCallbackBuf proc);
  83. void SetCallback(LogIDs id, msgCallbackFmt proc);
  84. void SetCallback(LogIDs id, msgCallbackBuf proc);
  85. bool writebuf(LogIDs id, const char *buf, int8 size, int32 count);
  86. bool write(LogIDs id, const char *fmt, ...);
  87. bool Dump(LogIDs id, int8* data, int32 size, int32 cols=16, int32 skip=0);
  88. private:
  89. bool open(LogIDs id);
  90. bool writeNTS(LogIDs id, bool dofile, const char *fmt, ...); // no error checking, assumes is open, no locking, no timestamp, no newline
  91. Mutex MOpen;
  92. Mutex MLog[MaxLogID];
  93. FILE* fp[MaxLogID];
  94. /* LogStatus: bitwise variable
  95. 1 = output to file
  96. 2 = output to stdout
  97. 4 = fopen error, dont retry
  98. 8 = use stderr instead (2 must be set)
  99. */
  100. int8 pLogStatus[MaxLogID];
  101. msgCallbackFmt logCallbackFmt[MaxLogID];
  102. msgCallbackBuf logCallbackBuf[MaxLogID];
  103. };
  104. //extern EQEMuLog* LogFile;
  105. #ifdef _EQDEBUG
  106. class PerformanceMonitor {
  107. public:
  108. PerformanceMonitor(sint64* ip) {
  109. p = ip;
  110. QueryPerformanceCounter(&tmp);
  111. }
  112. ~PerformanceMonitor() {
  113. LARGE_INTEGER tmp2;
  114. QueryPerformanceCounter(&tmp2);
  115. *p += tmp2.QuadPart - tmp.QuadPart;
  116. }
  117. LARGE_INTEGER tmp;
  118. sint64* p;
  119. };
  120. #endif
  121. #endif