Retrieve Logcat data through native (C) code - android

I was wondering if there is any way to access the logcat logging data from native code ?
Also if someone knows what kind of shell can be accessed on the native linux system directly on the device itself (so NOT by starting a shell through adb on a client pc !!!) ?
Thanks in advance

You can either read the raw data fom /dev/log/main or you can run the built-in shell command logcat and pipe the result to a file descriptor as usual. The logcat command is usually preferably because then the printout is easier to filter and format.
As for the built-in shell, it is called toolbox and the source can be found in the Android open source project. The shell is rather similar to bash. Toolbox contains lots more functionality than just a shell. It is very similar to Busybox, but released under another license.

You can set it up using a log tag in your source file and then include the library as so:
#define LOG_TAG "some.unique.identifier" //I usually use the class name
#include <utils/Log.h>
Then in the Android.mk file you will need to add the liblog library dependency to your LOCAL_SHARED_LIBRARIES.
LOCAL_SHARED_LIBRARIES += liblog
Also, take note that logcat looks for the LOG_TAG definition to include as part of the tagging so it makes searching for your logs easier. Then you can log as such:
int my_int = 0;
ALOGI("some logs.... print my int: %d", my_int);
ALOGI is for info, you can also use ALOGE, ALOGD, ALOGV and ALOGW for error, debug, verbose and warning logging, respectively.
ALOG* is analogous to printf. I interchange them at times if I need to debug on different platforms, say Linux.

Related

Kivy application does not work on Android

I want write simple application in Python for Android using kivy. Sadly when I start example code I see only splash screen and few second later application finish work. There is a huge problem with debugging because adb on Linux Mint does not detect my device.
Can someone look at my code and tell my why?
To build application I use buildozer. You can also see create_env script to check all dependencies are there.
Best regards.
Draqun
EDIT:
I started debugging my application. Conclusion:
buildozer + python3 + kivy is a bad idea
if I use kivy.uix.button.Button when text attribute is str than I got exception "AttributeError: 'str' object has no attribute 'decode'"
if I use kivy.uix.button.Button when text attribute is bytes than I got exception "ValueError: Button.text accept only str"
It looks like loop with no solution. Some idea when I should report it?
Exception is in .buildozer/android/platform/build/build/python-installs/pad/android/init.py" file so it does not look like kivy and/or buildozer exception.
I've used python-for android tool and faced with the same errors. But in my case, app didn't run at all - crashed from splash screen. Finally, I've found a solution. You can try the same way.
So my pipeline was python3 + python-for-android (p4a tool, python-for-android, from master branch) + kivy (1.10.1)
There is a file "_android.pyx" for android building recipe (full list of avaliable p4a recipes you can see by command p4a recipes). This file is, possibly, used by Buildozer, and exactly used by P4A during APK building procedure. You need to fix it.
You may find it's location in Ubuntu (for example) via:
sudo updatedb
locate _android.pyx
It's path should be something like:
~/.local/lib/python3.6/site-packages/pythonforandroid/recipes/android/src/android/_android.pyx
There should be a string:
python_act = autoclass(JAVA_NAMESPACE.decode('utf-8') + u'.PythonActivity')
so you should change it - something like this:
python_act = autoclass(str(JAVA_NAMESPACE) + u'.PythonActivity'),
or just use some hardcode:
python_act = autoclass("org/kivy/android/PythonActivity")
Or there might be the other decode() usage in sources.
The reason: differences between Python2 and Python3 - the decode() method is usable on the equivalent binary data type in either Python 2 or 3, but it can’t be used by the textual data type consistently between Python 2 and 3 because str in Python 3 doesn’t have the method decode function has different realisation in Python3. More details are here:
pyporting features
issues p4a's github
Hope, it will help you somehow.

Android reboot command - Who calls reboot_main()?

I am looking at the source for the android shell reboot command.
int reboot_main(int argc, char *argv[])
What I am not clear about is if this is a standalone binary, who calls reboot_main() ?
In a standard glibc linked binary, I was expecting to find a "main()" as the entry point for the program.
What am I missing here, could someone help me understand what is going on ?
Thanks,
vj
They are all compiled into one overall executable, with main in https://android.googlesource.com/platform/system/core.git/+/android-4.2.2_r1/toolbox/toolbox.c
Then, based on the actual program name invoked (usually argv[0]) it calls the appropriate method.
The commands are part of the build via the
#define TOOL(name) int name##_main(int, char**);
macro in toolbox.c which is used in the Android.mk file to generate tools.h.

C4DROID Makefile issues

For all you C4DROID dev's, I have a question about the Makefile option. I have been toying around with it for some time now, and just can't seem to get it to function properly. I've tried implementing the tutorial from http://mrbook.org/tutorials/make/ as well as http://www.gnu.org/software/make/manual/make.html#Implicit-Rules . These sites have helped me at least get different error messages (which in some ways is helpful) but I'm just not sure what else I need to be doing. I'm not sure how much detail I should include about what I'm doing (first time posting), so here goes.
At the moment, I have the files (for test purposes only) main.cpp, hello.cpp, factorial.cpp, and functions.h . I created a Makefile with:
all:
g++ main.cpp hello.cpp factorial.cpp functions.h -o MyFile
I have also tried variations like:
all: testTwo
testTwo:
g++ main.cpp hello.cpp factorial.cpp functions.h -o MyFile
In the compilation settings, I have selected Makefile, I have not modified the "Commands Before Make" code (I don't even understand it), my Make command is
make -f Makefile
I have selected the Native Activity in Run Mode, and that's all that I can think of explaining. My error message I get on compilation is "Failed to copy file".
Any help with the process would be greatly appreciated and thank you in advance.
In the compile options screen, section 'Result binary filename': enter the name of the executable that your Makefile generates i.e. MyFile for your example.
I also tend to use 'Run mode: Terminal app' for simple console programs. NativeActivity is intended for Android NDK app development and hence is a bit more involved.

Makefile(s) debug: which file/line calls to a command?

I'm facing a bug in a makefile build system (Android built under Linux) - some files are removed by an 'rm' command, and I can see that command in the build log.
How can I find the exact line in the makefiles which calls the 'rm' ? Is there any automated method?
For GNU Make you can do the following trick:
__shell := $(SHELL)
SHELL = \
$(warning making '$#'$(if $^, from '$^')$(if $?, because of '$?'))$(__shell)
SHELL variable is expanded each time when Make invokes a sub-shell to execute a recipe. In these lines it is replaced so that on each expansion it will print a target, its prerequisites and prerequisites that are newer than the target. Also each debug message is prepended with the file and line number of the rule being executed.
The same technique is used in GMD to set breakpoints to certain targets.
Assuming your make is a Gnu make, you can also pass some debugging options, like --debug=b (basic debugging messages, very often enough) or --debug=all which is the same as -d
Some files may be removed because they are intermediate. Read also about secondary files and precious files in make
You may try make -d -w and then grep your file from huge amount of output lines.

How to print log messages with in Android framework

I am trying to print log messages within core Android framework files. For example, I tried logging messages within MediaRecorderClient.cpp under frameworks\base\media\libmediaplayerservice\. I've tried LOGV, LOGE, LOGD, printf, and __android_log_print, without any success.
Which command should I use to print log messages?
Log should be used, but it will print to logcat not system print.
Example:
Log.d("filter", example text); // filter is any tag you want to use as filter
You can open logcat in eclipse from window-show view -> other -> android -> logcat
What kind of error do you receive? If it does not compile, make sure you've included <android/log.h>, also in Android.mk:
LOCAL_LDLIBS := -llog
If it compiles but does not produce any output, then, like #Warpzit has said, you have to look at logcat to see your messages.
It also seems that you can only pass a char* to the JNI LOG methods. So if you have numbers in your debug string, you have to put them into a char buffer (with sprintf).
/system/core/include/cutils/log.h defines the macros for logging in the Android Framework.
So you uncomment the line that says #define LOG_NDEBUG 0 at the top of the source file and it will enable VERBOSE logging. You will need to recompile that entire library and put it into your android system.img build.
I believe the macro calls are ALOGV, ALOGE and so on.
It is similar to this other answer:
How to get Verbose logging in logcat for a specific module
First declare string
Private String Tag = getClass().getsimplename();
and than in your method
Log.d(Tag,"This is my oncreate method");

Categories

Resources