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