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 #ifndef IMAGE_CODEC_LOG_H
17 #define IMAGE_CODEC_LOG_H
18 
19 #include <cinttypes>
20 #include <chrono>
21 #include "log_tags.h"
22 #include "hilog/log.h"
23 #ifdef BUILD_ENG_VERSION
24 #include "hitrace_meter.h"
25 #endif
26 
27 #undef LOG_DOMAIN
28 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
29 
30 #undef LOG_TAG
31 #define LOG_TAG "HEIF_HW_DECODER"
32 
33 #ifdef __FILE_NAME__
34 #define FILENAME __FILE_NAME__
35 #else
36 #define FILENAME __FILE__
37 #endif
38 
39 #define LOG_FMT "[%{public}s][%{public}s %{public}d] "
40 #define LOGE(x, ...) \
41     HILOG_ERROR(LOG_CORE, LOG_FMT x, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__)
42 #define LOGW(x, ...) \
43     HILOG_WARN(LOG_CORE, LOG_FMT x, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__)
44 #define LOGI(x, ...) \
45     HILOG_INFO(LOG_CORE, LOG_FMT x, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__)
46 #define LOGD(x, ...) \
47     HILOG_DEBUG(LOG_CORE, LOG_FMT x, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__)
48 
49 // for ImageCodecBuffer
50 #define HLOG_FMT "%{public}s[%{public}s][%{public}s %{public}d] "
51 #define HLOGE(x, ...) HILOG_ERROR(LOG_CORE, HLOG_FMT x, compUniqueStr_.c_str(), \
52     currState_->GetName().c_str(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
53 #define HLOGW(x, ...) HILOG_WARN(LOG_CORE, HLOG_FMT x, compUniqueStr_.c_str(), \
54     currState_->GetName().c_str(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
55 #define HLOGI(x, ...) HILOG_INFO(LOG_CORE, HLOG_FMT x, compUniqueStr_.c_str(), \
56     currState_->GetName().c_str(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
57 #define HLOGD(x, ...) \
58     do {    \
59         if (debugMode_) {   \
60             HILOG_DEBUG(LOG_CORE, HLOG_FMT x, compUniqueStr_.c_str(), \
61             currState_->GetName().c_str(), __FUNCTION__, __LINE__, ##__VA_ARGS__); \
62         }   \
63     } while (0)
64 
65 // for ImageCodecBuffer inner state
66 #define SLOGE(x, ...) HILOG_ERROR(LOG_CORE, HLOG_FMT x, \
67     codec_->compUniqueStr_.c_str(), stateName_.c_str(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
68 #define SLOGW(x, ...) HILOG_WARN(LOG_CORE, HLOG_FMT x, \
69     codec_->compUniqueStr_.c_str(), stateName_.c_str(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
70 #define SLOGI(x, ...) HILOG_INFO(LOG_CORE, HLOG_FMT x, \
71     codec_->compUniqueStr_.c_str(), stateName_.c_str(), __FUNCTION__, __LINE__, ##__VA_ARGS__)
72 #define SLOGD(x, ...) \
73     do {    \
74         if (codec_->debugMode_) {   \
75             HILOG_DEBUG(LOG_CORE, HLOG_FMT x, \
76             codec_->compUniqueStr_.c_str(), stateName_.c_str(), __FUNCTION__, __LINE__, ##__VA_ARGS__); \
77         }   \
78     } while (0)
79 
80 #define IF_TRUE_RETURN_VAL(cond, val)  \
81     do {                               \
82         if (cond) {                    \
83             return val;                \
84         }                              \
85     } while (0)
86 #define IF_TRUE_RETURN_VAL_WITH_MSG(cond, val, msg, ...) \
87     do {                                        \
88         if (cond) {                             \
89             LOGE(msg, ##__VA_ARGS__);           \
90             return val;                         \
91         }                                       \
92     } while (0)
93 #define IF_TRUE_RETURN_VOID(cond)  \
94     do {                                \
95         if (cond) {                     \
96             return;                     \
97         }                               \
98     } while (0)
99 #define IF_TRUE_RETURN_VOID_WITH_MSG(cond, msg, ...)     \
100     do {                                        \
101         if (cond) {                             \
102             LOGE(msg, ##__VA_ARGS__);           \
103             return;                             \
104         }                                       \
105     } while (0)
106 
107 #ifdef BUILD_ENG_VERSION
108 #ifdef H_SYSTRACE_TAG
109 #undef H_SYSTRACE_TAG
110 #endif
111 #define H_SYSTRACE_TAG HITRACE_TAG_ZMEDIA
112 #endif // BUILD_ENG_VERSION
113 
114 class HeifPerfTracker {
115 public:
HeifPerfTracker(std::string desc)116     explicit HeifPerfTracker(std::string desc) : desc_(desc)
117     {
118         startTimeInUs_ = GetCurrentTimeInUs();
119 #ifdef BUILD_ENG_VERSION
120         StartTrace(H_SYSTRACE_TAG, desc);
121 #endif
122     }
~HeifPerfTracker()123     ~HeifPerfTracker()
124     {
125 #ifdef BUILD_ENG_VERSION
126         FinishTrace(H_SYSTRACE_TAG);
127 #endif
128         static constexpr float MILLISEC_TO_MICROSEC = 1000.0f;
129         int64_t timeSpanInUs = GetCurrentTimeInUs() - startTimeInUs_;
130         LOGD("%{public}s cost: %{public}.2f ms",
131              desc_.c_str(), static_cast<float>(timeSpanInUs / MILLISEC_TO_MICROSEC));
132     }
133 private:
GetCurrentTimeInUs()134     int64_t GetCurrentTimeInUs()
135     {
136         auto now = std::chrono::steady_clock::now();
137         return std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
138     }
139 
140     int64_t startTimeInUs_;
141     std::string desc_;
142 };
143 
144 #endif // IMAGE_CODEC_LOG_H