Android 实现自己的LOG信息
作者:lqh 发布时间:2023-06-08 16:30:30
在程序开发过程中,LOG是广泛使用的用来记录程序执行过程的机制,它既可以用于程序调试,也可以用于产品运营中的事件记录。在Android系统中,提供了简单、便利的LOG机制,开发人员可以方便地使用。在这一篇文章中,我们简单介绍在Android内核空间和用户空间中LOG的使用和查看方法。
一. 内核开发时LOG的使用。Android内核是基于Linux Kerne 2.36的,因此,Linux Kernel的LOG机制同样适合于Android内核,它就是有名的printk,与C语言的printf齐名。与printf类似,printk提供格式化输入功能,同时,它也具有所有LOG机制的特点--提供日志级别过虑功能。printk提供了8种日志级别(<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的使用方法:
printk(KERN_ALERT"This is the log printed by printk in linux kernel space.");
KERN_ALERT表示日志级别,后面紧跟着要格式化字符串。
在Android系统中,printk输出的日志信息保存在/proc/kmsg中,要查看/proc/kmsg的内容,参照Android内核源码 在Ubuntu上下载,编译,安装一文,在后台中运行模拟器:
USER-NAME@MACHINE-NAME:~/Android$ emulator &
启动adb shell工具:
USER-NAME@MACHINE-NAME:~/Android$ adb shell
查看/proc/kmsg文件:
root@android:/ # cat /proc/kmsg
二. 用户空间程序开发时LOG的使用。Android系统在用户空间中提供了轻量级的logger日志系统,它是在内核中实现的一种设备驱动,与用户空间的logcat工具配合使用能够方便地跟踪调试程序。在Android系统中,分别为C/C++ 和Java语言提供两种不同的logger访问接口。C/C++日志接口一般是在编写硬件抽象层模块或者编写JNI方法时使用,而Java接口一般是在应用层编写APP时使用。
Android系统中的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中,定义了对应的宏,如对应于ANDROID_LOG_VERBOSE的宏LOGV:
/*
* 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)
因此,如果要使用C/C++日志接口,只要定义自己的LOG_TAG宏和包含头文件system/core/include/cutils/log.h就可以了:
#define LOG_TAG "MY LOG TAG"
#include <cutils/log.h>
就可以了,例如使用LOGV:
LOGV("This is the log printed by LOGV in android user space.");
再来看Android系统中的Java日志接口。Android系统在Frameworks层中定义了Log接口:
(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));
}
..................................................................
因此,如果要使用Java日志接口,只要在类中定义的LOG_TAG常量和引用android.util.Log就可以了:
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.");
要查看这些LOG的输出,可以配合logcat工具。如果是在Eclipse环境下运行模拟器,并且安装了Android插件,那么,很简单,直接在Eclipse就可以查看了:
如果是在自己编译的Android源代码工程中使用,则在后台中运行模拟器:
USER-NAME@MACHINE-NAME:~/Android$ emulator &
启动adb shell工具:
USER-NAME@MACHINE-NAME:~/Android$ adb shell
使用logcat命令查看日志:
root@android:/ # logcat
这样就可以看到输出的日志了。


猜你喜欢
- 背景:以前学的Java进行开发,多用到Mybatis,Hiberante等ORM框架,最近需要上手一个C#的项目,由于不是特别难,也不想再去
- 使用mybatis-plus自动生成了5个模块(xml/bean/mapper/service/controller)的代码,这里练习一下m
- 因为涉及到 io 流输入问题,如果不关闭会一直占用资源。所以使用过后要及时关闭,防止资源一直被占用。Scanner在使用前要导入 java.
- 01 高效设置我们先来讲讲有哪些设置调整之后 IDEA 会更好用。先说明一点:这里只是根据我个人喜好和习惯的建议,IDEA 的默认配置已经很
- 一. struts2读取进度原理分析(作为草稿存了好久,刚刚发布出来......)1.在strut2中控制文件上传信息的类是实现MultiP
- Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spr
- 一、枚举类类的对象只有有限个, 确定的. 我们称此类为枚举类.说明:1.类的对象只有有限个,确定的。如:星期:Monday(星期一)、…、S
- 数字可以标志货币、百分比、积分和电话号码等,就货币而言,在不同的国家会以不同的格式来定义,本实例将接收用户输入的数字,然后在控制台中输出其货
- 本文实例讲述了Android编程之蓝牙测试。分享给大家供大家参考。具体分析如下:一、软件平台:win7 + eclipse + sdk二、设
- 问题为了避免空指针调用,我们经常会看到这样的语句:if (someobject != null) { someob
- 本文实例为大家分享了Flutter实现顶部导航栏的具体代码,供大家参考,具体内容如下import 'package:flutter/
- 一、前言1.1 实现目标服务A调用服务B1和B2(B1和B2提供同种服务),当服务B1/B2在停止和重新发布阶段,或B1/B2有一个服务故障
- Java中为什么需要Callable在java中有两种创建线程的方法:一种是继承Thread类,重写run方法:public class T
- 相信大家肯定都在电商网站买过东西,当我们看中一件喜欢又想买的东西时,这时候你又不想这么快结账,这时候你就可以放入购物车;就像我们平时去超市买
- 一、表创建一、表创建//创建一个空表DataTable dt = new DataTable();//创建一个名为"Table_N
- Android 使用FragmentTabhost代替Tabhost前言:现在Fragment使用越来越广了,虽然Fragment寄生在Ac
- 切面编程听起来可能有点陌生,不过现在越来越多的开发团队正在用上这种技术。先说熟悉的面向对象编程 OOP,通常都是用各种对象/模块来负责具体的
- 1、@Valid与@Validated的区别1.1 基本区别@Valid:Hibernate validation校验机制@Validate
- 本文实例讲述了Android桌面插件App Widget用法。分享给大家供大家参考,具体如下:应用程序窗口小部件App Widgets应用程
- 一、什么是MVP在网上找了些资料,整理如下:MVP是模型(Model)、视图(View)、主持人(Presenter)的缩写,分别代表项目中