Android ndk开发之在c文件里打印log

There are three step in total

step one:

add  "LOCAL_LDLIBS := -llog " to your android.mk file

like:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE    := hellolog
LOCAL_SRC_FILES := native.c
include $(BUILD_SHARED_LIBRARY)


step two:

include <android/log.h> as header in you c file, and define LOG_TAG、LOGE(...)like:

#include <android/log.h>
#define LOG_TAG "plus.c"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)


you can also define a yourlog.h file, define all kinds of log, then include this into your c file, like:

=====mylog.h=======

#include <jni.h>
#include <android/log.h>

#define LOG_TAG "hgllog.h"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)

step three:

use yourself define LOGE() print info as you want, like:

LOGE("this is log info string....");

LOGE("this is log info string.... and str is %s", str);


你可能感兴趣的:(android,log,NDK)