How are logcat messages saved
Asked Answered
M

1

9

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?

Maggy answered 28/5, 2013 at 7:50 Comment(0)
F
11

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);
Fosque answered 29/5, 2013 at 9:25 Comment(5)
Regarding 4, I would do this in init.rc, as a script started at boot.Maggy
Yes, I guess it can work. like this: service goldfish-logcat /system/bin/logcat >> yourfileonsdcard oneshot it is a script for emulator.Fosque
@Fosque Thanks for a great overview. I've been looking for this in the Android documentation, and couldn't find it!Smedley
@buptcoder..i am not able to see /dev/log/ in my phone samsung galaxy s5 :( can u tell me how/where to find this?Decolonize
Hi vinu, if your phone is not rooted, you should not have the Permission to assess the dev folder.Fosque

© 2022 - 2024 — McMap. All rights reserved.