diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 126e24ef39ff98726f6df4183c76312187367037..e7273665fbda1e98e90cf1bb29453129c117fc44 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -93,18 +93,15 @@ const SensorAgentProxy *SensorAgentProxy::GetSensorsObj() void SensorAgentProxy::HandleSensorData(struct SensorEvent *events, int32_t num, void *data) { if (events == nullptr || num <= 0) { - HiLog::Error(LABEL, "%{public}s events is null or num is invalid", __func__); + SENSOR_LOGE("events is null or num is invalid"); return; } struct SensorEvent eventStream; for (int32_t i = 0; i < num; ++i) { eventStream = events[i]; - if (eventStream.data == nullptr) { - HiLog::Error(LABEL, "%{public}s data is nullptr", __func__); - return; - } + CHKPV(eventStream.data); if (g_subscribeMap.find(eventStream.sensorTypeId) == g_subscribeMap.end()) { - HiLog::Error(LABEL, "%{public}s sensorTypeId not in g_subscribeMap", __func__); + SENSOR_LOGE("sensorTypeId not in g_subscribeMap"); return; } g_subscribeMap[eventStream.sensorTypeId]->callback(&eventStream); @@ -119,13 +116,10 @@ int32_t SensorAgentProxy::CreateSensorDataChannel() const HiLog::Info(LABEL, "%{public}s the channel has already been created", __func__); return ERR_OK; } - if (dataChannel_ == nullptr) { - HiLog::Error(LABEL, "%{public}s data channel cannot be null", __func__); - return INVALID_POINTER; - } + CHKPR(dataChannel_, INVALID_POINTER); auto ret = dataChannel_->CreateSensorDataChannel(HandleSensorData, nullptr); if (ret != ERR_OK) { - HiLog::Error(LABEL, "%{public}s create data channel failed, ret : %{public}d", __func__, ret); + SENSOR_LOGE("create data channel failed, ret: %{public}d", ret); return ret; } auto &client = SensorServiceClient::GetInstance(); @@ -148,10 +142,7 @@ int32_t SensorAgentProxy::DestroySensorDataChannel() const HiLog::Info(LABEL, "%{public}s channel has been destroyed", __func__); return ERR_OK; } - if (dataChannel_ == nullptr) { - HiLog::Error(LABEL, "%{public}s data channel cannot be null", __func__); - return INVALID_POINTER; - } + CHKPR(dataChannel_, INVALID_POINTER); int32_t ret = dataChannel_->DestroySensorDataChannel(); if (ret != ERR_OK) { HiLog::Error(LABEL, "%{public}s destory data channel failed, ret : %{public}d", __func__, ret); @@ -320,10 +311,7 @@ int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) } *count = sensorList_.size(); *sensorInfo = (SensorInfo *)malloc(sizeof(SensorInfo) * (*count)); - if (*sensorInfo == nullptr) { - HiLog::Error(LABEL, "%{public}s malloc sensorInfo failed", __func__); - return OHOS::Sensors::ERROR; - } + CHKPR(*sensorInfo, ERROR); for (int32_t index = 0; index < *count; ++index) { errno_t ret = strcpy_s((*sensorInfo + index)->sensorName, NAME_MAX_LEN, sensorList_[index].GetSensorName().c_str()); diff --git a/utils/include/sensor_log.h b/utils/include/sensor_log.h new file mode 100755 index 0000000000000000000000000000000000000000..382451db4cff04fe3ca1b9ec18a957640543217a --- /dev/null +++ b/utils/include/sensor_log.h @@ -0,0 +1,50 @@ +/* + * 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 SENSOR_LOG_H +#define SENSOR_LOG_H + +#include "hilog/log.h" + +namespace OHOS { +namespace MMI { +namespace { +constexpr uint32_t SENSOR_LOG_DOMAIN = 0xD002800; +} // namespace +#ifndef SENSOR_FUNC_FMT +#define SENSOR_FUNC_FMT "in %{public}s, #%{public}d, " +#endif + +#ifndef SENSOR_FUNC_INFO +#define SENSOR_FUNC_INFO __FUNCTION__, __LINE__ +#endif + +#define SENSOR_LOGD(fmt, ...) do { \ + OHOS::HiviewDFX::HiLog::Debug(LABEL, SENSOR_FUNC_FMT fmt, SENSOR_FUNC_INFO, ##__VA_ARGS__); \ +} while (0) +#define SENSOR_LOGI(fmt, ...) do { \ + OHOS::HiviewDFX::HiLog::Info(LABEL, SENSOR_FUNC_FMT fmt, SENSOR_FUNC_INFO, ##__VA_ARGS__); \ +} while (0) +#define SENSOR_LOGW(fmt, ...) do { \ + OHOS::HiviewDFX::HiLog::Warn(LABEL, SENSOR_FUNC_FMT fmt, SENSOR_FUNC_INFO, ##__VA_ARGS__); \ +} while (0) +#define SENSOR_LOGE(fmt, ...) do { \ + OHOS::HiviewDFX::HiLog::Error(LABEL, SENSOR_FUNC_FMT fmt, SENSOR_FUNC_INFO, ##__VA_ARGS__); \ +} while (0) +#define SENSOR_LOGF(fmt, ...) do { \ + OHOS::HiviewDFX::HiLog::Fatal(LABEL, SENSOR_FUNC_FMT fmt, SENSOR_FUNC_INFO, ##__VA_ARGS__); \ +} while (0) +} // namespace MMI +} // namespace OHOS +#endif // SENSOR_LOG_H \ No newline at end of file diff --git a/utils/include/sensors_errors.h b/utils/include/sensors_errors.h index d6e5ec6f97ef9b221a82221c6afd6683652ec017..48c713a7976ed7e5f1f3a01114567403ec208fa2 100644 --- a/utils/include/sensors_errors.h +++ b/utils/include/sensors_errors.h @@ -17,6 +17,8 @@ #define SENSORS_ERRORS_H #include +#include + #include "hilog/log.h" namespace OHOS { @@ -122,7 +124,144 @@ 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) { \ + SENSOR_LOGE("%{public}s, (%{public}d), CHKPL(%{public}s) is null, do nothing", \ + __FILE__, __LINE__, #cond); \ + } \ + } while (0) + +#define CHKPV(cond) \ + do { \ + if ((cond) == nullptr) { \ + SENSOR_LOGE("%{public}s, (%{public}d), CHKPV(%{public}s) is null", \ + __FILE__, __LINE__, #cond); \ + return; \ + } \ + } while (0) + +#define CHKPF(cond) \ + do { \ + if ((cond) == nullptr) { \ + SENSOR_LOGE("%{public}s, (%{public}d), CHKPF(%{public}s) is null", \ + __FILE__, __LINE__, #cond); \ + return false; \ + } \ + } while (0) + +#define CHKPC(cond) \ + { \ + if ((cond) == nullptr) { \ + SENSOR_LOGE("%{public}s, (%{public}d), CHKPC(%{public}s) is null, skip then continue", \ + __FILE__, __LINE__, #cond); \ + continue; \ + } \ + } + +#define CHKPB(cond) \ + { \ + if ((cond) == nullptr) { \ + SENSOR_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) { \ + SENSOR_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) { \ + SENSOR_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)) { \ + SENSOR_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) { \ + SENSOR_LOGE("CHKPL(%{public}s) is null, do nothing", #cond); \ + } \ + } while (0) + +#define CHKPV(cond) \ + do { \ + if ((cond) == nullptr) { \ + SENSOR_LOGE("CHKPV(%{public}s) is null", #cond); \ + return; \ + } \ + } while (0) + +#define CHKPF(cond) \ + do { \ + if ((cond) == nullptr) { \ + SENSOR_LOGE("CHKPF(%{public}s) is null", #cond); \ + return false; \ + } \ + } while (0) + +#define CHKPC(cond) \ + { \ + if ((cond) == nullptr) { \ + SENSOR_LOGE("CHKPC(%{public}s) is null, skip then continue", #cond); \ + continue; \ + } \ + } + +#define CHKPB(cond) \ + { \ + if ((cond) == nullptr) { \ + SENSOR_LOGE("CHKPC(%{public}s) is null, skip then break", #cond); \ + break; \ + } \ + } + +#define CHKPR(cond, r) \ + do { \ + if ((cond) == nullptr) { \ + SENSOR_LOGE("CHKPR(%{public}s) is null, return value is %{public}d", #cond, r); \ + return r; \ + } \ + } while (0) + +#define CHKPP(cond) \ + do { \ + if ((cond) == nullptr) { \ + SENSOR_LOGE("CHKPP(%{public}s) is null, return value is null", #cond); \ + return nullptr; \ + } \ + } while (0) + +#define CK(cond, ec) \ + do { \ + if (!(cond)) { \ + SENSOR_LOGE("CK(%{public}s), errCode:%{public}d", #cond, ec); \ + } \ + } while (0) + +#endif } // namespace Sensors } // namespace OHOS -#define CALL_LOG_ENTER InnerFunctionTracer ___innerFuncTracer___ { LABEL, __FUNCTION__ } #endif // SENSORS_ERRORS_H