• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_APP_EVENT_READ_H
17 #define HI_APP_EVENT_READ_H
18 
19 #include <regex>
20 #include <string>
21 #include <vector>
22 
23 #include <time.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /**
30  * Value type of the timestamp, timestamp is always translated
31  * into milliseconds, eg. 1626266996728
32  * WARNING: int64_t type is always 64 bits in any platform
33  */
34 using TimeStampVarType = const int64_t;
35 
36 // Function type of the listener which observe on any update of
37 // the hiappevent real time log
38 using RealTimeEventLogListener = void(*)(const std::string&);
39 
40 // Function type of the listener which observe on the pull of the hiappevent
41 // history log
42 using HistoryEventLogListener = void(*)(const std::vector<std::string>&);
43 
44 /**
45  * A interface method for libjvmtiagent to call to set the unique
46  * listener which observe on any update of the hiappevent real
47  * time log
48  * WARNING: Any modification on the name of this method ISN'T permitted
49  */
50 void RegRealTimeAppLogListener(RealTimeEventLogListener listener);
51 
52 /**
53  * A interface method for libjvmtiagent to call to set the unique
54  * listener which observe on the pull of the hiappevent history
55  * log
56  * WARNING: Any modification on the name of this method ISN'T permitted
57  */
58 void RegHistoryAppLogListener(HistoryEventLogListener listener);
59 
60 /**
61  * A interaface method for libjvmtiagent to call to remove all listeners
62  * WARNING: Any modification on the name of this method ISN'T permitted
63  */
64 void RemoveAllListeners(void);
65 
66 // This method would always be called when a new real time hiappevent
67 // log record is persisted(hiappevent_write.cpp)
68 void RealTimeAppLogUpdate(const std::string& realTimeLog);
69 
70 // This method would always be called when the directory for the
71 // hiappevent log file to persist is setted.(hiappevent_config.cpp)
72 void UpdateHiAppEventLogDir(const std::string& logPersistDir);
73 
74 /**
75  * A interface method for libjvmtiagent to call to pull hiappevent history logs
76  * WARNING: Any modification on the name of this method ISN'T permitted.
77  */
78 void PullEventHistoryLog(TimeStampVarType beginTimeStamp, TimeStampVarType endTimeStamp,
79     int count);
80 
81 namespace OHOS {
82 namespace HiviewDFX {
83 class LogAssistant final {
84 public:
85     static LogAssistant& Instance();
86 public:
87     void RegRealTimeAppLogListener(RealTimeEventLogListener);
88     void RegHistoryAppLogListener(HistoryEventLogListener);
89     void RemoveAllListeners(void);
90     void RealTimeAppLogUpdate(const std::string&);
91     void UpdateHiAppEventLogDir(const std::string&);
92     void PullEventHistoryLog(TimeStampVarType, TimeStampVarType, int);
93 private:
94     LogAssistant();
95     LogAssistant(const LogAssistant&) = delete;
96     LogAssistant(const LogAssistant&&) = delete;
97     LogAssistant& operator=(const LogAssistant&) = delete;
98     LogAssistant& operator=(const LogAssistant&&) = delete;
99     ~LogAssistant();
100 private:
101     std::string FindMatchedRegex(const std::string&, const std::regex&);
102     std::string ParseLogLineTime(const std::string&);
103     bool CheckMatchedLogLine(const std::string&, TimeStampVarType, TimeStampVarType);
104     void ParseSingFileLogs(std::vector<std::string>&, const std::string&,
105         TimeStampVarType, TimeStampVarType, int);
106     void ParseAllHistoryLogs(std::vector<std::string>&, const std::vector<std::string>&,
107         TimeStampVarType, TimeStampVarType, int);
108     std::string ParseLogFileTimeStamp(const std::string&);
109     std::string TranslateLongToFormattedTimeStamp(TimeStampVarType);
110     bool IsMatchedLogFile(const std::string&, TimeStampVarType,
111         TimeStampVarType);
112     void AllMatchedLogFiles(std::vector<std::string>&, TimeStampVarType,
113         TimeStampVarType);
114     void ReadHistoryLogFromPersistFile(std::vector<std::string>&, TimeStampVarType,
115         TimeStampVarType, int);
116 private:
117     std::string logPersistDir;
118     // Listener which observe on any update of the hiappevent
119     // real time log
120     RealTimeEventLogListener realTimeLogUpdateListener;
121     // Listener which observe on the pull of the hiappevent
122     // history log
123     HistoryEventLogListener historyLogPulledListener;
124 };
125 } // namespace HiviewDFX
126 } // namespace OHOS
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 
132 #endif // HI_APP_EVENT_WRITE_H
133