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 #include "camera_hal_hisysevent.h"
17 #define MAX_STRING_SIZE 256
18 
19 namespace OHOS::Camera {
20 
21 #define TOSTR(x) #x
22 
23 std::map<ErrorEventType, std::string> g_eventNameMap = {
24     {CREATE_PIPELINE_ERROR, "CREATE_PIPELINE_ERROR"},
25     {TURN_BUFFER_ERROR, "TURN_BUFFER_ERROR"},
26     {REQUEST_BUFFER_ERROR, "REQUEST_BUFFER_ERROR"},
27     {REQUEST_GRAPHIC_BUFFER_ERROR, "REQUEST_GRAPHIC_BUFFER_ERROR"},
28     {COPY_BUFFER_ERROR, "COPY_BUFFER_ERROR"},
29     {TYPE_CAST_ERROR, "TYPE_CAST_ERROR"},
30     {OPEN_DEVICE_NODE_ERROR, "OPEN_DEVICE_NODE_ERROR"},
31     {FORMAT_CAST_ERROR, "FORMAT_CAST_ERROR"}
32 };
33 
34 std::map<PerformanceEventType, std::string> g_perfEventNameMap = {
35     {TIME_FOR_OPEN_CAMERA, std::string(TOSTR(TIME_FOR_OPEN_CAMERA))},
36     {TIME_FOR_CAPTURE, std::string(TOSTR(TIME_FOR_CAPTURE))},
37     {TIME_FOR_FIRST_FRAME, std::string(TOSTR(TIME_FOR_FIRST_FRAME))}
38 };
39 
40 std::map<StatisicEventType, std::string> g_staEventNameMap = {
41     {TIME_OF_CAPTURE, std::string(TOSTR(TIME_OF_CAPTURE))},
42     {TIME_OF_VEDIOA_AND_DURATION, std::string(TOSTR(TIME_OF_VEDIOA_AND_DURATION))},
43     {INFORMATION_OF_CAMERA, std::string(TOSTR(INFORMATION_OF_CAMERA))},
44     {PARAMS_OFCAPTURE_OR_VEDIO, std::string(TOSTR(PARAMS_OFCAPTURE_OR_VEDIO))},
45 };
46 
CreateMsg(const char * format,...)47 std::string CameraHalHisysevent::CreateMsg(const char* format, ...)
48 {
49     va_list args;
50     va_start(args, format);
51     char msg[MAX_STRING_SIZE] = {0};
52     if (vsnprintf_s(msg, sizeof(msg), sizeof(msg) - 1, format, args) < 0) {
53         CAMERA_LOGE("failed to call vsnprintf_s");
54         va_end(args);
55         return "";
56     }
57     va_end(args);
58     return msg;
59 }
60 
GetEventName(ErrorEventType errorEventType)61 std::string CameraHalHisysevent::GetEventName(ErrorEventType errorEventType)
62 {
63     auto it = g_eventNameMap.find(errorEventType);
64     if (it != g_eventNameMap.end()) {
65         return g_eventNameMap[errorEventType];
66     }
67     return "";
68 }
69 
WriteFaultHisysEvent(const std::string & name,const std::string & msg)70 void CameraHalHisysevent::WriteFaultHisysEvent(const std::string &name, const std::string &msg)
71 {
72     CAMERA_LOGI("WriteFaultHisysEvent name:%{public}s msg:%{public}s", name.c_str(), msg.c_str());
73     int32_t ret = HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::CAMERA_HAL, "CAMERA_HAL_ERR",
74                                   OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, "NAME", name, "MSG", msg);
75     if (ret != 0) {
76         CAMERA_LOGE("WriteFaultHisysEvent filed name:%{public}s ret:%{public}d", name.c_str(), ret);
77     }
78 }
79 
CameraHalPerfSysevent(PerformanceEventType perfEventType,bool isPrint,const char * name)80 CameraHalPerfSysevent::CameraHalPerfSysevent(PerformanceEventType perfEventType, bool isPrint, const char *name)
81     : perfEventType_(perfEventType), isPrint_(isPrint), funcName_(name)
82 {
83     begin = std::chrono::system_clock::now();
84 }
85 
~CameraHalPerfSysevent()86 CameraHalPerfSysevent::~CameraHalPerfSysevent()
87 {
88     if (isPrint_) {
89         std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
90         auto microsec = std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
91             HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::CAMERA_HAL, "CAMERA_HAL_PERFORMANCE",
92                 OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
93                 "NAME", g_perfEventNameMap[perfEventType_],
94                 "FUNCTION", std::string(funcName_),
95                 "TIMECOST", microsec);
96     }
97 }
98 
GetEventName(StatisicEventType statisicEventType)99 std::string CameraHalTimeSysevent::GetEventName(StatisicEventType statisicEventType)
100 {
101     auto it = g_staEventNameMap.find(statisicEventType);
102     if (it != g_staEventNameMap.end()) {
103         return g_staEventNameMap[statisicEventType];
104     }
105     return "";
106 }
107 
WriteTimeStatisicEvent(const std::string & name)108 void CameraHalTimeSysevent::WriteTimeStatisicEvent(const std::string &name)
109 {
110     std::chrono::system_clock::time_point begin = std::chrono::system_clock::now();
111     std::time_t now = std::chrono::system_clock::to_time_t(begin);
112     std::string timepointStr = std::ctime(&now);
113     CAMERA_LOGI("WriteTimeStatisicEvent name:%{public}s, timepoint:%{public}s", name.c_str(), timepointStr.c_str());
114     int32_t ret = HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::CAMERA_HAL, "CAMERA_HAL_STATISTIC",
115                                   OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
116                                   "NAME", name, "TIMEPOINT", timepointStr);
117     if (ret != 0) {
118         CAMERA_LOGE("WriteTimeStatisicEvent file name:%{public}s, ret:%{public}d", name.c_str(), ret);
119     }
120 }
121 
WriteCameraInformationEvent(const std::string & name,common_metadata_header_t * data)122 void CameraHalTimeSysevent::WriteCameraInformationEvent(const std::string &name, common_metadata_header_t *data)
123 {
124     CAMERA_LOGI("WriteCameraInformationEvent start!!");
125     camera_metadata_item_t entry;
126     if (FindCameraMetadataItem(data, OHOS_ABILITY_CAMERA_CONNECTION_TYPE, &entry) == 0) {
127         uint8_t cameraConnectType = *(entry.data.u8);
128         if (static_cast<int>(cameraConnectType) == OHOS_CAMERA_CONNECTION_TYPE_BUILTIN) {
129             CAMERA_LOGI("cameraConnectType is %{public}d", cameraConnectType);
130             HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::CAMERA_HAL, "CAMERA_HAL_STATISTIC",
131                             OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
132                             "NAME", name, "INFORMATION", cameraConnectType);
133         } else if (static_cast<int>(cameraConnectType) == OHOS_CAMERA_CONNECTION_TYPE_USB_PLUGIN) {
134             CAMERA_LOGI("cameraConnectType is %{public}d", cameraConnectType);
135             HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::CAMERA_HAL, "CAMERA_HAL_STATISTIC",
136                             OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
137                             "NAME", name, "INFORMATION", cameraConnectType);
138         } else {
139             CAMERA_LOGE("cameraConnectType not start!!");
140         }
141     } else {
142         CAMERA_LOGE("OHOS_ABILITY_CAMERA_CONNECTION_TYPE not find!!");
143     }
144 }
145 
WriteCameraParameterEvent(const std::string & name,common_metadata_header_t * data)146 void CameraHalTimeSysevent::WriteCameraParameterEvent(const std::string &name, common_metadata_header_t *data)
147 {
148     CAMERA_LOGI("WriteCameraParameterEvent start!!");
149     HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::CAMERA_HAL, "CAMERA_HAL_STATISTIC",
150                     OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
151                     "NAME", name, "PARAMETER", CameraMetadata::FormatCameraMetadataToString(data));
152 }
153 }