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 #ifndef _HILOG_PERSISTER_H 16 #define _HILOG_PERSISTER_H 17 18 #include <pthread.h> 19 #include <zlib.h> 20 21 #include <condition_variable> 22 #include <chrono> 23 #include <fstream> 24 #include <iostream> 25 #include <list> 26 #include <memory> 27 #include <string> 28 #include <thread> 29 #include <variant> 30 31 #include "log_buffer.h" 32 #include "log_filter.h" 33 #include "log_persister_rotator.h" 34 #include "log_compress.h" 35 36 namespace OHOS { 37 namespace HiviewDFX { 38 class LogPersister : public std::enable_shared_from_this<LogPersister> { 39 public: 40 using InitData = std::variant<LogPersistStartMsg, PersistRecoveryInfo>; 41 42 [[nodiscard]] static std::shared_ptr<LogPersister> CreateLogPersister(HilogBuffer &buffer); 43 44 ~LogPersister(); 45 46 static int Kill(uint32_t id); 47 static int Query(uint16_t logType, std::list<LogPersistQueryResult> &results); 48 49 int Init(const InitData& initData); 50 int Deinit(); 51 52 void Start(); 53 void Stop(); 54 55 void FillInfo(LogPersistQueryResult &response); 56 private: 57 struct BaseData { 58 uint32_t id; 59 std::string logPath; 60 uint32_t logFileSizeLimit; 61 uint16_t compressAlg; 62 uint32_t maxLogFileNum; 63 std::chrono::seconds newLogTimeout; 64 }; 65 66 LogPersister(HilogBuffer &buffer); 67 68 static bool CheckRegistered(uint32_t id, const std::string& logPath); 69 static std::shared_ptr<LogPersister> GetLogPersisterById(uint32_t id); 70 static void RegisterLogPersister(const std::shared_ptr<LogPersister>& obj); 71 static void DeregisterLogPersister(const std::shared_ptr<LogPersister>& obj); 72 73 void NotifyNewLogAvailable(); 74 75 int ReceiveLogLoop(); 76 77 int InitCompression(); 78 int InitFileRotator(const InitData& initData); 79 int WriteLogData(const HilogData& logData); 80 bool WriteUncompressedLogs(std::list<std::string>& formatedTextLogs); 81 void WriteCompressedLogs(); 82 83 int PrepareUncompressedFile(const std::string& parentPath, bool restore); 84 85 BaseData m_baseData = {0}; 86 87 std::string m_plainLogFilePath; 88 LogPersisterBuffer *m_mappedPlainLogFile; 89 uint32_t m_plainLogSize = 0; 90 std::unique_ptr<LogCompress> m_compressor; 91 std::unique_ptr<LogPersisterBuffer> m_compressBuffer; 92 std::unique_ptr<LogPersisterRotator> m_fileRotator; 93 94 std::mutex m_receiveLogCvMtx; 95 std::condition_variable m_receiveLogCv; 96 97 volatile bool m_stopThread = false; 98 std::thread m_persisterThread; 99 100 HilogBuffer &m_hilogBuffer; 101 HilogBuffer::ReaderId m_bufReader; 102 LogFilterExt m_filters; 103 104 std::mutex m_initMtx; 105 volatile bool m_inited = false; 106 107 static std::recursive_mutex s_logPersistersMtx; 108 static std::list<std::shared_ptr<LogPersister>> s_logPersisters; 109 }; 110 111 std::list<std::string> LogDataToFormatedStrings(HilogData *data); 112 } // namespace HiviewDFX 113 } // namespace OHOS 114 #endif 115