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 CONTENT_READER_H
17 #define CONTENT_READER_H
18 
19 #include <fstream>
20 #include <string>
21 
22 #include "base_def.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace EventRaw {
27 class RawData;
28 }
29 using EventRaw::RawData;
30 
31 struct EventInfo {
32     char domain[MAX_DOMAIN_LEN] = {0};
33 
34     char name[MAX_EVENT_NAME_LEN] = {0};
35 
36     std::string level;
37 
38     std::string tag;
39 
40     int64_t seq = 0;
41 
42     int64_t timestamp = 0;
43 };
44 using EventInfo = struct EventInfo;
45 
46 struct HeadExtraInfo {
47     std::string sysVersion;
48     std::string patchVersion;
49 };
50 
51 class ContentReader {
52 public:
~ContentReader()53     virtual ~ContentReader() {};
54 
55 public:
56     static uint8_t ReadFmtVersion(std::ifstream& docStream);
57     std::shared_ptr<RawData> ReadRawData(const EventInfo& eventInfo, uint8_t* content, uint32_t contentSize);
58     virtual int ReadDocDetails(std::ifstream& docStream, EventStore::DocHeader& header,
59         uint64_t& docHeaderSize, HeadExtraInfo& headExtra) = 0;
60     virtual bool IsValidMagicNum(const uint64_t magicNum) = 0;
61 
62 protected:
63     virtual int GetContentHeader(uint8_t* content, EventStore::ContentHeader& header) = 0;
64     virtual size_t GetContentHeaderSize() = 0;
65 
66 protected:
67     int AppendDomainAndName(std::shared_ptr<RawData> rawData, const EventInfo& eventInfo);
68     int AppendContentData(std::shared_ptr<RawData> rawData, uint8_t* content, uint32_t contentSize);
69     int AppendExtraInfo(std::shared_ptr<RawData> rawData, const EventInfo& eventInfo);
70     int AppendTag(std::shared_ptr<RawData> rawData, const std::string& tag);
71     int AppendLevel(std::shared_ptr<RawData> rawData, const std::string& level);
72     int AppendSeq(std::shared_ptr<RawData> rawData, int64_t seq);
73     bool GetDataString(std::ifstream& docStream, std::string& value);
74 };
75 } // HiviewDFX
76 } // OHOS
77 #endif // CONTENT_READER_H