From c12c9a0cbaacefa097c23c8e040ad78e160e0853 Mon Sep 17 00:00:00 2001 From: houpengfei Date: Tue, 22 Mar 2022 12:40:50 +0000 Subject: [PATCH] Signed-off-by:hhh2 Signed-off-by: houpengfei Change-Id: I35dea2a28c384f4f819c99cabf346e852ce3331c --- .../miscdevice/src/light_service_client.cpp | 9 +- utils/include/miscdevice_log.h | 53 ++++++ utils/include/sensors_errors.h | 157 ++++++++++++++++++ 3 files changed, 213 insertions(+), 6 deletions(-) create mode 100755 utils/include/miscdevice_log.h diff --git a/frameworks/native/miscdevice/src/light_service_client.cpp b/frameworks/native/miscdevice/src/light_service_client.cpp index 62080d0..ffd6c3f 100644 --- a/frameworks/native/miscdevice/src/light_service_client.cpp +++ b/frameworks/native/miscdevice/src/light_service_client.cpp @@ -35,17 +35,14 @@ constexpr uint32_t WAIT_MS = 200; int32_t LightServiceClient::InitServiceClient() { - HiLog::Debug(LABEL, "%{public}s begin", __func__); + CALL_LOG_ENTER; std::lock_guard clientLock(clientMutex_); if (miscdeviceProxy_ != nullptr) { HiLog::Debug(LABEL, "%{public}s already init", __func__); return ERR_OK; } auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (sm == nullptr) { - HiLog::Error(LABEL, "%{public}s sm cannot be null", __func__); - return MISC_NATIVE_SAM_ERR; - } + CHKPR(sm, MISC_NATIVE_SAM_ERR); int32_t retry = 0; while (retry < GET_SERVICE_MAX_COUNT) { miscdeviceProxy_ = iface_cast(sm->GetSystemAbility(MISCDEVICE_SERVICE_ABILITY_ID)); @@ -57,7 +54,7 @@ int32_t LightServiceClient::InitServiceClient() } return ERR_OK; } - HiLog::Warn(LABEL, "%{public}s get service failed, retry : %{public}d", __func__, retry); + MISC_LOGW("get service failed, retry : %{public}d", retry); std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS)); retry++; } diff --git a/utils/include/miscdevice_log.h b/utils/include/miscdevice_log.h new file mode 100755 index 0000000..1a4b298 --- /dev/null +++ b/utils/include/miscdevice_log.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MISCDEVICE_LOG_H +#define MISCDEVICE_LOG_H + +#include "hilog/log.h" + +#include "miscdevice_log.h" +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +namespace { +constexpr uint32_t MISC_LOG_DOMAIN = 0xD002800; +} // namespace +#ifndef MISC_FUNC_FMT +#define MISC_FUNC_FMT "in %{public}s, #%{public}d, " +#endif + +#ifndef MISC_FUNC_INFO +#define MISC_FUNC_INFO __FUNCTION__, __LINE__ +#endif + +#define MISC_LOGD(fmt, ...) do { \ + OHOS::HiviewDFX::HiLog::Debug(LABEL, MISC_FUNC_FMT fmt, MISC_FUNC_INFO, ##__VA_ARGS__); \ +} while (0) +#define MISC_LOGI(fmt, ...) do { \ + OHOS::HiviewDFX::HiLog::Info(LABEL, MISC_FUNC_FMT fmt, MISC_FUNC_INFO, ##__VA_ARGS__); \ +} while (0) +#define MISC_LOGW(fmt, ...) do { \ + OHOS::HiviewDFX::HiLog::Warn(LABEL, MISC_FUNC_FMT fmt, MISC_FUNC_INFO, ##__VA_ARGS__); \ +} while (0) +#define MISC_LOGE(fmt, ...) do { \ + OHOS::HiviewDFX::HiLog::Error(LABEL, MISC_FUNC_FMT fmt, MISC_FUNC_INFO, ##__VA_ARGS__); \ +} while (0) +#define MISC_LOGF(fmt, ...) do { \ + OHOS::HiviewDFX::HiLog::Fatal(LABEL, MISC_FUNC_FMT fmt, MISC_FUNC_INFO, ##__VA_ARGS__); \ +} while (0) +} // namespace Sensors +} // namespace OHOS +#endif // MISCDEVICE_LOG_H \ No newline at end of file diff --git a/utils/include/sensors_errors.h b/utils/include/sensors_errors.h index 3cc1b65..45f027f 100644 --- a/utils/include/sensors_errors.h +++ b/utils/include/sensors_errors.h @@ -16,7 +16,10 @@ #ifndef SENSORS_ERRORS_H #define SENSORS_ERRORS_H +#include "hilog/log.h" + #include +#include "miscdevice_log.h" namespace OHOS { namespace Sensors { @@ -76,6 +79,160 @@ enum { MISC_NATIVE_GET_SERVICE_ERR = MISCDEVICE_NATIVE_SAM_ERR + 1, MISC_NATIVE_SAM_ERR = MISC_NATIVE_GET_SERVICE_ERR + 1, }; + +class InnerFunctionTracer { +public: + InnerFunctionTracer(const OHOS::HiviewDFX::HiLogLabel& label, const char *func) + : label_ { label }, func_ { func } + { + OHOS::HiviewDFX::HiLog::Debug(label_, "in %{public}s, enter", func_); + } + ~InnerFunctionTracer() + { + OHOS::HiviewDFX::HiLog::Debug(label_, "in %{public}s, leave", func_); + } +private: + const OHOS::HiviewDFX::HiLogLabel& label_; + const char* func_ { nullptr }; +}; + +#define CALL_LOG_ENTER InnerFunctionTracer ___innerFuncTracer___ { LABEL, __FUNCTION__ } + +#ifdef DEBUG_CODE_TEST +#define CHKPL(cond, ...) \ + do { \ + if ((cond) == nullptr) { \ + MISC_LOGE("%{public}s, (%{public}d), CHKPL(%{public}s) is null, do nothing", \ + __FILE__, __LINE__, #cond); \ + } \ + } while (0) + +#define CHKPV(cond) \ + do { \ + if ((cond) == nullptr) { \ + MISC_LOGE("%{public}s, (%{public}d), CHKPV(%{public}s) is null", \ + __FILE__, __LINE__, #cond); \ + return; \ + } \ + } while (0) + +#define CHKPF(cond) \ + do { \ + if ((cond) == nullptr) { \ + MISC_LOGE("%{public}s, (%{public}d), CHKPF(%{public}s) is null", \ + __FILE__, __LINE__, #cond); \ + return false; \ + } \ + } while (0) + +#define CHKPC(cond) \ + { \ + if ((cond) == nullptr) { \ + MISC_LOGE("%{public}s, (%{public}d), CHKPC(%{public}s) is null, skip then continue", \ + __FILE__, __LINE__, #cond); \ + continue; \ + } \ + } + +#define CHKPB(cond) \ + { \ + if ((cond) == nullptr) { \ + MISC_LOGE("%{public}s, (%{public}d), CHKPC(%{public}s) is null, skip then break", \ + __FILE__, __LINE__, #cond); \ + break; \ + } \ + } + +#define CHKPR(cond, r) \ + do { \ + if ((cond) == nullptr) { \ + MISC_LOGE("%{public}s, (%{public}d), CHKPR(%{public}s) is null, return value is %{public}d", \ + __FILE__, __LINE__, #cond, r); \ + return r; \ + } \ + } while (0) + +#define CHKPP(cond) \ + do { \ + if ((cond) == nullptr) { \ + MISC_LOGE("%{public}s, (%{public}d), CHKPP(%{public}s) is null, return value is null", \ + __FILE__, __LINE__, #cond); \ + return nullptr; \ + } \ + } while (0) + +#define CK(cond, ec) \ + do { \ + if (!(cond)) { \ + MISC_LOGE("%{public}s, (%{public}d), CK(%{public}s), errCode:%{public}d", \ + __FILE__, __LINE__, #cond, ec); \ + } \ + } while (0) + +#else // DEBUG_CODE_TEST +#define CHKPL(cond) \ + do { \ + if ((cond) == nullptr) { \ + MISC_LOGE("CHKPL(%{public}s) is null, do nothing", #cond); \ + } \ + } while (0) + +#define CHKPV(cond) \ + do { \ + if ((cond) == nullptr) { \ + MISC_LOGE("CHKPV(%{public}s) is null", #cond); \ + return; \ + } \ + } while (0) + +#define CHKPF(cond) \ + do { \ + if ((cond) == nullptr) { \ + MISC_LOGE("CHKPF(%{public}s) is null", #cond); \ + return false; \ + } \ + } while (0) + +#define CHKPC(cond) \ + { \ + if ((cond) == nullptr) { \ + MISC_LOGE("CHKPC(%{public}s) is null, skip then continue", #cond); \ + continue; \ + } \ + } + +#define CHKPB(cond) \ + { \ + if ((cond) == nullptr) { \ + MISC_LOGE("CHKPC(%{public}s) is null, skip then break", #cond); \ + break; \ + } \ + } + +#define CHKPR(cond, r) \ + do { \ + if ((cond) == nullptr) { \ + MISC_LOGE("CHKPR(%{public}s) is null, return value is %{public}d", #cond, r); \ + return r; \ + } \ + } while (0) + +#define CHKPP(cond) \ + do { \ + if ((cond) == nullptr) { \ + MISC_LOGE("CHKPP(%{public}s) is null, return value is null", #cond); \ + return nullptr; \ + } \ + } while (0) + +#define CK(cond, ec) \ + do { \ + if (!(cond)) { \ + MISC_LOGE("CK(%{public}s), errCode:%{public}d", #cond, ec); \ + } \ + } while (0) + +#endif } // namespace Sensors } // namespace OHOS #endif // SENSORS_ERRORS_H -- Gitee