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 "image_func_timer.h"
17 
18 #include "securec.h"
19 
20 #include "hitrace_meter.h"
21 #include "image_utils.h"
22 #include "image_log.h"
23 
24 namespace OHOS {
25 namespace Media {
26 static constexpr int32_t FORMAT_BUF_SIZE = 254;
27 
ImageFuncTimer(const std::string & desc)28 ImageFuncTimer::ImageFuncTimer(const std::string &desc) : desc_(desc)
29 {
30 #if !defined(_WIN32) && !defined(_APPLE)
31     startTime_ = ImageUtils::GetNowTimeMicroSeconds();
32     StartTrace(HITRACE_TAG_ZIMAGE, desc);
33 #endif
34 }
35 
~ImageFuncTimer()36 ImageFuncTimer::~ImageFuncTimer()
37 {
38 #if !defined(_WIN32) && !defined(_APPLE)
39     FinishTrace(HITRACE_TAG_ZIMAGE);
40     uint64_t timeInterval = ImageUtils::GetNowTimeMicroSeconds() - startTime_;
41     IMAGE_LOGI("%{public}s cost %{public}llu us", desc_.c_str(), static_cast<unsigned long long>(timeInterval));
42 #endif
43 }
44 
ImageFuncTimer(const char * fmt,...)45 ImageFuncTimer::ImageFuncTimer(const char *fmt, ...)
46 {
47 #if !defined(_WIN32) && !defined(_APPLE)
48     if (fmt == nullptr) {
49         desc_ = "ImageFuncTimerFmt Param invalid";
50     } else {
51         char buf[FORMAT_BUF_SIZE] = { 0 };
52         va_list args;
53         va_start(args, fmt);
54         int32_t ret = vsprintf_s(buf, FORMAT_BUF_SIZE, fmt, args);
55         va_end(args);
56         if (ret != -1) {
57             desc_ = buf;
58         } else {
59             desc_ = "ImageTraceFmt Format Error";
60         }
61     }
62     startTime_ = ImageUtils::GetNowTimeMicroSeconds();
63     StartTrace(HITRACE_TAG_ZIMAGE, desc_);
64 #endif
65 }
66 
67 } // namespace Media
68 } // namespace OHOS