• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "hiappevent_write.h"
17 
18 #include <mutex>
19 #include <string>
20 
21 #include "app_event_store.h"
22 #include "app_event_observer_mgr.h"
23 #include "file_util.h"
24 #include "hiappevent_base.h"
25 #include "hiappevent_clean.h"
26 #include "hiappevent_config.h"
27 #include "hiappevent_read.h"
28 #include "hilog/log.h"
29 #include "time_util.h"
30 #include "xcollie/watchdog.h"
31 
32 #undef LOG_DOMAIN
33 #define LOG_DOMAIN 0xD002D07
34 
35 #undef LOG_TAG
36 #define LOG_TAG "Write"
37 
38 namespace OHOS {
39 namespace HiviewDFX {
40 namespace {
41 constexpr int DB_FAILED = -1;
42 const std::string  MAIN_THREAD_JANK = "MAIN_THREAD_JANK";
43 std::mutex g_mutex;
44 
GetStorageDirPath()45 std::string GetStorageDirPath()
46 {
47     return HiAppEventConfig::GetInstance().GetStorageDir();
48 }
49 
GetStorageFileName()50 std::string GetStorageFileName()
51 {
52     return "app_event_" + TimeUtil::GetDate() + ".log";
53 }
54 
WriteEventToFile(const std::string & filePath,const std::string & event)55 bool WriteEventToFile(const std::string& filePath, const std::string& event)
56 {
57     LogAssistant::Instance().RealTimeAppLogUpdate(event);
58     return FileUtil::SaveStringToFile(filePath, event);
59 }
60 }
61 
WriteEvent(std::shared_ptr<AppEventPack> appEventPack)62 void WriteEvent(std::shared_ptr<AppEventPack> appEventPack)
63 {
64     if (HiAppEventConfig::GetInstance().GetDisable()) {
65         HILOG_WARN(LOG_CORE, "the HiAppEvent function is disabled.");
66         return;
67     }
68     if (appEventPack == nullptr) {
69         HILOG_ERROR(LOG_CORE, "appEventPack is null.");
70         return;
71     }
72     std::string dirPath = GetStorageDirPath();
73     if (dirPath.empty()) {
74         HILOG_ERROR(LOG_CORE, "dirPath is null, stop writing the event.");
75         return;
76     }
77     std::string event = appEventPack->GetEventStr();
78     HILOG_DEBUG(LOG_CORE, "WriteEvent domain=%{public}s, name=%{public}s.",
79         appEventPack->GetDomain().c_str(), appEventPack->GetName().c_str());
80     {
81         std::lock_guard<std::mutex> lockGuard(g_mutex);
82         if (!FileUtil::IsFileExists(dirPath) && !FileUtil::ForceCreateDirectory(dirPath)) {
83             HILOG_ERROR(LOG_CORE, "failed to create hiappevent dir, errno=%{public}d.", errno);
84             return;
85         }
86         HiAppEventClean::CheckStorageSpace();
87         std::string filePath = FileUtil::GetFilePathByDir(dirPath, GetStorageFileName());
88         if (WriteEventToFile(filePath, event)) {
89             std::vector<std::shared_ptr<AppEventPack>> events;
90             events.emplace_back(appEventPack);
91             AppEventObserverMgr::GetInstance().HandleEvents(events);
92             return;
93         }
94         HILOG_ERROR(LOG_CORE, "failed to write event to log file, errno=%{public}d.", errno);
95     }
96 }
97 
SetEventParam(std::shared_ptr<AppEventPack> appEventPack)98 int SetEventParam(std::shared_ptr<AppEventPack> appEventPack)
99 {
100     if (appEventPack == nullptr) {
101         HILOG_ERROR(LOG_CORE, "appEventPack is null.");
102         return ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL;
103     }
104     int res = AppEventStore::GetInstance().InsertCustomEventParams(appEventPack);
105     if (res == DB_FAILED) {
106         HILOG_ERROR(LOG_CORE, "failed to insert event param, domain=%{public}s.", appEventPack->GetDomain().c_str());
107         return ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL;
108     }
109     return res;
110 }
111 
SetEventConfig(const std::string & name,const std::map<std::string,std::string> & configMap)112 int SetEventConfig(const std::string& name, const std::map<std::string, std::string> &configMap)
113 {
114     if (name != MAIN_THREAD_JANK) {
115         HILOG_ERROR(LOG_CORE, "Failed to set event config, name is invalid. name=%{public}s", name.c_str());
116         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
117     }
118     return Watchdog::GetInstance().SetEventConfig(configMap);
119 }
120 } // namespace HiviewDFX
121 } // namespace OHOS
122