You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "basicLogCollection.h"
  2. #ifdef MEM_DEBUG_LOG
  3. #include "../../alg/generic/heapDebug.h"
  4. #endif
  5. basicLogCollection * basicLogCollection::_instance = NULL;
  6. CSTR basicLogCollection::m_pBroadcastBuffer = NULL;
  7. /**
  8. * Returns the single instance of the object.
  9. */
  10. basicLogCollection * basicLogCollection::getInstance(_LOG_SETTINGS val)
  11. {
  12. // check if the instance has been created yet
  13. if (NULL == _instance)
  14. {
  15. // if not, then create it
  16. _instance = new basicLogCollection(val);
  17. };
  18. // return the single instance
  19. return _instance;
  20. }
  21. void basicLogCollection::addHandler(basicLoggingService * handler)
  22. {
  23. if (m_pRing != NULL)
  24. {
  25. m_mLogMutex.lock();
  26. m_pRing->add(handler);
  27. m_mLogMutex.unlock();
  28. }
  29. }
  30. void basicLogCollection::broadcast(CSTR Msg)
  31. {
  32. basicLoggingService * mService = NULL;
  33. LoggersIterator * mpIterator = NULL;
  34. m_mLogMutex.lock();
  35. mpIterator = m_pRing->begin();
  36. while (mpIterator != NULL)
  37. {
  38. mService = m_pRing->getAtCursor(mpIterator);
  39. mService->logMessage(Msg);
  40. mpIterator = mpIterator->next;
  41. };
  42. m_mLogMutex.unlock();
  43. }
  44. void basicLogCollection::setLogLevel(_LOG_LEVEL level)
  45. {
  46. m_bMaxLevel = level;
  47. }
  48. void basicLogCollection::Log(const char * Mod, void * ptr, CSTR Msg)
  49. {
  50. Log(Mod, ptr, m_bMaxLevel, Msg);
  51. }
  52. void basicLogCollection::Log(const char * Mod, void * ptr, const char * Msg)
  53. {
  54. Log(Mod, ptr, m_bMaxLevel, Msg);
  55. }
  56. void basicLogCollection::Log(const char * Mod, void * ptr, _LOG_LEVEL Level, CSTR Msg)
  57. {
  58. #ifdef MEM_DEBUG_LOG
  59. size_t memStat = getCurrentRSS();
  60. sprintf(m_pBroadcastBuffer, "MemStat: %d\n", memStat);
  61. broadcast(m_pBroadcastBuffer);
  62. #endif
  63. if (Level <= m_bMaxLevel)
  64. {
  65. if (strlen(Msg) < (MAX_LOG_MSG_SIZE - 120))
  66. {
  67. time_t LocalTime;
  68. time(&LocalTime);
  69. #ifndef __linux__
  70. tm * gmt_time = NULL;
  71. gmt_time = gmtime(&LocalTime);
  72. #else
  73. tm gmtt;
  74. tm * gmt_time = &gmtt;
  75. if (gmtime_r(&LocalTime, &gmtt) == NULL)
  76. {
  77. return;
  78. }
  79. #endif
  80. switch (m_bLogSettings)
  81. {
  82. case _LOG_SIMPLE: sprintf(m_pBroadcastBuffer, "%s: %s\n", Mod, Msg); break;
  83. case _LOG_TIME:
  84. {
  85. sprintf(m_pBroadcastBuffer, "%04d-%02d-%02d %02d:%02d:%02d %s: %s\n", 1900 + gmt_time->tm_year, 1 + gmt_time->tm_mon, gmt_time->tm_mday, gmt_time->tm_hour, gmt_time->tm_min, gmt_time->tm_sec, Mod, Msg);
  86. break;
  87. };
  88. case _LOG_PTR: sprintf(m_pBroadcastBuffer, "%p %s: %s\n", ptr, Mod, Msg); break;
  89. case _LOG_TIME_PTR:
  90. {
  91. sprintf(m_pBroadcastBuffer, "%04d-%02d-%02d %02d:%02d:%02d %p %s: %s\n", 1900 + gmt_time->tm_year, 1 + gmt_time->tm_mon, gmt_time->tm_mday, gmt_time->tm_hour, gmt_time->tm_min, gmt_time->tm_sec, ptr, Mod, Msg);
  92. break;
  93. };
  94. case _LOG_LVL: sprintf(m_pBroadcastBuffer, "%02X %s: %s\n", Level, Mod, Msg); break;
  95. case _LOG_LVL_TIME:
  96. {
  97. sprintf(m_pBroadcastBuffer, "%02X %04d-%02d-%02d %02d:%02d:%02d %s: %s\n", Level, 1900 + gmt_time->tm_year, 1 + gmt_time->tm_mon, gmt_time->tm_mday, gmt_time->tm_hour, gmt_time->tm_min, gmt_time->tm_sec, Mod, Msg);
  98. break;
  99. };
  100. case _LOG_LVL_PTR: sprintf(m_pBroadcastBuffer, "%02X %p %s: %s\n", Level, ptr, Mod, Msg); break;
  101. case _LOG_LVL_TIME_PTR:
  102. {
  103. sprintf(m_pBroadcastBuffer, "%02X %04d-%02d-%02d %02d:%02d:%02d %p %s: %s\n", Level, 1900 + gmt_time->tm_year, 1 + gmt_time->tm_mon, gmt_time->tm_mday, gmt_time->tm_hour, gmt_time->tm_min, gmt_time->tm_sec, ptr, Mod, Msg);
  104. break;
  105. };
  106. };
  107. }
  108. else
  109. {
  110. sprintf(m_pBroadcastBuffer, "Log Message Too Long Buffer is Overflowed Splitting!\n");
  111. };
  112. broadcast((CSTR) m_pBroadcastBuffer);
  113. };
  114. }
  115. void basicLogCollection::Log(const char * Mod, void * ptr, _LOG_LEVEL Level, const char * Msg)
  116. {
  117. Log(Mod, ptr, Level, (CSTR) Msg);
  118. }