Sam YC
Sam YC

Reputation: 11617

About Android Logcat in real device

I have some question related to the Android Logcat, I am refering these questions to be in REAL DEVICE not the emulator:

  1. I have a program which has a lot of Log.d (or other similar log functions), I did some google search and found that those logs will be output to a circular 64K buffer. My question is, where are those 64K buffer located? Is it located in RAM or file system? Will it be removed after my app exits?

  2. I google and found that, the log will be output to a file called /dev/event (not sure), but I couldn't see any application-related logging inside, why? Only system-related log entries can be seen.

  3. Does every app output its log to a different log file? Or are they all dumped into the same log file? In this case, how can we seperate the log?

  4. If the logging buffer is 64K, how can we increase it? If we want to re-direct the log to a file which is on a sd card, how can we limit the file size (of course make it circular too)?

Upvotes: 4

Views: 2358

Answers (1)

fadden
fadden

Reputation: 52303

Going point by point:

(1) The log buffers are in RAM, in the kernel. Logging is handled by a kernel driver. On recent devices, the log buffer is much larger than 64KB. They are not lost when an application exits, but will not survive a device reboot.

(2) The device entries are in /dev/log, e.g. /dev/log/main for the main log, /dev/log/radio for the radio log, and /dev/log/events for the event log. The logs are stored in a format that is generally more compact than what you see in logcat, so reading them directly with something like cat may be confusing.

(3) All apps write to the same log devices. Each log message is tagged with the process ID and thread ID of the log writer, so you can separate them out that way. (logcat -v threadtime will show you the pid, tid, timestamp, and tag.)

(4) You would need to change a value in the driver to alter the size of the log. Again, recent devices have much larger log buffers, so this is probably not so useful. You can send the output to a file with logcat; if you want to do some sort of circular buffer you will need to capture the output of logcat and handle it yourself. (You could also read the log device directly, but I don't think the API is public. See the AOSP logcat sources.)

Recent versions of Android do not allow apps to read all entries in the log. Instead, apps may only read the entries that they themselves generated. This was done to allow bug reporting systems to capture the logs, while preventing wayward apps from "spying" on other apps. When you run logcat from adb shell, it runs as the shell user, which has permission to read all logs.

Upvotes: 5

Related Questions