Clone of PhatAC @ https://github.com/floaterxk/PhatAC

Logging.cpp 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "StdAfx.h"
  2. #include "Logging.h"
  3. CLogger g_Logger;
  4. CLogFile::CLogFile()
  5. {
  6. m_File = NULL;
  7. }
  8. CLogFile::~CLogFile()
  9. {
  10. Close();
  11. }
  12. bool CLogFile::Open(const char *filepath)
  13. {
  14. Close();
  15. m_File = fopen(filepath, "wt");
  16. return m_File != NULL;
  17. }
  18. void CLogFile::Close()
  19. {
  20. if (m_File)
  21. {
  22. fclose(m_File);
  23. m_File = NULL;
  24. }
  25. }
  26. void CLogFile::Write(const char *text)
  27. {
  28. if (m_File)
  29. {
  30. fwrite(text, sizeof(char), strlen(text), m_File);
  31. }
  32. }
  33. CLogger::CLogger()
  34. {
  35. }
  36. CLogger::~CLogger()
  37. {
  38. Close();
  39. }
  40. bool CLogger::Open()
  41. {
  42. return m_Log.Open(g_pGlobals->GetGameFile("console.txt").c_str());
  43. }
  44. void CLogger::Close()
  45. {
  46. m_Log.Close();
  47. }
  48. void CLogger::Write(int category, int level, const char *format, ...)
  49. {
  50. va_list args;
  51. va_start(args, format);
  52. int charcount = _vscprintf(format, args) + 1;
  53. char *charbuffer = new char[charcount];
  54. _vsnprintf(charbuffer, charcount, format, args);
  55. #ifdef _DEBUG
  56. OutputDebugStringA(charbuffer);
  57. #endif
  58. m_Log.Write(charbuffer);
  59. for (auto& callback : m_LogCallbacks)
  60. {
  61. callback(category, level, charbuffer);
  62. }
  63. delete[] charbuffer;
  64. va_end(args);
  65. }