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 uint32_t MAX_STRING_SIZE = 256;
25 }
26
27 namespace OHOS {
28 namespace Media {
CreateMsg(const char * format,...)29 bool MediaEvent::CreateMsg(const char *format, ...)
30 {
31 va_list args;
32 va_start(args, format);
33 char msg[MAX_STRING_SIZE] = {0};
34 auto ret = vsnprintf_s(msg, sizeof(msg), sizeof(msg) - 1, format, args);
35 va_end(args);
36 msg_ = msg;
37 return ret < 0 ? false : true;
38 }
39
EventWrite(std::string eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,std::string module)40 void MediaEvent::EventWrite(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
41 std::string module)
42 {
43 int32_t pid = getpid();
44 uint32_t uid = getuid();
45 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
46 "PID", pid,
47 "UID", uid,
48 "MODULE", module,
49 "MSG", msg_);
50 }
51
BehaviorEventWrite(std::string status,std::string moudle)52 void BehaviorEventWrite(std::string status, std::string moudle)
53 {
54 MediaEvent event;
55 if (event.CreateMsg("%s, current state is: %s", "state change", status.c_str())) {
56 event.EventWrite("PLAYER_STATE", OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, moudle);
57 }
58 }
59
FaultEventWrite(std::string msg,std::string moudle)60 void FaultEventWrite(std::string msg, std::string moudle)
61 {
62 MediaEvent event;
63 if (event.CreateMsg("%s", msg.c_str())) {
64 event.EventWrite("PLAYER_ERR", OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, moudle);
65 }
66 }
67
MediaTrace(const std::string & funcName)68 MediaTrace::MediaTrace(const std::string &funcName)
69 {
70 StartTrace(HITRACE_TAG_ZMEDIA, funcName);
71 isSync_ = true;
72 }
73
TraceBegin(const std::string & funcName,int32_t taskId)74 void MediaTrace::TraceBegin(const std::string &funcName, int32_t taskId)
75 {
76 StartAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
77 }
78
TraceEnd(const std::string & funcName,int32_t taskId)79 void MediaTrace::TraceEnd(const std::string &funcName, int32_t taskId)
80 {
81 FinishAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
82 }
83
CounterTrace(const std::string & varName,int32_t val)84 void MediaTrace::CounterTrace(const std::string &varName, int32_t val)
85 {
86 CountTrace(HITRACE_TAG_ZMEDIA, varName, val);
87 }
88
~MediaTrace()89 MediaTrace::~MediaTrace()
90 {
91 if (isSync_) {
92 FinishTrace(HITRACE_TAG_ZMEDIA);
93 }
94 }
95 } // namespace Media
96 } // namespace OHOS
97