Understanding the Logd Daemon
In AOSP, the logd daemon is the centralized logger responsible for managing all system logs. It runs in the background, collecting logs from various components (like the kernel, native C/C++ daemons, and Java apps) and organizing them into specific circular buffers.
Developers interact with logd using the logcat command-line utility.
Log Buffers
Android segregates logs into multiple buffers to prevent high-volume logs (like radio events) from drowning out application or system logs.
main: The default buffer for most application logs.system: Logs generated by the Android framework and core system services.crash: Dedicated buffer for native crashes and Java exceptions.radio: Telephony and radio-related logs.events: Binary-formatted logs used for system metrics and telemetry.
You can specify a buffer using the -b flag:
adb logcat -b crash,system
Log Levels
Logs are categorized by priority. The standard levels, in increasing order of severity, are:
- V (Verbose)
- D (Debug)
- I (Info)
- W (Warning)
- E (Error)
- F (Fatal)
C++ Logging Example
In native AOSP code, logging is typically handled using the ALOG macros defined in system/core/liblog/include/log/log.h.
#define LOG_TAG "MySystemDaemon"
#include <log/log.h>
void process_data() {
ALOGD("Data processing started.");
if (error_occurred) {
ALOGE("Failed to process data: %s", strerror(errno));
}
}
Advanced Filtering
To filter noise, you can specify tags and priority levels. The format is <tag>:<priority>. To silence everything else, append *:S.
# Show only 'ActivityManager' logs at INFO or higher, and silence the rest
adb logcat ActivityManager:I *:S
To filter by a specific Process ID (PID):
adb logcat --pid=1234
Persistent Logging (logpersist)
By default, log buffers are stored in RAM and cleared upon reboot. For debugging issues that cause the device to crash or reboot unexpectedly, you can enable persistent logging.
The logpersist daemon writes logcat buffers to the filesystem (/data/misc/logd/).
To enable persistent logging (requires root):
adb root
adb shell setprop persist.logd.logpersistd logcatd
# Optional: set max storage size (e.g., 50MB)
adb shell setprop persist.logd.size 50
adb reboot