3.NDK Android jni開發 C語言中打印log debug模式下 (相機圖片美化)

日誌打印:

對於很多Android開發人員來說,Android的標準日誌打印已經使用的非常習慣,如果在調試C/C++時使用的是printf或者cout這種方式打印日誌,那查找起來會非常不舒服,爲了使自己舒服起來,還是有必要使用android標準的日誌方式打印日誌的。先看一下效果:

 

 

引入

導入log頭文件

在你使用的 .c/ .cpp 文件中

導入 log.h 頭文件

 #include<android/log.h>

 

#include <android/log.h>

#define LOG_TAG    "MyDemo"
#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOGD(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)

使用

LOGE("myName : %s", name); // Log.e(TAG,"myName : $name")
LOGD("age: %d",age); // Log.d(TAG,"age : $age")

 

在Android.mk裏面配置

LOCAL_LDLIBS :=-llog

 

%s代表的含義

%d代表的含義

#include <iostream>
#include <vector>
#include <jni.h>
#include "com_yuedong_sport_health_jni_HeartRate.h"
#include "HeartRate.h"
#include "ImageProcess.h"

#include <android/log.h>

#define TAG "Yd-jni" // 這個是自定義的LOG的標識
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__) // 定義LOGD類型


JNIEXPORT jfloat JNICALL Java_com_yuedong_sport_health_jni_HeartRate_getHeartRate
        (JNIEnv *env, jclass jz, jfloatArray floatArray, jint arrayLength, jint flag) {
    float array[arrayLength];
    env->GetFloatArrayRegion(floatArray, 0, arrayLength, array);
    vector<float> in_vector;
    for (int index = 0; index < arrayLength; ++index) {
        in_vector.push_back(array[index]);
    }
    return (jfloat)to_beat(in_vector, flag);
}

JNIEXPORT jdouble JNICALL Java_com_yuedong_sport_health_jni_HeartRate_decodeYUV420
        (JNIEnv *env, jclass jz, jshortArray shortArray, jint arrayLength, jint width, jint height,
         jint type) {

    LOGD("########## i = %s", "Java_com_yuedong_sport_health_jni_HeartRate_decodeYUV420--start");
    short array[arrayLength];
    LOGD("########## i = %s", "Java_com_yuedong_sport_health_jni_HeartRate_decodeYUV420--short array[arrayLength]");
    env->GetShortArrayRegion(shortArray, 0, arrayLength, array);
     LOGD("########## i = %s", "Java_com_yuedong_sport_health_jni_HeartRate_decodeYUV420");
    return (jdouble)decodeYUV420SPtoRedBlueGreenAvg(array, width, height, type);
}

 

如果是:cmake的話

demo如下:

 

 

 

 

 

設置debug模式的打印?

方法:在c++中,如何定義全局變量?

或者數據存儲

原理:封裝成一個公共的類,在方法裏面控制。其他地方都可以調用這個類和類的方法。

https://blog.csdn.net/weixin_33709609/article/details/92065843

    #include <android/log.h>
            #ifndef LOG_H_
#define LOG_H_
    class Log {
        private:
        Log();
        virtual ~Log();
        public:
        static bool IS_DEBUG;
        static int  DEFAULT_MIN_LEVEL;
        static void V(const char * tag,const char * msg);
        static void D(const char * tag,const char * msg);
        static void I(const char * tag,const char * msg);
        static void W(const char * tag,const char * msg);
        static void E(const char * tag,const char * msg);
        static void F(const char * tag,const char * msg);
    };
#endif /* LOG_H_ */
            #include "Log.h"

    Log::Log() {

    }
    Log::~Log() {
        Log::DEFAULT_MIN_LEVEL = ANDROID_LOG_DEBUG;
        Log::IS_DEBUG = false;
    }
    int    Log::DEFAULT_MIN_LEVEL = ANDROID_LOG_DEBUG;
    bool   Log::IS_DEBUG        = true;

 void Log::D(const char * tag,const char *msg)
    {
        if(Log::IS_DEBUG && Log::DEFAULT_MIN_LEVEL<=ANDROID_LOG_DEBUG)
        {
            __android_log_print(ANDROID_LOG_DEBUG,tag,"%s",msg);
        }
    }
 void Log::V(const char * tag,const char *msg)
    {
        if(Log::IS_DEBUG && Log::DEFAULT_MIN_LEVEL<=ANDROID_LOG_VERBOSE)
        {
            __android_log_print(ANDROID_LOG_VERBOSE,tag,"%s",msg);
        }
    }
 void Log::I(const char * tag,const char *msg)
    {
        if(Log::IS_DEBUG && Log::DEFAULT_MIN_LEVEL<=ANDROID_LOG_INFO)
        {
            __android_log_print(ANDROID_LOG_INFO,tag,"%s",msg);
        }
    }
 void Log::E(const char * tag,const char *msg)
    {
        if(Log::IS_DEBUG && Log::DEFAULT_MIN_LEVEL<=ANDROID_LOG_ERROR)
        {
            __android_log_print(ANDROID_LOG_ERROR,tag,"%s",msg);
        }
    }
 void Log::W(const char * tag,const char *msg)
 {
        if(Log::IS_DEBUG && Log::DEFAULT_MIN_LEVEL<=ANDROID_LOG_WARN)
        {
            __android_log_print(ANDROID_LOG_WARN,tag,"%s",msg);
        }
 }

 void Log::F(const char * tag,const char *msg)
    {
        if(Log::IS_DEBUG && Log::DEFAULT_MIN_LEVEL<=ANDROID_LOG_FATAL)
        {
            __android_log_print(ANDROID_LOG_FATAL,tag,"%s",msg);
        }
    }

 

第一個參數爲打印級別,爲以下枚舉之一:

typedef enum android_LogPriority {
    ANDROID_LOG_UNKNOWN = 0,
    ANDROID_LOG_DEFAULT,    /* only for SetMinPriority() */
    ANDROID_LOG_VERBOSE,
    ANDROID_LOG_DEBUG,
    ANDROID_LOG_INFO,
    ANDROID_LOG_WARN,
    ANDROID_LOG_ERROR,
    ANDROID_LOG_FATAL,
    ANDROID_LOG_SILENT,     /* only for SetMinPriority(); must be last */
} android_L

 

 

https://blog.csdn.net/wolinxuebin/article/details/80181672

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章