PageRenderTime 168ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/miles/ShiningloreTribes/T509/logger.cpp

#
C++ | 100 lines | 69 code | 25 blank | 6 comment | 2 complexity | d287650c1c1f849ee1ea9bdc99ba4950 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. #include "s-int.h"
  2. #ifdef _DEBUG
  3. using namespace std;
  4. ofstream fs;
  5. CRITICAL_SECTION cs;
  6. void CreateLogger(const wchar_t* fileName)
  7. {
  8. // Initialize the critical section one time only.
  9. InitializeCriticalSectionAndSpinCount(&cs, 0x80000400);
  10. fs.open(fileName, ios::in | ios::out | ios::trunc);
  11. }
  12. void CloseLogger()
  13. {
  14. fs.close();
  15. // Release resources used by the critical section object.
  16. DeleteCriticalSection(&cs);
  17. }
  18. void WriteLogger(const char* lpszText, ...)
  19. {
  20. char buffer[MAX_CHAR];
  21. va_list ap;
  22. va_start(ap, lpszText);
  23. vsprintf(buffer, lpszText, ap);
  24. va_end(ap);
  25. // Request ownership of the critical section.
  26. EnterCriticalSection(&cs);
  27. fs << buffer << endl;
  28. fs.flush();
  29. // Release ownership of the critical section.
  30. LeaveCriticalSection(&cs);
  31. }
  32. inline char Hex2Asic(const char x)
  33. {
  34. if(x >= 0xA && x <= 0xF)
  35. return x + 'A' - 10;
  36. else
  37. return x + '0';
  38. }
  39. inline char* GetHex(const char x, char* buf)
  40. {
  41. char* p = buf;
  42. *p++ = Hex2Asic((x >> 4) & 0xF);
  43. *p++ = Hex2Asic(x & 0xF);
  44. return buf;
  45. }
  46. void WritePackage(SENDRECIEVE how, PACKAGESTATE state, unsigned int s, const char* buf, int len)
  47. {
  48. char hex[3] = {0};
  49. switch(how)
  50. {
  51. case SENDRECIEVE::RECIEVE:
  52. fs << "Receive package"; break;
  53. case SENDRECIEVE::SEND:
  54. fs << "Send package"; break;
  55. }
  56. switch(state)
  57. {
  58. case PACKAGESTATE::ORIGINAL:
  59. fs << " [ORIGINAL]"; break;
  60. case PACKAGESTATE::ENCRYPED:
  61. fs << " [ENCRYPED]"; break;
  62. }
  63. // Request ownership of the critical section.
  64. EnterCriticalSection(&cs);
  65. fs << " SOCKET=" << s << " len=" << len << " BYTE=";
  66. while(len-->0)
  67. {
  68. fs << " " << GetHex(*buf++, hex);
  69. }
  70. fs << endl;
  71. fs.flush();
  72. // Release ownership of the critical section.
  73. LeaveCriticalSection(&cs);
  74. }
  75. #endif //__509_DEBUG