分享

在非NDK编译条件下使用Android Log函数

 小仙女本仙人 2021-12-30

解决的需求

有些时候不能在NDK环境编译,或者使用NDK编译会颇费周折,然后又想使用Android系统自带的Log类方法,那么我们就可以使用dlopen来实现我们的目的。比如在OpenCV中添加Android的Log打印。

关于dlopen

  1. dlopen和dlclose对处理的lib进行引用计数,dlopen使引用计数加1,dlclose让引用计数减1,当对库的引用计数达到0的时候,才会把库unmap掉。
  2. dlsym返回查找到的第一个符合要求的函数地址,该地址指的是库加载进进程虚拟地址。
  3. 可以使用dlsym来实现对库的前一个函数的隐藏。There are two special pseudo-handles, RTLD_DEFAULT and RTLD_NEXT. The former will find the first occurrence of the desired symbol using the default library search order. The latter will find the next occurrence of a function in the search order after the current library. This allows one to provide a wrapper around a function in another shared library.
  4. 在调用了dlclose之后,再使用dlsym得到的函数是不行的。虽然当使用dlclose使得引用计数为0之后,系统并不会立马把加载的库给unmap掉,但是这个时间是不确定的。也许在你调用dlclose之后,在系统unmap之前调用dlsym的函数,或者本次dlclose并没有让引用计数为0,比如进程中还有其他地方也dlopen同一个库,那么系统也不会unmap,这样调用dlsym的函数并不会出错。
  5. 如果你想dlopen一个库之后,需要在程序中一直使用这个库的函数,那么没有必要调用dlclose,包括在程序退出之前调用dlclose。因为程序退出会自动unmap这些库。但是如果你要dlopen很多库,可能会导致虚拟地址不足的情况,那么就需要调用dlclose以保证不会出错。https:///questions/26131102/is-it-safe-to-call-dlclose-after-dlsym

代码

使用如下代码,实现基于dlopen的Android Log方法调用

#include <dlfcn.h>
#include <stdarg.h>
#define DLLOG_TAG  "Slender3d"


static void logd( const char* format, ...)
{
    
#ifdef __aarch64__
    const char libpath[] = "/system/lib64/liblog.so";
#else
    const char libpath[] = "/system/lib/liblog.so";
#endif

    const int ANDROID_LOG_DEBUG = 3;

    using __android_log_printFunc = int(*)(int , const char* , const char* , ...);
    static __android_log_printFunc slogFunc = NULL;
    if(NULL == slogFunc){
        void *handler = dlopen(libpath, RTLD_NOW | RTLD_LOCAL);
		dlerror();
        if (handler) {
            const char *SYM = "__android_log_print";
            slogFunc = reinterpret_cast<__android_log_printFunc>(dlsym(handler, SYM));
			char *error;
			if( (error = dlerror() != NULL){
			slogFunc = NULL;
				//LOGE("dlsym %s fail", libpath);
			}
        }
        else {
            //LOGE("dlopen %s fail", libpath);
        }
    }

    if (slogFunc) {
        va_list args;
        va_start(args, format);
        slogFunc(ANDROID_LOG_DEBUG, DLLOG_TAG, format, args);
        va_end(args);
    }
    else {
            //LOGE("dlsym %s fail", SYM);
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多