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_ROTATOR_H 16 #define _HILOG_PERSISTER_ROTATOR_H 17 #include <fstream> 18 #include <string> 19 20 #include <zlib.h> 21 #include "hilog_common.h" 22 #include "hilogtool_msg.h" 23 #include "log_filter.h" 24 namespace OHOS { 25 namespace HiviewDFX { 26 typedef struct { 27 uint32_t index; 28 uint16_t types; 29 uint8_t levels; 30 LogPersistStartMsg msg; 31 } PersistRecoveryInfo; 32 33 static constexpr const char* AUXILLARY_PERSISTER_PREFIX = "persisterInfo_"; 34 35 uint64_t GenerateHash(const PersistRecoveryInfo &info); 36 37 class LogPersisterRotator { 38 public: 39 LogPersisterRotator(const std::string& path, uint32_t id, uint32_t maxFiles, const std::string& suffix = ""); 40 ~LogPersisterRotator(); 41 int Init(const PersistRecoveryInfo& info, bool restore = false); 42 int Input(const char *buf, uint32_t length); 43 void FinishInput(); 44 45 void SetFileIndex(uint32_t index, bool forceRotate); 46 private: 47 void RemoveOldFile(); 48 bool IsOldFile(const std::string& logName, const int index); 49 int OpenInfoFile(); 50 void UpdateRotateNumber(); 51 void WriteRecoveryInfo(); 52 int SetInfo(const LogPersistStartMsg& pMsg, uint16_t logType, uint8_t logLevel); 53 54 void CreateLogFile(); 55 void Rotate(); 56 57 uint32_t m_maxLogFileNum = 0; 58 std::string m_logsPath; 59 std::string m_fileNameSuffix; 60 uint32_t m_currentLogFileIdx = 0; 61 std::fstream m_currentLogOutput; 62 63 uint32_t m_id = 0; 64 std::fstream m_infoFile; 65 std::string m_infoFilePath; 66 PersistRecoveryInfo m_info = {0}; 67 68 bool m_needRotate = false; 69 }; 70 } // namespace HiviewDFX 71 } // namespace OHOS 72 #endif 73