[Android Software/Hacking General [Developers Only]] llrraa2010:
log in android

1. kernel log

<linux/kernel.h>

#define KERN_EMERG	"<0>"	/* system is unusable	*/

#define KERN_ALERT	"<1>"	/* action must be taken immediately	*/

#define KERN_CRIT	"<2>"	/* critical conditions	*/

#deinfe KERN_ERR	"<3>"	/* error conditions	*/

#deinfe KERN_WARNING	"<4>"	/* warning conditions	*/

#deinfe KERN_NOTICE	"<5>"	/* normal but significant condition	*/

#deinfe KERN_INFO	"<6>"	/* informational	*/

#deinfe KERN_DEBUG	"<7>"	/* debug-level messages	*/

printk(KERN_ALERT"This is the log printed by printk in linux kernel space.");

to read the log:

USER-NAME@MACHINE-NAME:~/Android$ emulator &

USER-NAME@MACHINE-NAME:~/Android$ adb shell

root@android:/ # cat /proc/kmsg

2. LOG for app

C/C++:

system/core/include/android/log.h

/*

* Android log priority values, in ascending priority order.

*/

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_LogPriority;

system/core/include/cutils/log.h

/*

* This is the local tag used for the following simplified

* logging macros. You can change this preprocessor definition

* before using the other macros to change the tag.

*/

#ifndef LOG_TAG

#define LOG_TAG NULL

#endif

/*

* Simplified macro to send a verbose log message using the current LOG_TAG.

*/

#ifndef LOGV

#if LOG_NDEBUG

#define LOGV(...) ((void)0)

#else

#define LOGV(...) ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))

#endif

#endif

/*

* Basic log message macro.

*

* Example:

* LOG(LOG_WARN, NULL, "Failed with error %d", errno);

*

* The second argument may be NULL or "" to indicate the "global" tag.

*/

#ifndef LOG

#define LOG(priority, tag, ...) \

LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)

#endif

/*

* Log macro that allows you to specify a number for priority.

*/

#ifndef LOG_PRI

#define LOG_PRI(priority, tag, ...) \

android_printLog(priority, tag, __VA_ARGS__)

#endif

/*

* ================================================================

*

* The stuff in the rest of this file should not be used directly.

*/

#define android_printLog(prio, tag, fmt...) \

__android_log_print(prio, tag, fmt)

to use:

#define LOG_TAG "MY LOG TAG"

#include <cutils/log.h>

LOGV("This is the log printed by LOGV in android user space.");

java:

frameworks/base/core/java/android/util/Log.java

................................................

public final class Log {

................................................

/**

* Priority constant for the println method; use Log.v.

*/

public static final int VERBOSE = 2;

/**

* Priority constant for the println method; use Log.d.

*/

public static final int DEBUG = 3;

/**

* Priority constant for the println method; use Log.i.

*/

public static final int INFO = 4;

/**

* Priority constant for the println method; use Log.w.

*/

public static final int WARN = 5;

/**

* Priority constant for the println method; use Log.e.

*/

public static final int ERROR = 6;

/**

* Priority constant for the println method.

*/

public static final int ASSERT = 7;

.....................................................

public static int v(String tag, String msg) {

return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);

}

public static int v(String tag, String msg, Throwable tr) {

return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(tr));

}

public static int d(String tag, String msg) {

return println_native(LOG_ID_MAIN, DEBUG, tag, msg);

}

public static int d(String tag, String msg, Throwable tr) {

return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr));

}

public static int i(String tag, String msg) {

return println_native(LOG_ID_MAIN, INFO, tag, msg);

}

public static int i(String tag, String msg, Throwable tr) {

return println_native(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr));

}

public static int w(String tag, String msg) {

return println_native(LOG_ID_MAIN, WARN, tag, msg);

}

public static int w(String tag, String msg, Throwable tr) {

return println_native(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(tr));

}

public static int w(String tag, Throwable tr) {

return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));

}

public static int e(String tag, String msg) {

return println_native(LOG_ID_MAIN, ERROR, tag, msg);

}

public static int e(String tag, String msg, Throwable tr) {

return println_native(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr));

}

..................................................................

/**@hide */ public static native int println_native(int bufID,

int priority, String tag, String msg);

}

to use:

private static final String LOG_TAG = "MY_LOG_TAG";

Log.i(LOG_TAG, "This is the log printed by Log.i in android user space.");

read log:

USER-NAME@MACHINE-NAME:~/Android$ emulator &

USER-NAME@MACHINE-NAME:~/Android$ adb shell

root@android:/ # logcat