编程知识 cdmana.com

Android log output control

A very simple topic . When I hear someone talking about their own mechanism control log When the output , I think it's necessary to record it . What bothers me recently is , quite a lot Android The basic skills are not known . A lot of people “ The hammer ” The consciousness is very serious , Always use past experience to deal with everything .

influence Android log Properties of the output

Android The log is stored by logd Maintaining a set of ring buffers , This set of buffers includes :

  • main: Used to store most application logs .
  • radio: Used to store communication related logs .
  • events: Used to store Event The event log .
  • system: Used to store data from Android Operating system messages .
  • crash: Used to store crash logs .
  • stats: Used to store system state event logs .
  • security: Used to store security related event logs .
  • kernel: Used to store Linux Kernel log , Its output is affected by several other attributes , See Logcat Read Kernel Log.

Android Each entry in the log contains a priority 、 A module tag to which the log belongs and the actual log message . Log priority represents the level of log output , Its priority is :VERBOSE(V) < DEBUG(D) < INFO(I) < WARNING(W) < ERROR(E) < FATAL(A) < SILENT(S). The log system outputs according to the log level , Output only logs at current level and below . The log level can be controlled by the following four attributes , Its priority is as follows ,

  • persist.log.tag.<MODULE_TAG>: Module log level , Priority is higher than system log level , Permanent storage .
  • log.tag.<MODULE_TAG>: Module log level , Priority is higher than system log level , Temporary storage , It disappears after restart .
  • persist.log.tag: System log level , Permanent storage
  • log.tag: System log level , Temporary storage , It disappears after restart .

persist.log.tag/log.tag Log level of control system , When the module log level is not set ( None is set by default ), All logs are output at this level , among persist.log.tag Priority is greater than log.tag .

persist.log.tag.<MODULE_TAG>/log.tag.<MODULE_TAG> Log level of control module , But it takes precedence over the system log level . When the module log level exists , The log output of the module is only determined by the log level of the module , No reference to system log level . among persist.log.tag.<MODULE_TAG> Priority is greater than log.tag.<MODULE_TAG>.

Through the above four attributes , It can control the log output of system and module flexibly . Usually , Module log level is not set , But it's very useful for development debugging . As long as there are enough logs in the module , You don't need to recompile the module , As long as you set the properties, you can get the debugging information you need . When none of the above four properties are set , The system will use the default log level , Depending on the system version , May be V or I.

How to use isLoggable

isLoggablel yes android.util.Log A method is provided , It can get the log level of the specified module .isLoggablel Provides another way to control log output , You can control the current log output by specifying the log level of the module .isLoggablel Is defined as follows ,

frameworks/base/core/java/android/util/Log.java
    
    /**
     * Checks to see whether or not a log for the specified tag is loggable at the specified level.
     *
     *  The default level of any tag is set to INFO. This means that any level above and including
     *  INFO will be logged. Before you make any calls to a logging method you should check to see
     *  if your tag should be logged. You can change the default level by setting a system property:
     *      'setprop log.tag.<YOUR_LOG_TAG> <LEVEL>'
     *  Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will
     *  turn off all logging for your tag. You can also create a local.prop file that with the
     *  following in it:
     *      'log.tag.<YOUR_LOG_TAG>=<LEVEL>'
     *  and place that in /data/local.prop.
     *
     * @param tag The tag to check.
     * @param level The level to check.
     * @return Whether or not that this is allowed to be logged.
     * @throws IllegalArgumentException is thrown if the tag.length() > 23
     *         for Nougat (7.0) releases (API <= 23) and prior, there is no
     *         tag limit of concern after this API level.
     */
    public static native boolean isLoggable(String tag, int level);

isLoggable Very useful in the module development process , The main use scenarios are ,

  • Control the debugging level of the module according to the log level of the current module , The code flow and log output can be controlled according to the debugging level .
  • Get the log level of the relevant module to control the code flow and log output of the current module .

isLoggable Compare the defined log level with the level to be verified in the parameters . When level When it is greater than or equal to the current log level ,log The output can be logged , The function returns true. Otherwise return to false. A typical application is as follows ,

frameworks/base/core/java/android/bluetooth/BluetoothMapClient.java
    
public final class BluetoothMapClient implements BluetoothProfile {

    private static final String TAG = "BluetoothMapClient";
    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
    private static final boolean VDBG = Log.isLoggable(TAG, Log.VERBOSE);
    ......
    private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
            new IBluetoothStateChangeCallback.Stub() {
                public void onBluetoothStateChange(boolean up) {
                    if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
                    if (!up) {
                        if (VDBG) Log.d(TAG, "Unbinding service...");
    ......

In the above code , When log.tag.BluetoothMapClient=D when ,DBG=true; VDBG=false . At this time DBG The relevance of control log Can output . When log.tag.BluetoothMapClient=V when ,DBG=true; VDBG=true. DEB and VDBG The control of the log Can output .

Native Log output control

stay Android Native in , Usually use ALOGx() Series of functions to output logs . ALOGx() Function and Log.x() Series functions are the same , Also affected by the level of logging . Again , The four attributes that control the log level are for ALOGx() Series functions are equally effective . This is related to Java There is no difference in the log output of the layer .ALOGx() A series of functions include ,

  • ALOGV(...): Output VERBOSE Level of logging .
  • ALOGD(...): Output DEBUG Level of logging .
  • ALOGI(...): Output INFO Level of logging .
  • ALOGW(...): Output WARNING Level of logging .
  • ALOGE(...): Output ERROR Level of logging .

Besides using ALOGx() Out of the output log , You can also use ALOGx_IF() A series of functions to print the log .ALOGx_IF() The function determines whether to print the log according to the input conditions , Its functions include ,

  • ALOGV_IF(cond, ...)cond by true Time output VERBOSE Level of logging .
  • ALOGD_IF(cond, ...)cond by true Time output DEBUG Level of logging .
  • ALOGI_IF(cond, ...)cond by true Time output INFO Level of logging .
  • ALOGW_IF(cond, ...)cond by true Time output WARNING Level of logging .
  • ALOGE_IF(cond, ...)cond by true Time output ERROR Level of logging .

Besides ,Native One more IF_ALOGx() Series of functions , Can play a role with isLoggable Similar functions , It is used to determine whether the requirement level is greater than the current log level , So you can control the log output .IF_ALOGx() A series of functions include ,

  • IF_ALOGV(): When the log level is less than or equal to VERBOSE when , return true.
  • IF_ALOGD(): When the log level is less than or equal to DEBUG when , return true.
  • IF_ALOGI(): When the log level is less than or equal to INFO when , return true.
  • IF_ALOGW(): When the log level is less than or equal to WARNING when , return true.
  • IF_ALOGE(): When the log level is less than or equal to ERROR when , return true.

Last

The specific code doesn't matter ,Android There is too much code involved in the log , Not a little bit , I don't understand a lot of them . In the development process can do a reasonable use of the log system can give us a lot of help . Most of the time , We don't need to be very clear about the implementation of the logging system , Just make full use of the log system . Remember to get rid of “ The hammer ” thinking .

“ People with hammers in their hands , Think of everything in the world as a nail .” —— Charlie · Munger

版权声明
本文为[Gobi old king]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224152603263f.html

Scroll to Top