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