Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 1 | /* Copyright (c) 2019 - 2020 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 | |
| 30 | #include "LogBuffer.h" |
| 31 | #ifdef USE_GLIB |
| 32 | #include <execinfo.h> |
| 33 | #endif |
| 34 | |
| 35 | #ifdef LOG_TAG |
| 36 | #undef LOG_TAG |
| 37 | #endif |
| 38 | #define LOG_TAG "LocSvc_LogBuffer" |
| 39 | |
| 40 | namespace loc_util { |
| 41 | |
| 42 | LogBuffer* LogBuffer::mInstance; |
| 43 | struct sigaction LogBuffer::mOriSigAction[NSIG]; |
| 44 | struct sigaction LogBuffer::mNewSigAction; |
| 45 | mutex LogBuffer::sLock; |
| 46 | |
| 47 | LogBuffer* LogBuffer::getInstance() { |
| 48 | if (mInstance == nullptr) { |
| 49 | lock_guard<mutex> guard(sLock); |
| 50 | if (mInstance == nullptr) { |
| 51 | mInstance = new LogBuffer(); |
| 52 | } |
| 53 | } |
| 54 | return mInstance; |
| 55 | } |
| 56 | |
| 57 | LogBuffer::LogBuffer(): mLogList(TOTAL_LOG_LEVELS), |
| 58 | mConfigVec(TOTAL_LOG_LEVELS, ConfigsInLevel(TIME_DEPTH_THRESHOLD_MINIMAL_IN_SEC, |
| 59 | MAXIMUM_NUM_IN_LIST, 0)) { |
| 60 | loc_param_s_type log_buff_config_table[] = |
| 61 | { |
| 62 | {"E_LEVEL_TIME_DEPTH", &mConfigVec[0].mTimeDepthThres, NULL, 'n'}, |
| 63 | {"E_LEVEL_MAX_CAPACITY", &mConfigVec[0].mMaxNumThres, NULL, 'n'}, |
| 64 | {"W_LEVEL_TIME_DEPTH", &mConfigVec[1].mTimeDepthThres, NULL, 'n'}, |
| 65 | {"W_LEVEL_MAX_CAPACITY", &mConfigVec[1].mMaxNumThres, NULL, 'n'}, |
| 66 | {"I_LEVEL_TIME_DEPTH", &mConfigVec[2].mTimeDepthThres, NULL, 'n'}, |
| 67 | {"I_LEVEL_MAX_CAPACITY", &mConfigVec[2].mMaxNumThres, NULL, 'n'}, |
| 68 | {"D_LEVEL_TIME_DEPTH", &mConfigVec[3].mTimeDepthThres, NULL, 'n'}, |
| 69 | {"D_LEVEL_MAX_CAPACITY", &mConfigVec[3].mMaxNumThres, NULL, 'n'}, |
| 70 | {"V_LEVEL_TIME_DEPTH", &mConfigVec[4].mTimeDepthThres, NULL, 'n'}, |
| 71 | {"V_LEVEL_MAX_CAPACITY", &mConfigVec[4].mMaxNumThres, NULL, 'n'}, |
| 72 | }; |
| 73 | loc_read_conf(LOC_PATH_GPS_CONF_STR, log_buff_config_table, |
| 74 | sizeof(log_buff_config_table)/sizeof(log_buff_config_table[0])); |
| 75 | registerSignalHandler(); |
| 76 | } |
| 77 | |
| 78 | void LogBuffer::append(string& data, int level, uint64_t timestamp) { |
| 79 | lock_guard<mutex> guard(mLock); |
| 80 | pair<uint64_t, string> item(timestamp, data); |
| 81 | mLogList.append(item, level); |
| 82 | mConfigVec[level].mCurrentSize++; |
| 83 | |
| 84 | while ((timestamp - mLogList.front(level).first) > mConfigVec[level].mTimeDepthThres || |
| 85 | mConfigVec[level].mCurrentSize > mConfigVec[level].mMaxNumThres) { |
| 86 | mLogList.pop(level); |
| 87 | mConfigVec[level].mCurrentSize--; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | //Dump the log buffer of specific level, level = -1 to dump all the levels in log buffer. |
| 92 | void LogBuffer::dump(std::function<void(stringstream&)> log, int level) { |
| 93 | lock_guard<mutex> guard(mLock); |
| 94 | list<pair<pair<uint64_t, string>, int>> li; |
| 95 | if (-1 == level) { |
| 96 | li = mLogList.dump(); |
| 97 | } else { |
| 98 | li = mLogList.dump(level); |
| 99 | } |
| 100 | ALOGE("Begining of dump, buffer size: %d", (int)li.size()); |
| 101 | stringstream ln; |
| 102 | ln << "dump log buffer, level[" << level << "]" << ", buffer size: " << li.size() << endl; |
| 103 | log(ln); |
| 104 | for_each (li.begin(), li.end(), [&, this](const pair<pair<uint64_t, string>, int> &item){ |
| 105 | stringstream line; |
| 106 | line << "["<<item.first.first << "] "; |
| 107 | line << "Level " << mLevelMap[item.second] << ": "; |
| 108 | line << item.first.second << endl; |
| 109 | if (log != nullptr) { |
| 110 | log(line); |
| 111 | } |
| 112 | }); |
| 113 | ALOGE("End of dump"); |
| 114 | } |
| 115 | |
| 116 | void LogBuffer::dumpToAdbLogcat() { |
| 117 | dump([](stringstream& line){ |
| 118 | ALOGE("%s", line.str().c_str()); |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | void LogBuffer::dumpToLogFile(string filePath) { |
| 123 | ALOGE("Dump GPS log buffer to file: %s", filePath.c_str()); |
| 124 | fstream s; |
| 125 | s.open(filePath, std::fstream::out | std::fstream::app); |
| 126 | dump([&s](stringstream& line){ |
| 127 | s << line.str(); |
| 128 | }); |
| 129 | s.close(); |
| 130 | } |
| 131 | |
| 132 | void LogBuffer::flush() { |
| 133 | mLogList.flush(); |
| 134 | } |
| 135 | |
| 136 | void LogBuffer::registerSignalHandler() { |
| 137 | ALOGE("Singal handler registered"); |
| 138 | mNewSigAction.sa_sigaction = &LogBuffer::signalHandler; |
| 139 | mNewSigAction.sa_flags = SA_SIGINFO | SA_RESTART; |
| 140 | sigemptyset(&mNewSigAction.sa_mask); |
| 141 | |
| 142 | sigaction(SIGINT, &mNewSigAction, &mOriSigAction[SIGINT]); |
| 143 | sigaction(SIGKILL, &mNewSigAction, &mOriSigAction[SIGKILL]); |
| 144 | sigaction(SIGSEGV, &mNewSigAction, &mOriSigAction[SIGSEGV]); |
| 145 | sigaction(SIGABRT, &mNewSigAction, &mOriSigAction[SIGABRT]); |
| 146 | sigaction(SIGTRAP, &mNewSigAction, &mOriSigAction[SIGTRAP]); |
| 147 | sigaction(SIGUSR1, &mNewSigAction, &mOriSigAction[SIGUSR1]); |
| 148 | } |
| 149 | |
| 150 | void LogBuffer::signalHandler(const int code, siginfo_t *const si, void *const sc) { |
| 151 | ALOGE("[Gnss Log buffer]Singal handler, signal ID: %d", code); |
| 152 | |
| 153 | #ifdef USE_GLIB |
| 154 | int nptrs; |
| 155 | void *buffer[100]; |
| 156 | char **strings; |
| 157 | |
| 158 | nptrs = backtrace(buffer, sizeof(buffer)/sizeof(*buffer)); |
| 159 | strings = backtrace_symbols(buffer, nptrs); |
| 160 | if (strings != NULL) { |
| 161 | timespec tv; |
| 162 | clock_gettime(CLOCK_BOOTTIME, &tv); |
| 163 | uint64_t elapsedTime = (uint64_t)tv.tv_sec + (uint64_t)tv.tv_nsec/1000000000; |
| 164 | for (int i = 0; i < nptrs; i++) { |
| 165 | string s(strings[i]); |
| 166 | mInstance->append(s, 0, elapsedTime); |
| 167 | } |
| 168 | } |
| 169 | #endif |
| 170 | //Dump the log buffer to adb logcat |
| 171 | mInstance->dumpToAdbLogcat(); |
| 172 | |
| 173 | //Dump the log buffer to file |
| 174 | time_t now = time(NULL); |
| 175 | struct tm *curr_time = localtime(&now); |
| 176 | char path[50]; |
| 177 | snprintf(path, 50, LOG_BUFFER_FILE_PATH "gpslog_%d%d%d-%d%d%d.log", |
| 178 | (1900 + curr_time->tm_year), ( 1 + curr_time->tm_mon), curr_time->tm_mday, |
| 179 | curr_time->tm_hour, curr_time->tm_min, curr_time->tm_sec); |
| 180 | |
| 181 | mInstance->dumpToLogFile(path); |
| 182 | |
| 183 | //Process won't be terminated if SIGUSR1 is recieved |
| 184 | if (code != SIGUSR1) { |
| 185 | mOriSigAction[code].sa_sigaction(code, si, sc); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | } |