Log targeting in Android

Source: Internet
Author: User
Http://blog.csdn.net/knock/article/details/5511255
For debugging, you must understand how to print logs, so the following analysis is available.

We usually insert statements such as logd (..) and loge (..) in the program. But under what circumstances can we view these printed messages?
First, go to the definition: System/CORE/include/cutils/log. h.
# Ifndef log_tag
# Define log_tag null
# Endif
Therefore, log_tag must be defined before # include "log. H" in the program. Otherwise, it will be empty.
Let's look at the definition of logd.
# Ifndef logd
# Define logd (...) (void) log (log_debug, log_tag, _ va_args __))
# Endif
Follow up
# Ifndef log
# Define log (priority, Tag ,...)/
Log_pri (Android _ # priority, Tag, _ va_args __)
# Endif
Continue
# Ifndef log_pri
# Define log_pri (priority, Tag ,...)/
Android_printlog (priority, Tag, _ va_args __)
# Endif
Follow up again
# Define android_printlog (PRIO, Tag, FMT ...)/
_ Android_log_print (PRIO, Tag, FMT)

_ Android_log_print () is located in system/CORE/liblog/logd_write.c
Int _ android_log_print (INT Prio, const char * tag, const char * FMT ,...)
{
Va_list AP;
Char Buf [log_buf_size];

Va_start (AP, FMT );
Vsnprintf (BUF, log_buf_size, FMT, AP );
Va_end (AP );

Return _ android_log_write (PRIO, Tag, Buf );
}
View _ android_log_write ()
Int _ android_log_write (INT Prio, const char * tag, const char * MSG)
{
......

Return write_to_log (log_id, VEC, 3 );
}
Write_to_log is defined as follows:
Static int (* write_to_log) (log_id_t, struct iovec * VEC, size_t nr) =
_ Write_to_log_init;
View
Static int _ write_to_log_init (log_id_t log_id, struct iovec * VEC, size_t nr)
{
# Ifdef have_pthreads
Pthread_mutex_lock (& log_init_lock );
# Endif

If (write_to_log = _ write_to_log_init ){
Log_fds [log_id_main] = log_open ("/dev/" logger_log_main, o_wronly );
Log_fds [log_id_radio] = log_open ("/dev/" logger_log_radio, o_wronly );
Log_fds [log_id_events] = log_open ("/dev/" logger_log_events, o_wronly );

Write_to_log = _ write_to_log_kernel;

If (log_fds [log_id_main] <0 | log_fds [log_id_radio] <0 |
Log_fds [log_id_events] <0 ){
Log_close (log_fds [log_id_main]);
Log_close (log_fds [log_id_radio]);
Log_close (log_fds [log_id_events]);
Log_fds [log_id_main] =-1;
Log_fds [log_id_radio] =-1;
Log_fds [log_id_events] =-1;
Write_to_log = _ write_to_log_null;
}
}

# Ifdef have_pthreads
Pthread_mutex_unlock (& log_init_lock );
# Endif

Return write_to_log (log_id, VEC, NR );
}
This section mainly means to enable/dev/log/Main,/dev/log/Radio,/dev/log/events

Write_to_log points to _ write_to_log_kernel. Otherwise, it points to _ write_to_log_null.
Let's take a look at the two
Static int _ write_to_log_null (log_id_t log_fd, struct iovec * VEC, size_t nr)
{
Return-1;
}

Static int _ write_to_log_kernel (log_id_t log_id, struct iovec * VEC, size_t nr)
{
Ssize_t ret;
Int log_fd;

If (/* (INT) log_id> = 0 & */(INT) log_id <(INT) log_id_max ){
Log_fd = log_fds [(INT) log_id];
} Else {
Return ebadf;
}

Do {
Ret = log_writev (log_fd, VEC, NR );
} While (Ret <0 & errno = eintr );

Return ret;
}
_ Write_to_log_null () indicates that log information is discarded. _ Write_to_log_kernel will call log_writev ()

Write the log into the corresponding device (/dev/log /*).

Why can't programs written into init. RC run by init output logs? Let's explore it again ..
In System/CORE/init. C,
Void service_start (struct Service * SVC) function to start the service.
If (needs_console ){
Setsid ();
Open_console ();
} Else {
Zap_stdio ();
}
The two functions are:
Static void zap_stdio (void)
{
Int FD;
FD = open ("/dev/null", o_rdwr );
Dup2 (FD, 0 );
Dup2 (FD, 1 );
Dup2 (FD, 2 );
Close (FD );
}

Static void open_console ()
{
Int FD;
If (FD = open (console_name, o_rdwr) <0 ){
FD = open ("/dev/null", o_rdwr );
}
Dup2 (FD, 0 );
Dup2 (FD, 1 );
Dup2 (FD, 2 );
Close (FD );
}
Zap_stdio () is harsh. stdin, stdout, and stderr are killed directly, while open_console () is only in/dev/console.

Otherwise, stdin, stdout, and stderr will be killed. If/dev/console exists, all input and output will be redirected to it.

.
Depending on needs_console,
Needs_console = (SVC-> flags & svc_console )? 1: 0;
The svc_console part of SVC-> flags comes from system/CORE/init/parser. C.
Static void parse_line_service (struct parse_state * state, int nargs, char ** ARGs)
{
Case k_console:
SVC-> flags | = svc_console;
Break;

}
This means that if the service part of init. RC has a request console, it can be printed to the console.

But how can I print it to the system log? Can I use logcat to view it? This requires logwrapper.
In System/CORE/logwrapper. C, logwrapper first opens/dev/ptmx and queries the device name
Fork () A sub-process and directs stdout and stderr to the queried device.
// Redirect stdout and stderr
Close (parent_ptty );
Dup2 (child_ptty, 1 );
Dup2 (child_ptty, 2 );
Close (child_ptty );
Then start executing the program to run
Child (argc-1, & argv [1]);

Summary:
The output log in the program in the system is generally to the three devices under/dev/log/. You can use logcat to view the output log.
There are two ways to view the log information for the program running init:
1. Add/system/bin/logwrapper, which can be viewed using logcat. For example:
Service/system/bin/logwrapper/system/bin/rild
2. Add a console and output it directly to the console like sh
Service Console/system/bin/sh
Console

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.