2023-12-14 分析android 系统标准的打印方法 ALOGD ALOGI ALOGE,因为ndk里面是不能使用这些函数,特意花时间跟踪一下这部分代码。本文包含封装log打印函数实例

本文详细介绍了如何在Android系统中使用ALOGD, ALOGE等日志打印方法,以及在NDK环境下由于缺少libcutils库而需要自行封装log打印函数的过程。通过对源码的跟踪,解析了从包含<utils/Log.h>到最终调用__android_log_print函数的路径。同时,提供了在NDK中使用ALOGI, ALOGE的封装示例。" 113270614,10553608,NIFI数据库间数据同步实战,"['数据处理', '数据库', 'NIFI', '数据同步', 'MySQL']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、要想使用ALOGD ALOGE等,需要添加头文件#include <utils/Log.h> ,然后在Android.mk里面增加,跟踪到最后是调用system/core/liblog/include/android/log.h里面的__android_log_print函数。ndk里面是没有libcutils 的。

OCAL_SHARED_LIBRARIES := \
						libcutils \
						liblog

二、我们这里就拿ALOGD来分析,跟踪一下源码。

       1.1 、system/core/libcutils/include/cutils/log.h

       1.2 、system/core/liblog/include/log/log.h

       1.3 、system/core/liblog/include/log/log_main.h

#include "OplusGameOpt.h" #include <math.h> #include <stdlib.h> #include <utils/Log.h> #include <utils/String8.h> #include <android/binder_manager.h> #include <android/binder_process.h> #include <log/log.h> #include <android-base/logging.h> using namespace std; using namespace android; using android::hidl::base::V1_0::IBase; using ::android::sp; using ::aidl::vendor::oplus::hardware::gameopt::IGameOptHalService; template<typename T> using Return = hardware::Return<T>; ANDROID_SINGLETON_STATIC_INSTANCE(OplusGameOpt); OplusGameOpt::OplusGameOpt() { ALOGI("OplusGameOpt init"); Mutex::Autolock _l(mLock); checkService(); } OplusGameOpt::~OplusGameOpt() { delete mDeathMonitor; mDeathMonitor = nullptr; } bool OplusGameOpt::checkService() { if (mService != nullptr) { return true; } else { std::string aidl_instance = std::string() + IGameOptHalService::descriptor + "/default"; mService = IGameOptHalService::fromBinder(ndk::SpAIBinder(AServiceManager_checkService(aidl_instance.c_str()))); if (mService == nullptr) { ALOGE("unable to get OplusGameOpt service"); } else { ALOGD("succeed in getting OplusGameOpt service"); if (mDeathMonitor == nullptr) { ALOGD("create DeathMonitor()"); mDeathMonitor = new DeathMonitor(); } else { ALOGD("delete old DeathMonitor() and create a new one"); delete mDeathMonitor; mDeathMonitor = nullptr; mDeathMonitor = new DeathMonitor(); } mDeathMonitor->init(mService); return true; } } mService = nullptr; return false; } void OplusGameOpt::handleServiceDied() { Mutex::Autolock _l(mLock); mService = NULL; } void OplusGameOpt::notifySFBufferProduced(int32_t bufferNum, int64_t timeStamp) { Mutex::Autolock _l(mLock); if (!checkService()) { ALOGE("OplusGameOpt notifySFBufferProduced check service failed"); return; } ndk::ScopedAStatus ret = mService->notifySFBufferProduced(bufferNum, timeStamp); if (!ret.isOk()) { ALOGE("OplusGameOpt notifySFBufferProduced failed: %s", ret.getDescription().c_str()); } } void OplusGameOpt::notifyGameInfo(int32_t type, const string& data) { Mutex::Autolock _l(mLock); if (!checkService()) { ALOGE("OplusGameOpt notifyGameInfo check service failed"); return; } ndk::ScopedAStatus ret = mService->notifyGameInfo(type, data); if (!ret.isOk()) { ALOGE("OplusGameOpt notifyGameInfo failed: %s", ret.getDescription().c_str()); } } DeathMonitor::DeathMonitor(): mDeathRecipient(AIBinder_DeathRecipient_new(DeathMonitor::serviceDied)) { } DeathMonitor::~DeathMonitor() { //AIBinder_DeathRecipient_delete(mDeathRecipient.get()); } void DeathMonitor::init(std::shared_ptr<::aidl::vendor::oplus::hardware::gameopt::IGameOptHalService> obj) { mObj = obj; if (mObj == nullptr) { return; } auto status = ::ndk::ScopedAStatus::fromStatus(::AIBinder_linkToDeath( mObj->asBinder().get(), mDeathRecipient.get(), this)); if (!status.isOk()) { ALOGE("AIBinder_linkToDeath failed"); } } void DeathMonitor::serviceDied(void* cookie) { DeathMonitor* monitor = static_cast<DeathMonitor*>(cookie); monitor->serviceDied(); } void DeathMonitor::serviceDied() { if (mObj != nullptr) { auto ret = ::ndk::ScopedAStatus::fromStatus(::AIBinder_unlinkToDeath( mObj->asBinder().get(), mDeathRecipient.get(), this)); if (!ret.isOk()) { ALOGE("AIBinder_unlinkToDeath failed"); } } mObj = nullptr; OplusGameOpt::getInstance().handleServiceDied(); } 解释代码含义
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值