1 /* 2 * Copyright (c) 2024 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 OHOS_HIVIEWDFX_LOG_FILE_WRITER_H 17 #define OHOS_HIVIEWDFX_LOG_FILE_WRITER_H 18 19 #include <fstream> 20 #include <list> 21 #include <mutex> 22 #include <string> 23 24 namespace OHOS { 25 namespace HiviewDFX { 26 struct LogStrategy { 27 // the name prefix of the log file 28 std::string fileNamePrefix; 29 30 // the max count of the log file with same type 31 size_t fileMaxCnt = 0; 32 33 // the size limit of a single log file 34 uint64_t singleFileMaxSize = 0; 35 }; 36 37 class LogFileWriter { 38 public: 39 LogFileWriter(const LogStrategy& strategy); 40 ~LogFileWriter(); 41 42 public: 43 void Write(const std::string& content); 44 45 private: 46 void DeleteOutNumberLogFiles(); 47 void InitByStrategy(const LogStrategy& strategy); 48 void ResetLogFileStreamByFileIndex(size_t fileIndex); 49 50 private: 51 std::mutex writeMutex_; 52 LogStrategy logStrategy_; 53 std::ofstream logFileStream_; 54 size_t curFileIndex_ = 0; 55 uint64_t curFileSize_ = 0; 56 }; 57 } // namespace HiviewDFX 58 } // namespace OHOS 59 60 #endif // OHOS_HIVIEWDFX_LOG_FILE_WRITER_H