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.

140 lines
3.3 KiB

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