1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 void InitHilogCmd(); 44 45 bool OpenLogFile(); 46 int GetDateTime(char* psDateTime, uint32_t size); 47 48 template <typename T> void ParseLogLineInfo(const char* data, size_t len, T& hilogLineInfo); 49 50 template <typename T> void ParseLogLineData(const char* data, size_t len, T hilogInfoProto); 51 52 template <typename T> bool SetHilogLineDetails(const char* data, T& hilogLineInfo); 53 54 bool TimeStringToNS(const char* data, struct timespec *ts); 55 bool FindFirstNum(char** p); 56 bool RemoveSpaces(char** p); 57 bool FindFirstSpace(char** p); 58 59 bool StringToL(const char* word, long& value); 60 // for ut SetConfig(HilogConfig& config)61 void SetConfig(HilogConfig& config) 62 { 63 protoConfig_ = config; 64 return; 65 } 66 67 template <typename T> void FlushData(const T hilogLineProto); 68 template <typename T> void FlushDataOptimize(const T hilogLineProto); 69 70 private: 71 HilogConfig protoConfig_; 72 std::vector<char> protoBuffer_; 73 std::vector<char> dataBuffer_; 74 WriterStruct* resultWriter_ = nullptr; 75 std::mutex mutex_; 76 std::thread workThread_; 77 std::atomic<bool> running_ = true; 78 std::vector<std::string> fullCmd_; 79 std::unique_ptr<FILE, std::function<int (FILE*)>> fp_; 80 std::unique_ptr<FileCache> fileCache_ = nullptr; 81 int pipeFds_[2] = {-1, -1}; 82 volatile pid_t childPid_ = -1; 83 }; 84 85 #endif // !HILOG_PLUGIN_H