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_ROTATOR_H 172498b56bSopenharmony_ci#define _HILOG_PERSISTER_ROTATOR_H 182498b56bSopenharmony_ci#include <fstream> 192498b56bSopenharmony_ci#include <string> 202498b56bSopenharmony_ci#include <zlib.h> 212498b56bSopenharmony_ci#include <hilog_common.h> 222498b56bSopenharmony_ci#include <log_utils.h> 232498b56bSopenharmony_ci 242498b56bSopenharmony_ci#include "log_filter.h" 252498b56bSopenharmony_ci 262498b56bSopenharmony_cinamespace OHOS { 272498b56bSopenharmony_cinamespace HiviewDFX { 282498b56bSopenharmony_ci#define FILE_PATH_MAX_LEN 100 292498b56bSopenharmony_cistatic constexpr const char* AUXILLARY_PERSISTER_PREFIX = "persisterInfo_"; 302498b56bSopenharmony_ci 312498b56bSopenharmony_ciusing LogPersistStartMsg = struct { 322498b56bSopenharmony_ci uint16_t compressAlg; 332498b56bSopenharmony_ci char filePath[FILE_PATH_MAX_LEN]; 342498b56bSopenharmony_ci uint32_t fileSize; 352498b56bSopenharmony_ci uint32_t fileNum; 362498b56bSopenharmony_ci uint32_t jobId; 372498b56bSopenharmony_ci LogFilter filter; 382498b56bSopenharmony_ci} __attribute__((__packed__)); 392498b56bSopenharmony_ci 402498b56bSopenharmony_ciusing PersistRecoveryInfo = struct { 412498b56bSopenharmony_ci uint32_t index; 422498b56bSopenharmony_ci LogPersistStartMsg msg; 432498b56bSopenharmony_ci} __attribute__((__packed__)); 442498b56bSopenharmony_ci 452498b56bSopenharmony_ciclass LogPersisterRotator { 462498b56bSopenharmony_cipublic: 472498b56bSopenharmony_ci LogPersisterRotator(const std::string& path, uint32_t id, uint32_t maxFiles, const std::string& suffix = ""); 482498b56bSopenharmony_ci ~LogPersisterRotator(); 492498b56bSopenharmony_ci int Init(const PersistRecoveryInfo& info, bool restore = false); 502498b56bSopenharmony_ci int Input(const char *buf, uint32_t length); 512498b56bSopenharmony_ci void FinishInput(); 522498b56bSopenharmony_ci 532498b56bSopenharmony_ci void SetFileIndex(uint32_t index, bool forceRotate); 542498b56bSopenharmony_ci 552498b56bSopenharmony_ciprivate: 562498b56bSopenharmony_ci void RemoveOldFile(); 572498b56bSopenharmony_ci bool IsOldFile(const std::string& logName, const int index); 582498b56bSopenharmony_ci int OpenInfoFile(); 592498b56bSopenharmony_ci void UpdateRotateNumber(); 602498b56bSopenharmony_ci void WriteRecoveryInfo(); 612498b56bSopenharmony_ci int SetInfo(const LogPersistStartMsg& pMsg, uint16_t logType, uint8_t logLevel); 622498b56bSopenharmony_ci 632498b56bSopenharmony_ci void CreateLogFile(); 642498b56bSopenharmony_ci void Rotate(); 652498b56bSopenharmony_ci 662498b56bSopenharmony_ci uint32_t m_maxLogFileNum = 0; 672498b56bSopenharmony_ci std::string m_logsPath; 682498b56bSopenharmony_ci std::string m_fileNameSuffix; 692498b56bSopenharmony_ci std::string m_currentLogFileName; 702498b56bSopenharmony_ci uint32_t m_currentLogFileIdx = 0; 712498b56bSopenharmony_ci std::fstream m_currentLogOutput; 722498b56bSopenharmony_ci 732498b56bSopenharmony_ci uint32_t m_id = 0; 742498b56bSopenharmony_ci std::fstream m_infoFile; 752498b56bSopenharmony_ci std::string m_infoFilePath; 762498b56bSopenharmony_ci PersistRecoveryInfo m_info = {0}; 772498b56bSopenharmony_ci 782498b56bSopenharmony_ci bool m_needRotate = false; 792498b56bSopenharmony_ci}; 802498b56bSopenharmony_ci} // namespace HiviewDFX 812498b56bSopenharmony_ci} // namespace OHOS 822498b56bSopenharmony_ci#endif 83