[轉載請註明出處] http://kezeodsnx.pixnet.net/blog

作者: kezeodsnx

說明

先把emulator跑起來後,在console下adb logcat後,就會有一堆message跑出來了,如

I/SystemServer(  568): Starting Battery Service.
E/BatteryService(  568): Could not open '/sys/class/power_supply/usb/online'

其中第一個欄位是priority:

  V    Verbose
  D    Debug
  I    Info
  W    Warn
  E    Error
  F    Fatal
  S    Silent

第二個欄位是tag,例如Dalvikm,qmenud,BatteryService,也可自訂,主要是用來辨認是何種訊息。括號內的是pid,後面就是訊息內容了。這大概是log的格式。

In JAVA

在JAVA中,logcat的class是在android.util.log裡,因此若要使用,需要在header加上import android.util.Log。程式的使用如下:

Log.v(): VERBOSE
Log.d(): DEBUG
Log.i() : INFO
Log.w(): WARN
Log.e(): ERROR

第一個欄位是tag,第二個欄位是訊息,因此可以這樣用:

Log.i("OnCreate", "#########Test in java");

在logcat可以看到:

I/OnCreate(  712): #########Test in java

In JNI

在JNI中,可以使用<android/log.h>所定義的macro:

int __android_log_print(int prio, const char *tag,  const char *fmt, ...)
#if defined(__GNUC__)
    __attribute__ ((format(printf, 3, 4)))
#endif
    ;

參考android討論區所提到的這篇文章,可定義如下的macro以方便使用:
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG  , "libnav",__VA_ARGS__)

就可以在.c檔裡輸出到logcat,方法如下:

jint
Java_com_latrell_libtest_libtest_add( JNIEnv* env, jobject thiz, jint x, jint y)
{
        LOGD("#############test log in JNI, x is %d, y is %d\n",x,y);
    return add(x,y);
}

從logcat就可看到,也是一種debug方式:

D/libnav  (  716): #############test log in JNI, x is 3, y is 5

在JNI裡使用logcat,要在Android.mk裡將liblog link進來,因此要加上:

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog

將ndk裡的hello-jni改寫了一下,程式碼如下:

hello-jni.c

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

#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "libnav",__VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG  , "libnav",__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO   , "libnav",__VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN   , "libnav",__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR  , "libnav",__VA_ARGS__)
int add(int x, int y)
{
        return x+y;
}
jint
Java_com_latrell_libtest_libtest_add( JNIEnv* env, jobject thiz, jint x, jint y)
{
        LOGD("#############test log in JNI, x is %d, y is %d\n",x,y);
    return add(x,y);
}


Android.mk:

LOCAL_PATH := $(call my-dir)
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog

include $(CLEAR_VARS)

LOCAL_MODULE    := latrell-libtest
LOCAL_SRC_FILES := hello-jni.c

include $(BUILD_SHARED_LIBRARY)

libtest.java:

package com.latrell.libtest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class libtest extends Activity {
    /** Called when the activity is first created. */
    int ret=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView  tv = new TextView(this);
            int a=3;
            int b=5;
            ret=add(a,b);
            tv.setText(Integer.toString(ret));
            Log.i("Create Android", "################Test in java");
            setContentView(tv);
    }
 public native int  add(int x, int y);
    static {
        System.loadLibrary("latrell-libtest");
    }

}

issue:

1. 用eclipse時,在hello-jni.c裡改變log的內容,重新make APP後,用eclipse上傳到emulator後,沒辦法將改變apply?後來將整個project移除再import一次才解決,不知有沒有人知道該如何解決?

2. 試著調用如strcmp,strlen的function,但還無法將字串從java傳到c (傳integer沒問題),這可能是java寫錯了,還在研究中。

傳字串是要用特定方法的:

int stringlen(char* str)
{
        LOGD("#############test log in native c function, str is %s\n",str);
        return strlen(str);
}

Java_com_latrell_libtest_libtest_stringlen( JNIEnv* env, jobject thiz, jstring str)
{
        char* buf;
        buf = (*env)->GetStringUTFChars(env, str, 0);
        LOGD("#############test log in JNI, str is %s\n",buf);
        return stringlen(buf);
}

 

 

 


arrow
arrow
    全站熱搜

    kezeodsnx 發表在 痞客邦 留言(3) 人氣()