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 16 #ifndef LOG_STORE_LOG_STORE_EX_H 17 #define LOG_STORE_LOG_STORE_EX_H 18 19 #include "log_file.h" 20 21 #include <cinttypes> 22 #include <functional> 23 #include <ostream> 24 #include <string> 25 #include <vector> 26 27 #include <sys/stat.h> 28 #include <unistd.h> 29 30 namespace OHOS { 31 namespace HiviewDFX { 32 class LogStoreEx { 33 public: 34 using LogFileFilter = std::function<bool(const LogFile&)>; 35 using LogFileComparator = std::function<bool(const LogFile&, const LogFile&)>; 36 using FileHandle = int32_t; 37 38 // managing logs in specific path with following functions 39 // 1. get logs in specific order 40 // 2. remove old logs in specific rule 41 LogStoreEx(const std::string& path, bool autoDeleteFiles = false); ~LogStoreEx()42 ~LogStoreEx() {}; 43 44 // create path with expected permission 45 bool Init(); 46 void SetLogFileComparator(LogFileComparator comparator); 47 48 // Get all files in log store, sorted by last modify time by default 49 std::vector<LogFile> GetLogFiles(); 50 std::vector<LogFile> GetLogFiles(LogFileFilter filter); 51 52 // Get the path managed by store 53 const std::string& GetPath() const; 54 55 // Remove all log files in managed folder 56 bool Clear(); 57 58 // remove file according to the size and file count quota 59 // keep minimum number of file in store 60 void ClearOldestFilesIfNeeded(); 61 62 // remove filtered files if the number of files exceeds maximum count 63 void ClearSameLogFilesIfNeeded(LogFileFilter filter, uint32_t maxCount); 64 65 // create a log file inside store path 66 FileHandle CreateLogFile(const std::string& name); 67 bool RemoveLogFile(const std::string& name); 68 69 void SetMaxSize(uint32_t size); 70 void SetMinKeepingFileNumber(uint32_t number); 71 72 protected: 73 bool autoDeleteFiles_; 74 uint32_t maxSize_; 75 uint32_t minKeepingNumberOfFiles_; 76 LogFileComparator comparator_; 77 std::string path_; 78 79 private: 80 void DoDeleteLogFiles(const std::vector<LogFile> &fileList, int32_t removeFileNums) const; 81 }; 82 } // namespace HiviewDFX 83 } // namespace OHOS 84 #endif // LOG_STORE_LOG_STORE_EX_H 85