在程序开发过程中,LOG是广泛使用的用来记录程序执行过程的机制,它既可以用于程序调试,也可以用于产品运营中的事件记录。在Android系统中,提供了简单、便利的LOG机制,开发人员可以方便地使用。在这一篇文章中,我们简单介绍在Android内核空间和用户空间中LOG的使用和查看方法。
- #define
KERN_EMERG "<0>" - #define
KERN_ALERT "<1>" - #define
KERN_CRIT "<2>" - #deinfe
KERN_ERR "<3>" - #deinfe
KERN_WARNING "<4>" - #deinfe
KERN_NOTICE "<5>" - #deinfe
KERN_INFO "<6>" - #deinfe
KERN_DEBUG "<7>"
-
- typedef
enum android_LogPriority { -
ANDROID_LOG_UNKNOWN = 0, -
ANDROID_LOG_DEFAULT, -
ANDROID_LOG_VERBOSE, -
ANDROID_LOG_DEBUG, -
ANDROID_LOG_INFO, -
ANDROID_LOG_WARN, -
ANDROID_LOG_ERROR, -
ANDROID_LOG_FATAL, -
ANDROID_LOG_SILENT, - }
android_LogPriority;
-
- #ifndef
LOG_TAG - #define
LOG_TAG NULL - #endif
-
-
- #ifndef
LOGV - #if
LOG_NDEBUG - #define
LOGV(...) ((void)0) - #else
- #define
LOGV(...) ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) - #endif
- #endif
-
-
- #ifndef
LOG - #define
LOG(priority, tag, ...) \ -
LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__) - #endif
-
-
- #ifndef
LOG_PRI - #define
LOG_PRI(priority, tag, ...) \ -
android_printLog(priority, tag, __VA_ARGS__) - #endif
-
-
- #define
android_printLog(prio, tag, fmt...) \ -
__android_log_print(prio, tag, fmt)
- ................................................
-
- public
final class Log { -
- ................................................
-
-
-
public static final int VERBOSE = 2; -
-
-
public static final int DEBUG = 3; -
-
-
public static final int INFO = 4; -
-
-
public static final int WARN = 5; -
-
-
public static final int ERROR = 6; -
-
-
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)); -
} -
- ..................................................................
-
-
public static native int println_native(int bufID, -
int priority, String tag, String msg); - }