12498b56bSopenharmony_ci/* 22498b56bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 42498b56bSopenharmony_ci * you may not use this file except in compliance with the License. 52498b56bSopenharmony_ci * You may obtain a copy of the License at 62498b56bSopenharmony_ci * 72498b56bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 82498b56bSopenharmony_ci * 92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 122498b56bSopenharmony_ci * See the License for the specific language governing permissions and 132498b56bSopenharmony_ci * limitations under the License. 142498b56bSopenharmony_ci */ 152498b56bSopenharmony_ci 162498b56bSopenharmony_ci#ifndef _HILOG_PERSISTER_H 172498b56bSopenharmony_ci#define _HILOG_PERSISTER_H 182498b56bSopenharmony_ci 192498b56bSopenharmony_ci#include <pthread.h> 202498b56bSopenharmony_ci#include <zlib.h> 212498b56bSopenharmony_ci 222498b56bSopenharmony_ci#include <condition_variable> 232498b56bSopenharmony_ci#include <chrono> 242498b56bSopenharmony_ci#include <fstream> 252498b56bSopenharmony_ci#include <iostream> 262498b56bSopenharmony_ci#include <list> 272498b56bSopenharmony_ci#include <memory> 282498b56bSopenharmony_ci#include <string> 292498b56bSopenharmony_ci#include <thread> 302498b56bSopenharmony_ci#include <variant> 312498b56bSopenharmony_ci 322498b56bSopenharmony_ci#include "log_buffer.h" 332498b56bSopenharmony_ci#include "log_filter.h" 342498b56bSopenharmony_ci#include "log_persister_rotator.h" 352498b56bSopenharmony_ci#include "log_compress.h" 362498b56bSopenharmony_ci 372498b56bSopenharmony_cinamespace OHOS { 382498b56bSopenharmony_cinamespace HiviewDFX { 392498b56bSopenharmony_ciusing LogPersistQueryResult = struct { 402498b56bSopenharmony_ci int32_t result; 412498b56bSopenharmony_ci uint32_t jobId; 422498b56bSopenharmony_ci uint16_t logType; 432498b56bSopenharmony_ci uint16_t compressAlg; 442498b56bSopenharmony_ci char filePath[FILE_PATH_MAX_LEN]; 452498b56bSopenharmony_ci uint32_t fileSize; 462498b56bSopenharmony_ci uint32_t fileNum; 472498b56bSopenharmony_ci} __attribute__((__packed__)); 482498b56bSopenharmony_ci 492498b56bSopenharmony_ciclass LogPersister : public std::enable_shared_from_this<LogPersister> { 502498b56bSopenharmony_cipublic: 512498b56bSopenharmony_ci [[nodiscard]] static std::shared_ptr<LogPersister> CreateLogPersister(HilogBuffer &buffer); 522498b56bSopenharmony_ci 532498b56bSopenharmony_ci ~LogPersister(); 542498b56bSopenharmony_ci 552498b56bSopenharmony_ci static int Kill(uint32_t id); 562498b56bSopenharmony_ci static int Query(std::list<LogPersistQueryResult> &results); 572498b56bSopenharmony_ci static int Refresh(uint32_t id); 582498b56bSopenharmony_ci static void Clear(); 592498b56bSopenharmony_ci 602498b56bSopenharmony_ci int Init(const PersistRecoveryInfo& msg, bool restore); 612498b56bSopenharmony_ci int Deinit(); 622498b56bSopenharmony_ci 632498b56bSopenharmony_ci void Start(); 642498b56bSopenharmony_ci void Stop(); 652498b56bSopenharmony_ci 662498b56bSopenharmony_ci void FillInfo(LogPersistQueryResult &response); 672498b56bSopenharmony_ci 682498b56bSopenharmony_ciprivate: 692498b56bSopenharmony_ci explicit LogPersister(HilogBuffer &buffer); 702498b56bSopenharmony_ci 712498b56bSopenharmony_ci static bool CheckRegistered(uint32_t id, const std::string& logPath); 722498b56bSopenharmony_ci static std::shared_ptr<LogPersister> GetLogPersisterById(uint32_t id); 732498b56bSopenharmony_ci static void RegisterLogPersister(const std::shared_ptr<LogPersister>& obj); 742498b56bSopenharmony_ci static void DeregisterLogPersister(const std::shared_ptr<LogPersister>& obj); 752498b56bSopenharmony_ci 762498b56bSopenharmony_ci void NotifyNewLogAvailable(); 772498b56bSopenharmony_ci 782498b56bSopenharmony_ci int ReceiveLogLoop(); 792498b56bSopenharmony_ci 802498b56bSopenharmony_ci int InitCompression(); 812498b56bSopenharmony_ci int InitFileRotator(const PersistRecoveryInfo& msg, bool restore); 822498b56bSopenharmony_ci int WriteLogData(const HilogData& logData); 832498b56bSopenharmony_ci bool WriteUncompressedLogs(std::string& logLine); 842498b56bSopenharmony_ci void WriteCompressedLogs(); 852498b56bSopenharmony_ci 862498b56bSopenharmony_ci int PrepareUncompressedFile(const std::string& parentPath, bool restore); 872498b56bSopenharmony_ci 882498b56bSopenharmony_ci std::string m_plainLogFilePath; 892498b56bSopenharmony_ci LogPersisterBuffer *m_mappedPlainLogFile; 902498b56bSopenharmony_ci uint32_t m_plainLogSize = 0; 912498b56bSopenharmony_ci std::unique_ptr<LogCompress> m_compressor; 922498b56bSopenharmony_ci std::unique_ptr<LogPersisterBuffer> m_compressBuffer; 932498b56bSopenharmony_ci std::unique_ptr<LogPersisterRotator> m_fileRotator; 942498b56bSopenharmony_ci 952498b56bSopenharmony_ci std::mutex m_receiveLogCvMtx; 962498b56bSopenharmony_ci std::condition_variable m_receiveLogCv; 972498b56bSopenharmony_ci 982498b56bSopenharmony_ci volatile bool m_stopThread = false; 992498b56bSopenharmony_ci std::thread m_persisterThread; 1002498b56bSopenharmony_ci 1012498b56bSopenharmony_ci HilogBuffer &m_hilogBuffer; 1022498b56bSopenharmony_ci HilogBuffer::ReaderId m_bufReader; 1032498b56bSopenharmony_ci LogPersistStartMsg m_startMsg; 1042498b56bSopenharmony_ci 1052498b56bSopenharmony_ci std::mutex m_initMtx; 1062498b56bSopenharmony_ci volatile bool m_inited = false; 1072498b56bSopenharmony_ci 1082498b56bSopenharmony_ci static std::recursive_mutex s_logPersistersMtx; 1092498b56bSopenharmony_ci static std::list<std::shared_ptr<LogPersister>> s_logPersisters; 1102498b56bSopenharmony_ci}; 1112498b56bSopenharmony_ci 1122498b56bSopenharmony_cistd::list<std::string> LogDataToFormatedStrings(HilogData *data); 1132498b56bSopenharmony_ci} // namespace HiviewDFX 1142498b56bSopenharmony_ci} // namespace OHOS 1152498b56bSopenharmony_ci#endif 116