1. in init.rc service , add console .
example:
service exa1 /system/bin/exa.sh
class main
user root
console
2. in system/core/init/init.c:
if (needs_console) {
setsid();
open_console();
} else {
zap_stdio(); //change zap_stdio() to open_console
}
in c/cpp file , the printf can not send message to console , so to send log to logcat according below:
in *.c or *.cpp or *.h:
#include<android/log.h>
#include<cutils/log.h>
#define LOGD(LOCAL_TAG,...) __android_log_print(ANDROID_LOG_DEBUG, LOCAL_TAG, __VA_ARGS__)
3. By default, the Android system sends stdout and stderr
(System.out and System.err) output to /dev/null. In
processes that run the Dalvik VM, you can have the system write a copy of the output to the log
file. In this case, the system writes the messages to the log using the log tags
setprop log.redirect-stdio true:
grep -irsn "redirect-stdio" .
./frameworks/base/docs/html/tools/debugging/debugging-log.jd:296:$ adb shell setprop log.redirect-stdio true
./frameworks/base/core/jni/AndroidRuntime.cpp:494: property_get("log.redirect-stdio", propBuf, "");
frameworks/base/core/jni/AndroidRuntime.cpp:
property_get("log.redirect-stdio", propBuf, "");
if (strcmp(propBuf, "true") == 0) {
logStdio = true;
}
if (logStdio) {
/* convert stdout/stderr to log messages */
opt.optionString = "-Xlog-stdio";
mOptions.add(opt);
}
4. system/core/logwrapper/logwrapper.c:
"Usage: logwrapper [-a] [-d] [-k] BINARY [ARGS ...]\n"
"\n"
"Forks and executes BINARY ARGS, redirecting stdout and stderr to\n"
"the Android logging system. Tag is set to BINARY, priority is\n"
"always LOG_INFO.\n"
"\n"
"-a: Causes logwrapper to do abbreviated logging.\n"
" This logs up to the first 4K and last 4K of the command\n"
" being run, and logs the output when the command exits\n"
"-d: Causes logwrapper to SIGSEGV when BINARY terminates\n"
" fault address is set to the status of wait()\n"
"-k: Causes logwrapper to log to the kernel log instead of\n"
" the Android system log\n");