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 BACKUP_HI_AUDIT_H 17 #define BACKUP_HI_AUDIT_H 18 19 #include <cstdint> 20 #include <mutex> 21 #include <queue> 22 #include <string> 23 #include <sys/stat.h> 24 25 #include "nocopyable.h" 26 27 namespace OHOS::Media { 28 #define EXPORT __attribute__ ((visibility ("default"))) 29 struct BackupAuditLog { 30 bool isUserBehavior{false}; 31 std::string cause; 32 std::string operationType; 33 std::string operationScenario; 34 uint32_t operationCount{0}; 35 std::string operationStatus; 36 std::string extend; 37 std::string taskId; 38 TitleStringBackupAuditLog39 const virtual std::string TitleString() const 40 { 41 return "happenTime,packageName,isForeground,cause,isUserBehavior,operationType,operationScenario," 42 "operationStatus,operationCount,extend,taskId"; 43 } 44 ToStringBackupAuditLog45 const virtual std::string ToString() const 46 { 47 return cause + "," + std::to_string(isUserBehavior) + "," + operationType + "," + operationScenario + 48 "," + operationStatus + "," + std::to_string(operationCount) + "," + extend + "," + taskId; 49 } 50 }; 51 52 class BackupHiAudit : public NoCopyable { 53 public: 54 EXPORT static BackupHiAudit& GetInstance(); 55 EXPORT void Write(const BackupAuditLog& auditLog); 56 57 private: 58 BackupHiAudit(); 59 ~BackupHiAudit(); 60 61 void Init(); 62 void GetWriteFilePath(); 63 void WriteToFile(const std::string& log); 64 uint64_t GetMilliseconds(); 65 std::string GetFormattedTimestamp(time_t timeStamp, const std::string& format); 66 std::string GetFormattedTimestampEndWithMilli(); 67 void CleanOldAuditFile(); 68 void ZipAuditLog(); 69 70 private: 71 std::mutex mutex_; 72 int writeFd_; 73 std::atomic<uint32_t> writeLogSize_ = 0; 74 }; 75 } // namespace OHOS::Media 76 #endif // BACKUP_HI_AUDIT_H