Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 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 | #ifndef LOG_BUFFER_H |
| 31 | #define LOG_BUFFER_H |
| 32 | |
| 33 | #include "SkipList.h" |
| 34 | #include "log_util.h" |
| 35 | #include <loc_cfg.h> |
| 36 | #include <loc_pla.h> |
| 37 | #include <string> |
| 38 | #include <sstream> |
| 39 | #include <ostream> |
| 40 | #include <fstream> |
| 41 | #include <time.h> |
| 42 | #include <mutex> |
| 43 | #include <signal.h> |
| 44 | #include <thread> |
| 45 | #include <functional> |
| 46 | |
| 47 | //default error level time depth threshold, |
| 48 | #define TIME_DEPTH_THRESHOLD_MINIMAL_IN_SEC 60 |
| 49 | //default maximum log buffer size |
| 50 | #define MAXIMUM_NUM_IN_LIST 50 |
| 51 | //file path of dumped log buffer |
| 52 | #define LOG_BUFFER_FILE_PATH "/data/vendor/location/" |
| 53 | |
| 54 | namespace loc_util { |
| 55 | |
| 56 | class ConfigsInLevel{ |
| 57 | public: |
| 58 | uint32_t mTimeDepthThres; |
| 59 | uint32_t mMaxNumThres; |
| 60 | int mCurrentSize; |
| 61 | |
| 62 | ConfigsInLevel(uint32_t time, int num, int size): |
| 63 | mTimeDepthThres(time), mMaxNumThres(num), mCurrentSize(size) {} |
| 64 | }; |
| 65 | |
| 66 | class LogBuffer { |
| 67 | private: |
| 68 | static LogBuffer* mInstance; |
| 69 | static struct sigaction mOriSigAction[NSIG]; |
| 70 | static struct sigaction mNewSigAction; |
| 71 | static mutex sLock; |
| 72 | |
| 73 | SkipList<pair<uint64_t, string>> mLogList; |
| 74 | vector<ConfigsInLevel> mConfigVec; |
| 75 | mutex mLock; |
| 76 | |
| 77 | const vector<string> mLevelMap {"E", "W", "I", "D", "V"}; |
| 78 | |
| 79 | public: |
| 80 | static LogBuffer* getInstance(); |
| 81 | void append(string& data, int level, uint64_t timestamp); |
| 82 | void dump(std::function<void(stringstream&)> log, int level = -1); |
| 83 | void dumpToAdbLogcat(); |
| 84 | void dumpToLogFile(string filePath); |
| 85 | void flush(); |
| 86 | private: |
| 87 | LogBuffer(); |
| 88 | void registerSignalHandler(); |
| 89 | static void signalHandler(const int code, siginfo_t *const si, void *const sc); |
| 90 | |
| 91 | }; |
| 92 | |
| 93 | } |
| 94 | |
| 95 | #endif |