在Java核心库中打印log
android 在Java核心库libcore中打印log
Follow below steps to add log utility function in libcore/luni/src/main/java/java/text/SimpleDateFormat.java
Android version: 4.0.4
Affected files:
- libcore\luni\src\main\java\java\text\SimpleDateFormat.java
- libcore\luni\src\main\native\Register.cpp
- libcore\luni\src\main\native\sub.mk
- libcore\luni\src\main\native\java_text_SimpleDateFormat.cpp
[Step 1]
Create libcore\luni\src\main\native\java_text_SimpleDateFormat.cpp
#define LOG_TAG "SimpleDateFormat"
#include "JNIHelp.h"
#include "JniConstants.h"
#include "JniException.h"
#include "ScopedPrimitiveArray.h"
#include "UniquePtr.h"
#include <stdlib.h>
#include <string.h>
static jint SimpleDateFormat_println_native(JNIEnv* env, jclass,
jint bufID, jint priority, jstring tagObj, jstring msgObj)
{
const char* tag = NULL;
const char* msg = NULL;
if (msgObj == NULL) {
jniThrowNullPointerException(env, "println needs a message");
return -1;
}
if (bufID < 0 || bufID >= LOG_ID_MAX) {
jniThrowNullPointerException(env, "bad bufID");
return -1;
}
if (tagObj != NULL)
tag = env->GetStringUTFChars(tagObj, NULL);
msg = env->GetStringUTFChars(msgObj, NULL);
int res = __android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);
if (tag != NULL)
env->ReleaseStringUTFChars(tagObj, tag);
env->ReleaseStringUTFChars(msgObj, msg);
return res;
}
static JNINativeMethod gMethods[] = {
NATIVE_METHOD(SimpleDateFormat, println_native, "(IILjava/lang/String;Ljava/lang/String;)I"),
};
int register_java_text_SimpleDateFormat(JNIEnv* env) {
return jniRegisterNativeMethods(env, "java/text/SimpleDateFormat", gMethods, NELEM(gMethods));
}
[Step 2]
Add java_text_SimpleDateFormat.cpp into make file libcore\luni\src\main\native\sub.mk
LOCAL_SRC_FILES := \
AsynchronousSocketCloseMonitor.cpp \
JniConstants.cpp \
JniException.cpp \
NetworkUtilities.cpp \
Register.cpp \
cbigint.cpp \
java_io_Console.cpp \
java_io_File.cpp \
java_io_ObjectStreamClass.cpp \
java_lang_Character.cpp \
java_lang_Math.cpp \
java_lang_ProcessManager.cpp \
java_lang_RealToString.cpp \
java_lang_StrictMath.cpp \
java_lang_StringToReal.cpp \
java_lang_System.cpp \
java_math_NativeBN.cpp \
java_nio_ByteOrder.cpp \
java_nio_charset_Charsets.cpp \
java_text_Bidi.cpp \
java_text_SimpleDateFormat.cpp \
java_util_regex_Matcher.cpp \
java_util_regex_Pattern.cpp \
java_util_zip_Adler32.cpp \
java_util_zip_CRC32.cpp \
java_util_zip_Deflater.cpp \
java_util_zip_Inflater.cpp \
libcore_icu_ICU.cpp \
libcore_icu_NativeBreakIterator.cpp \
libcore_icu_NativeCollation.cpp \
libcore_icu_NativeConverter.cpp \
libcore_icu_NativeDecimalFormat.cpp \
libcore_icu_NativeIDN.cpp \
libcore_icu_NativeNormalizer.cpp \
libcore_icu_NativePluralRules.cpp \
libcore_icu_TimeZones.cpp \
libcore_io_AsynchronousCloseMonitor.cpp \
libcore_io_Memory.cpp \
libcore_io_OsConstants.cpp \
libcore_io_Posix.cpp \
libcore_net_RawSocket.cpp \
org_apache_harmony_xml_ExpatParser.cpp \
org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp \
readlink.cpp \
realpath.cpp \
toStringArray.cpp \
valueOf.cpp
[Step 3]
Register java_text_SimpleDateFormat(env) in libcore\luni\src\main\native\Register.cpp
extern int register_java_nio_ByteOrder(JNIEnv* env);
extern int register_java_nio_charset_Charsets(JNIEnv* env);
extern int register_java_text_Bidi(JNIEnv* env);
extern int register_java_text_SimpleDateFormat(JNIEnv* env);
extern int register_java_util_regex_Matcher(JNIEnv* env);
extern int register_java_util_regex_Pattern(JNIEnv* env);
extern int register_java_util_zip_Adler32(JNIEnv* env);
register_java_nio_ByteOrder(env) != -1 &&
register_java_nio_charset_Charsets(env) != -1 &&
register_java_text_Bidi(env) != -1 &&
register_java_text_SimpleDateFormat(env) != -1 &&
register_java_util_regex_Matcher(env) != -1 &&
register_java_util_regex_Pattern(env) != -1 &&
register_java_util_zip_Adler32(env) != -1 &&
[Step 4]
Declare native function in libcore\luni\src\main\java\java\text\SimpleDateFormat.java
private static native int println_native(int bufID, int priority, String tag, String msg);
private static final String TAG = "SimpleDateFormat";
private static final int LOG_ID_MAIN = 0;
private static final int DEBUG = 3;
private static int Log(String tag, String msg) {
return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
}
[Step 5]
Finally, we can use Log(TAG, msg) in SimpleDataFormat.java!
Log("SimpleDateFormat", "This is debug message");