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.

134 lines
3.6 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. switch (m_bLogSettings)
  84. {
  85. case _LOG_SIMPLE: sprintf(temp, "%s: %s\n", Mod, Msg); break;
  86. case _LOG_TIME:
  87. {
  88. sprintf(temp, "%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);
  89. break;
  90. };
  91. case _LOG_PTR: sprintf(temp, "%p %s: %s\n", ptr, Mod, Msg); break;
  92. case _LOG_TIME_PTR:
  93. {
  94. sprintf(temp, "%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);
  95. break;
  96. };
  97. case _LOG_LVL: sprintf(temp, "%02X %s: %s\n", Level, Mod, Msg); break;
  98. case _LOG_LVL_TIME:
  99. {
  100. sprintf(temp, "%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);
  101. break;
  102. };
  103. case _LOG_LVL_PTR: sprintf(temp, "%02X %p %s: %s\n", Level, ptr, Mod, Msg); break;
  104. case _LOG_LVL_TIME_PTR:
  105. {
  106. sprintf(temp, "%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);
  107. break;
  108. };
  109. };
  110. broadcast((CSTR) temp);
  111. }
  112. else
  113. {
  114. sprintf(temp, "Log Message Too Long Buffer is Overflowed Splitting!");
  115. broadcast((CSTR) temp);
  116. };
  117. };
  118. }
  119. void basicLogCollection::Log(const char * Mod, void * ptr, _LOG_LEVEL Level, const char * Msg)
  120. {
  121. Log(Mod, ptr, Level, (CSTR) Msg);
  122. }