• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 HIVIEW_BASE_AUDIT_H
16 #define HIVIEW_BASE_AUDIT_H
17 #include <atomic>
18 #include <fstream>
19 #include <list>
20 #include <mutex>
21 #include <string>
22 
23 #include "public_defines.h"
24 #include "singleton.h"
25 
26 #include "unique_fd.h"
27 namespace OHOS {
28 namespace HiviewDFX {
29 class Audit : public Singleton<Audit> {
30 public:
Audit()31     Audit() : init_(false), useBak_(false), enabled_(false) {};
32     ~Audit();
33     enum StatsEvent {
34         QUEUE_EVENT_IN = 0,
35         QUEUE_EVENT_OUT,
36         PIPELINE_EVENT_CREATE,
37         PIPELINE_EVENT_HANDLE_IN,
38         PIPELINE_EVENT_HANDLE_OUT,
39         PIPELINE_EVENT_DONE,
40         PRIVATE_AUDIT_EVENT_TYPE
41     };
42 
43     // the format of the normal event digestion should be 1.sender 2.processor 3.thread 4.detailed event info
44     static bool WriteAuditEvent(StatsEvent eventType, uint64_t eventId, const std::string& digest = "");
45 
46     // get a copy of the audit log
47     // if loadFromFile is enabled, log store in the disk will be inserted into the front of current logs
48     static bool GetAuditLog(bool loadFromFile, std::list<std::string>& ret);
49 
50     static bool IsEnabled();
51 
52     // clear all logs
53     void Clear();
54 
55     // judge the version and do some initialization
56     void Init(bool isBeta);
57 
58     static constexpr char DOMAIN_DELIMITER = '|';
59 private:
60     bool IsActiveLogFileSizeReachTheashold();
61     bool ReadLogFromFile(const std::string& path, std::list<std::string>& ret);
62     bool IsBackupFileActive();
63     const std::string GetLogFilePath(bool active);
64     void GetAuditLogInner(bool loadFromFile, std::list<std::string>& ret);
65     void SaveToFileLocked(const std::string& content);
66     void SwitchActiveFile();
67     void WriteEvent(const std::string& content);
68     bool init_;
69     bool useBak_;
70     bool enabled_;
71     UniqueFd writeFd_;
72     std::mutex mutex_;
73     std::once_flag initFlag_;
74     std::atomic<uint32_t> writeLogCount_;
75     static constexpr uint32_t MAX_MEMORY_LOG_COUNT = 1024; // 1024 lines
76     static constexpr uint32_t MAX_DISK_LOG_SIZE = 1024 * 512; // 512KB
77     static constexpr uint32_t MAX_AUDIT_LOG_SIZE = 1024; // 1024 byte
78 };
79 } // namespace HiviewDFX
80 } // namespace OHOS
81 #endif // HIVIEW_BASE_AUDIT_H