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 HI_AUDIT_H 17 #define 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 struct AuditLog { 29 bool isUserBehavior; 30 std::string cause; 31 std::string operationType; 32 std::string operationScenario; 33 uint32_t operationCount; 34 std::string operationStatus; 35 std::string extend; 36 std::string id; 37 int32_t type; 38 int32_t size; 39 TitleStringAuditLog40 const std::string TitleString() const 41 { 42 return "happenTime, packageName, isForeground, cause, isUserBehavior, operationType, operationScenario, " 43 "operationStatus, operationCount, extend, id, type, size\n"; 44 } 45 ToStringAuditLog46 const std::string ToString() const 47 { 48 return cause + ", " + std::to_string(isUserBehavior) + ", " + operationType + ", " + operationScenario + 49 ", " + operationStatus + ", " + std::to_string(operationCount) + ", " + extend + ", " + id + ", " + 50 std::to_string(type) + ", " + std::to_string(size) + "\n"; 51 } 52 }; 53 54 struct FileAuditLog : public AuditLog { 55 std::string type; 56 std::string path; 57 std::string targetPath; 58 std::string createTime; 59 uint64_t size; 60 TitleStringFileAuditLog61 const std::string TitleString() const 62 { 63 return AuditLog::TitleString() + ", type, path, targetPath, createTime, size"; 64 } 65 ToStringFileAuditLog66 const std::string ToString() const 67 { 68 return AuditLog::ToString() + ", " + type + ", " + path + ", " + targetPath + ", " + createTime + 69 ", " + std::to_string(size); 70 } 71 }; 72 73 struct DatabaseAuditLog : public AuditLog { 74 std::string dbStatus; 75 std::string hwidStatus; 76 TitleStringDatabaseAuditLog77 const std::string TitleString() const 78 { 79 return AuditLog::TitleString() + ", dbStatus, hwidStatus"; 80 } 81 ToStringDatabaseAuditLog82 const std::string ToString() const 83 { 84 return AuditLog::ToString() + ", " + dbStatus + ", " + hwidStatus; 85 } 86 }; 87 88 class HiAudit : public NoCopyable { 89 public: 90 static HiAudit& GetInstance(); 91 void Write(const AuditLog& auditLog); 92 93 private: 94 HiAudit(); 95 ~HiAudit(); 96 97 void Init(); 98 void GetWriteFilePath(); 99 void WriteToFile(const std::string& log); 100 uint64_t GetMilliseconds(); 101 std::string GetFormattedTimestamp(time_t timeStamp, const std::string& format); 102 std::string GetFormattedTimestampEndWithMilli(); 103 void CleanOldAuditFile(); 104 void ZipAuditLog(); 105 106 private: 107 std::mutex mutex_; 108 int writeFd_; 109 std::atomic<uint32_t> writeLogSize_ = 0; 110 }; 111 } 112 #endif