LogBuffer.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Copyright (c) 2019 - 2021 The Linux Foundation. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are
  5. * met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above
  9. * copyright notice, this list of conditions and the following
  10. * disclaimer in the documentation and/or other materials provided
  11. * with the distribution.
  12. * * Neither the name of The Linux Foundation, nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  20. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  23. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  25. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  26. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. */
  29. #include "LogBuffer.h"
  30. #ifdef USE_GLIB
  31. #include <execinfo.h>
  32. #endif
  33. #ifdef LOG_TAG
  34. #undef LOG_TAG
  35. #endif
  36. #define LOG_TAG "LocSvc_LogBuffer"
  37. namespace loc_util {
  38. LogBuffer* LogBuffer::mInstance;
  39. struct sigaction LogBuffer::mOriSigAction[NSIG];
  40. struct sigaction LogBuffer::mNewSigAction;
  41. mutex LogBuffer::sLock;
  42. LogBuffer* LogBuffer::getInstance() {
  43. if (mInstance == nullptr) {
  44. lock_guard<mutex> guard(sLock);
  45. if (mInstance == nullptr) {
  46. mInstance = new LogBuffer();
  47. }
  48. }
  49. return mInstance;
  50. }
  51. LogBuffer::LogBuffer(): mLogList(TOTAL_LOG_LEVELS),
  52. mConfigVec(TOTAL_LOG_LEVELS, ConfigsInLevel(TIME_DEPTH_THRESHOLD_MINIMAL_IN_SEC,
  53. MAXIMUM_NUM_IN_LIST, 0)) {
  54. loc_param_s_type log_buff_config_table[] =
  55. {
  56. {"E_LEVEL_TIME_DEPTH", &mConfigVec[0].mTimeDepthThres, NULL, 'n'},
  57. {"E_LEVEL_MAX_CAPACITY", &mConfigVec[0].mMaxNumThres, NULL, 'n'},
  58. {"W_LEVEL_TIME_DEPTH", &mConfigVec[1].mTimeDepthThres, NULL, 'n'},
  59. {"W_LEVEL_MAX_CAPACITY", &mConfigVec[1].mMaxNumThres, NULL, 'n'},
  60. {"I_LEVEL_TIME_DEPTH", &mConfigVec[2].mTimeDepthThres, NULL, 'n'},
  61. {"I_LEVEL_MAX_CAPACITY", &mConfigVec[2].mMaxNumThres, NULL, 'n'},
  62. {"D_LEVEL_TIME_DEPTH", &mConfigVec[3].mTimeDepthThres, NULL, 'n'},
  63. {"D_LEVEL_MAX_CAPACITY", &mConfigVec[3].mMaxNumThres, NULL, 'n'},
  64. {"V_LEVEL_TIME_DEPTH", &mConfigVec[4].mTimeDepthThres, NULL, 'n'},
  65. {"V_LEVEL_MAX_CAPACITY", &mConfigVec[4].mMaxNumThres, NULL, 'n'},
  66. };
  67. loc_read_conf(LOC_PATH_GPS_CONF_STR, log_buff_config_table,
  68. sizeof(log_buff_config_table)/sizeof(log_buff_config_table[0]));
  69. registerSignalHandler();
  70. }
  71. void LogBuffer::append(string& data, int level, uint64_t timestamp) {
  72. lock_guard<mutex> guard(mLock);
  73. pair<uint64_t, string> item(timestamp, data);
  74. mLogList.append(item, level);
  75. mConfigVec[level].mCurrentSize++;
  76. while ((timestamp - mLogList.front(level).first) > mConfigVec[level].mTimeDepthThres ||
  77. mConfigVec[level].mCurrentSize > mConfigVec[level].mMaxNumThres) {
  78. mLogList.pop(level);
  79. mConfigVec[level].mCurrentSize--;
  80. }
  81. }
  82. //Dump the log buffer of specific level, level = -1 to dump all the levels in log buffer.
  83. void LogBuffer::dump(std::function<void(stringstream&)> log, int level) {
  84. lock_guard<mutex> guard(mLock);
  85. list<pair<pair<uint64_t, string>, int>> li;
  86. if (-1 == level) {
  87. li = mLogList.dump();
  88. } else {
  89. li = mLogList.dump(level);
  90. }
  91. ALOGE("Begining of dump, buffer size: %d", (int)li.size());
  92. stringstream ln;
  93. ln << "dump log buffer, level[" << level << "]" << ", buffer size: " << li.size() << endl;
  94. log(ln);
  95. for_each (li.begin(), li.end(), [&, this](const pair<pair<uint64_t, string>, int> &item){
  96. stringstream line;
  97. line << "["<<item.first.first << "] ";
  98. line << "Level " << mLevelMap[item.second] << ": ";
  99. line << item.first.second << endl;
  100. if (log != nullptr) {
  101. log(line);
  102. }
  103. });
  104. ALOGE("End of dump");
  105. }
  106. void LogBuffer::dumpToAdbLogcat() {
  107. dump([](stringstream& line){
  108. ALOGE("%s", line.str().c_str());
  109. });
  110. }
  111. void LogBuffer::dumpToLogFile(string filePath) {
  112. ALOGE("Dump GPS log buffer to file: %s", filePath.c_str());
  113. fstream s;
  114. s.open(filePath, std::fstream::out | std::fstream::app);
  115. dump([&s](stringstream& line){
  116. s << line.str();
  117. });
  118. s.close();
  119. }
  120. void LogBuffer::flush() {
  121. mLogList.flush();
  122. }
  123. void LogBuffer::registerSignalHandler() {
  124. ALOGE("Singal handler registered");
  125. mNewSigAction.sa_sigaction = &LogBuffer::signalHandler;
  126. mNewSigAction.sa_flags = SA_SIGINFO | SA_RESTART;
  127. sigemptyset(&mNewSigAction.sa_mask);
  128. sigaction(SIGINT, &mNewSigAction, &mOriSigAction[SIGINT]);
  129. sigaction(SIGKILL, &mNewSigAction, &mOriSigAction[SIGKILL]);
  130. sigaction(SIGSEGV, &mNewSigAction, &mOriSigAction[SIGSEGV]);
  131. sigaction(SIGABRT, &mNewSigAction, &mOriSigAction[SIGABRT]);
  132. sigaction(SIGTRAP, &mNewSigAction, &mOriSigAction[SIGTRAP]);
  133. sigaction(SIGUSR1, &mNewSigAction, &mOriSigAction[SIGUSR1]);
  134. }
  135. void LogBuffer::signalHandler(const int code, siginfo_t *const si, void *const sc) {
  136. ALOGE("[Gnss Log buffer]Singal handler, signal ID: %d", code);
  137. #ifdef USE_GLIB
  138. int nptrs;
  139. void *buffer[100];
  140. char **strings;
  141. nptrs = backtrace(buffer, sizeof(buffer)/sizeof(*buffer));
  142. strings = backtrace_symbols(buffer, nptrs);
  143. if (strings != NULL) {
  144. timespec tv = {};
  145. clock_gettime(CLOCK_BOOTTIME, &tv);
  146. uint64_t elapsedTime = (uint64_t)tv.tv_sec + (uint64_t)tv.tv_nsec/1000000000;
  147. for (int i = 0; i < nptrs; i++) {
  148. string s(strings[i]);
  149. mInstance->append(s, 0, elapsedTime);
  150. }
  151. }
  152. #endif
  153. //Dump the log buffer to adb logcat
  154. mInstance->dumpToAdbLogcat();
  155. //Dump the log buffer to file
  156. time_t now = time(NULL);
  157. struct tm *curr_time = localtime(&now);
  158. char path[50];
  159. snprintf(path, 50, LOG_BUFFER_FILE_PATH "gpslog_%d%d%d-%d%d%d.log",
  160. (1900 + curr_time->tm_year), ( 1 + curr_time->tm_mon), curr_time->tm_mday,
  161. curr_time->tm_hour, curr_time->tm_min, curr_time->tm_sec);
  162. mInstance->dumpToLogFile(path);
  163. //Process won't be terminated if SIGUSR1 is recieved
  164. if (code != SIGUSR1) {
  165. mOriSigAction[code].sa_sigaction(code, si, sc);
  166. }
  167. }
  168. }