1 /*
2 * Copyright (c) 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 #include <media_dfx.h>
17 #include <unistd.h>
18 #include "securec.h"
19 #include "media_log.h"
20 #include "media_errors.h"
21 #include "hitrace_meter.h"
22
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "MediaDFX"};
25 constexpr uint32_t MAX_STRING_SIZE = 256;
26 }
27
28 namespace OHOS {
29 namespace Media {
CreateMsg(const char * format,...)30 bool MediaEvent::CreateMsg(const char *format, ...)
31 {
32 va_list args;
33 va_start(args, format);
34 char msg[MAX_STRING_SIZE] = {0};
35 if (vsnprintf_s(msg, sizeof(msg), sizeof(msg) - 1, format, args) < 0) {
36 MEDIA_LOGE("failed to call vsnprintf_s");
37 va_end(args);
38 return false;
39 }
40 va_end(args);
41 msg_ = msg;
42 return true;
43 }
44
EventWrite(std::string eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,std::string module)45 void MediaEvent::EventWrite(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
46 std::string module)
47 {
48 int32_t pid = getpid();
49 uint32_t uid = getuid();
50 OHOS::HiviewDFX::HiSysEvent::Write("MULTIMEDIA", eventName, type,
51 "PID", pid,
52 "UID", uid,
53 "MODULE", module,
54 "MSG", msg_);
55 }
56
BehaviorEventWrite(std::string status,std::string moudle)57 void BehaviorEventWrite(std::string status, std::string moudle)
58 {
59 MediaEvent event;
60 if (event.CreateMsg("%s, current state is: %s", "state change", status.c_str())) {
61 event.EventWrite("PLAYER_STATE", OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, moudle);
62 } else {
63 MEDIA_LOGW("Failed to call CreateMsg");
64 }
65 }
66
FaultEventWrite(std::string msg,std::string moudle)67 void FaultEventWrite(std::string msg, std::string moudle)
68 {
69 MediaEvent event;
70 if (event.CreateMsg("%s", msg.c_str())) {
71 event.EventWrite("PLAYER_ERR", OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, moudle);
72 } else {
73 MEDIA_LOGW("Failed to call CreateMsg");
74 }
75 }
76
MediaTrace(const std::string & funcName)77 MediaTrace::MediaTrace(const std::string &funcName)
78 {
79 StartTrace(HITRACE_TAG_ZMEDIA, funcName);
80 isSync_ = true;
81 }
82
TraceBegin(const std::string & funcName,int32_t taskId)83 void MediaTrace::TraceBegin(const std::string &funcName, int32_t taskId)
84 {
85 StartAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
86 }
87
TraceEnd(const std::string & funcName,int32_t taskId)88 void MediaTrace::TraceEnd(const std::string &funcName, int32_t taskId)
89 {
90 FinishAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
91 }
92
CounterTrace(const std::string & varName,int32_t val)93 void MediaTrace::CounterTrace(const std::string &varName, int32_t val)
94 {
95 CountTrace(HITRACE_TAG_ZMEDIA, varName, val);
96 }
97
~MediaTrace()98 MediaTrace::~MediaTrace()
99 {
100 if (isSync_) {
101 FinishTrace(HITRACE_TAG_ZMEDIA);
102 }
103 }
104 } // namespace Media
105 } // namespace OHOS
106