How logcat messages are saved

After Where are the logcat files for Android stored? I found out that logcat is stored as an internal ring buffer in the 256 kbyte kernel. Applications use a special API to ask the kernel to save logs.

My device logs are wayyyyyyy more. I know this because when I adb logcat > adblogcat.txt , I get a really big file. This means that “something” clears the kernel buffers and stores them in the file system (?) And adb logcat reads from this larger file.

Can anyone explain how this works? I look at https://github.com/cgjones/android-system-core/blob/master/logcat/logcat.cpp and I can not understand the exact details.

The bonus indicates who explains what happens during a reboot, is the log saved between reboots?

+7
source share
1 answer

Here is my understanding about logcat, maybe there are some errors, welcome any comments:

  • About the log file in the phone: From frameworks/base/core/jni/android_util_Log.cpp we will see that all entries will be written to the /dev/log/ folder on your devices by default. main will be written to /dev/log/main radio will be written to /dev/log/radio ... because all the input / output files of the operator system. Thus, when you restart the phone, it will be cleared. And the size of these files has some limitation, if the size reaches the limit, the old log will be redefined.

  • About Logcat: Logcat is a tool for reading or retrieving log files. It is installed in the system / bin / folder on the phone. I do not read every logcat.cpp code; but I can say that the logcat application will read the log file in system / logcat / main by default and display them on the screen or file based on the parameters you use.

  • About how the log is created: If you see the source code for android.util.log , you can find all log.d / log.e to use the JNI method to write log to log files as above.

    public static native int println_native(int bufID,int priority, String tag, String msg);

    Here you can find the JNI method here . If this interests you, you can read the source code to find out what it does there.

  • Save the log code in the desired file: Based on the I / O of the log, the developer can write an application to save the log to a file on the SD card in the phone so that we can store more logs or a larger log file. The easiest way is to use Runtime.exec() to execute the logcat application: You can follow the completed BOOT receiver to run the logcat command to read the entire logcat log into its own files.

For example:

  String cmd = "logcat -v time -f yourown.file" Process process = Runtime.getRuntime().exec(cmd); 
+9
source

All Articles