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 HIVIEW_BASE_EVENT_EXPORT_JSON_WRITER_H
17 #define HIVIEW_BASE_EVENT_EXPORT_JSON_WRITER_H
18 
19 #include <functional>
20 #include <map>
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 
25 #include "cJSON.h"
26 #include "export_db_storage.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 struct EventVersion {
31     // system version
32     std::string systemVersion;
33 
34     // patch version
35     std::string patchVersion;
36 };
37 
38 class ExportJsonFileWriter {
39 public:
40     ExportJsonFileWriter(const std::string& moduleName, const EventVersion& eventVersion, const std::string& exportDir,
41         int64_t maxFileSize);
42 
43 public:
44     using ExportJsonFileZippedListener = std::function<void(const std::string&, const std::string&)>;
45     void SetExportJsonFileZippedListener(ExportJsonFileZippedListener listener);
46 
47     void ClearEventCache();
48 
49 public:
50     bool Write();
51     bool AppendEvent(const std::string& domain, const std::string& name, const std::string& eventStr);
52 
53 private:
54     std::string moduleName_;
55     EventVersion eventVersion_;
56     std::string exportDir_;
57     int64_t maxFileSize_ = 0;
58     int64_t totalJsonStrSize_ = 0;
59     ExportJsonFileZippedListener exportJsonFileZippedListener_;
60     // <domain, <name, eventContentString>>
61     std::unordered_map<std::string, std::vector<std::pair<std::string, std::string>>> sysEventMap_;
62 };
63 } // namespace HiviewDFX
64 } // namespace OHOS
65 
66 #endif // HIVIEW_BASE_EVENT_EXPORT_JSON_WRITER_H