Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are logcat messages saved

Following Where are Android logcat files stored? I learn that logcat is saved as an internal ring buffer in the kernel, which is sized 256kb. Applications use a special API to ask the kernel to save logs.

My device logs are wayyyyyy larger. I know this because when I adb logcat > adblogcat.txt I get a really large file. This implies 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 am looking into https://github.com/cgjones/android-system-core/blob/master/logcat/logcat.cpp and I cannot understand the exact details.

Bonus points to someone who explains what happens on reboot, is the log saved between reboots?

like image 658
elcuco Avatar asked May 28 '13 07:05

elcuco


1 Answers

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

  1. About log file in the phone: From the frameworks/base/core/jni/android_util_Log.cpp we can see all log will be written into /dev/log/ folder in your devices at default. main will be written into /dev/log/main radio will be written into /dev/log/radio ... because all are I/O files of operator system. So there will be cleared when the phone is rebooted. And these files size have some limitation, if the size reaches the limitation, the old log will be overridden.

  2. About Logcat: Logcat is a tool to read or re-output the log files. It is installed in the system/bin/ folder in the phone. I don't read the every code of logcat.cpp; but I can say the logcat application will read the log file in system/logcat/main at default and output them in the screen or the file based the parameters you use.

  3. About how the log is generated: If you see the source code of android.util.log, you can find all log.d/log.e will use JNI method to write the log to log files, as mentioned above.

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

    You can find the JNI method here. If you are interested it, you can read the source code to find what it is doing there.

  4. Save the logcat to specific file you want: Based on the Log I/O, developer can write an application to save the log into a file in SD card in the phone so that we can keep the more logs or bigger log file. The most simplest method is use the Runtime.exec() to execute the logcat application: You can monitor the BOOT completed receiver to start your logcat command to read all logcat's log into your own files.

For example:

 String cmd = "logcat -v time -f yourown.file"
 Process process = Runtime.getRuntime().exec(cmd);
like image 149
buptcoder Avatar answered Nov 04 '22 19:11

buptcoder