1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HILOG_PLUGIN_H 17 #define HILOG_PLUGIN_H 18 19 #include "file_cache.h" 20 #include "hilog_plugin_config.pb.h" 21 #include "hilog_plugin_result.pb.h" 22 #include "logging.h" 23 #include "plugin_module_api.h" 24 25 #include <atomic> 26 #include <mutex> 27 #include <thread> 28 #include <vector> 29 30 class HilogPlugin { 31 public: 32 HilogPlugin(); 33 ~HilogPlugin(); 34 int Start(const uint8_t* configData, uint32_t configSize); 35 int Stop(); 36 37 int SetWriter(WriterStruct* writer); 38 void Run(void); 39 40 private: 41 std::string GetPidCmd(); 42 std::string GetlevelCmd(); 43 bool InitHilogCmd(); 44 std::string ClearHilog(); 45 46 bool OpenLogFile(); 47 int GetDateTime(char* psDateTime, uint32_t size); 48 void ParseLogLineInfo(const char* data, size_t len, HilogLine* info); 49 bool SetHilogLineDetails(const char* data, HilogLine* info); 50 bool TimeStringToNS(const char* data, struct timespec *ts); 51 bool FindFirstNum(char** p); 52 bool RemoveSpaces(char** p); 53 bool FindFirstSpace(char** p); 54 55 static FILE* CustomPopen(const char* command, const char* type); 56 static int CustomPclose(FILE* fp); 57 bool StringToL(const char* word, long& value); 58 // for ut SetConfig(HilogConfig & config)59 void SetConfig(HilogConfig& config) 60 { 61 protoConfig_ = config; 62 return; 63 } GetFullCmd()64 std::string GetFullCmd() 65 { 66 return fullCmd_; 67 } 68 69 private: 70 HilogConfig protoConfig_; 71 std::vector<char> protoBuffer_; 72 std::vector<char> dataBuffer_; 73 WriterStruct* resultWriter_ = nullptr; 74 75 std::mutex mutex_; 76 std::thread workThread_; 77 std::atomic<bool> running_ = true; 78 79 std::string fullCmd_; 80 81 std::unique_ptr<FILE, int (*)(FILE*)> fp_; 82 std::unique_ptr<FileCache> fileCache_ = nullptr; 83 }; 84 85 #endif // !HILOG_PLUGIN_H