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 std::string GetCmdArgs(const HilogConfig& protoConfig); 59 60 bool StringToL(const char* word, long& value); 61 // for ut SetConfig(HilogConfig & config)62 void SetConfig(HilogConfig& config) 63 { 64 protoConfig_ = config; 65 return; 66 } 67 68 template <typename T> void FlushData(const T hilogLineProto); 69 template <typename T> void FlushDataOptimize(const T hilogLineProto); 70 71 private: 72 HilogConfig protoConfig_; 73 std::vector<char> protoBuffer_; 74 std::vector<char> dataBuffer_; 75 WriterStruct* resultWriter_ = nullptr; 76 std::mutex mutex_; 77 std::thread workThread_; 78 std::atomic<bool> running_ = true; 79 std::vector<std::string> fullCmd_; 80 std::unique_ptr<FILE, std::function<int (FILE*)>> fp_; 81 std::unique_ptr<FileCache> fileCache_ = nullptr; 82 int pipeFds_[2] = {-1, -1}; 83 volatile pid_t childPid_ = -1; 84 }; 85 86 #endif // !HILOG_PLUGIN_H