I've seen a lot of different ways in Java to dump Android logcat messages to a file, but I haven't been able to find a way of doing it in C.
Is it possible to programmatically retrieve the messages in C and dump them to a file?
Create a child logcat process, read its stdout and use cstdio to save data to a file.
In that way Android isn't different from any other linux distributive.
Related
In Android, I know it's possible to use Runtime.getRuntime().exec(...) to execute native command line on android system like echo or ls.
I wonder if is there is any way to get data from any sensor module (like photo or gps) not from Android API (through Java or Kotlin), but by executing a command line with Runtime.getRuntime().exec(...). Is there a way to do it?
Technically all that the Android framework ( + HAL ) does is communicate via system calls with the kernel.
It would certainly be possible for you to write a binary ( C/C++) that does that communication for you, bypassing the framework.
And then you could call that binary with Runtime.getRuntime().exec(...) ( assuming rooted and have access ).
There aren't many tools to access the sensors like that ( expect maybe some vendors might have for testing). The only thing that comes to mind that you could use to get some information is by calling dumpsys in shell. This will give you lots of info about the current state of the system, and for example some location data as explaind in this answer
You can pack binary executables into your apk and launch them via Runtime.getRuntime().exec(...). Depending on the data you want to read it may be possible to implement an C/C++ program which reads /proc or /dev. If you completely statically link this executable (use i.e. musl libc) you can call it from your android app to read the data you need.
Basically, I have a C++ program that finds the sum of two numbers given. I need to provide the two numbers to the C++ program as input using my android app and then display the result in my android app. I guess I need to use parcelable class. Can someone please tell me the steps to be followed?
Edit: I forgot to mention that the C++ program that I intend to communicate with is an executable program (sum.exe)
To run a C++ executable on Android, you can use something like Runtime.exec("sum 1 2"). There are a lot of tutorials, e.g. https://www.mkyong.com/java/how-to-execute-shell-command-from-java/. The output (stdout and stderr) can be parsed, too. A more sophisticated way is to use ProcessBuilder, but the idea is the same.
If you want your executable to keep running in background, and send the numbers to crunch once in a while, you can either use input pipe, or some IPC protocol. Shared memory works well, see e.g. How to use Shared Memory (IPC) in Android.
You can use JNI code, take a look here:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo025
where you can find super simple code with C++ being called via JNI wrapper from Java.
I want to implement __android_log_write() functionality using write() or some api available in libc(actually ulibc). The reason being that i cannot use any libraries associated with android as that would increase the memory required. I have very limited amount of memory as my code is running in separate memory region reserved during boot up. Main goal is to attach my debugging logs to logcat.
I am looking something similar to this:
write(1,"sandy",6);
The abovce code i can directly write to stdout. Similarly, i want to use write() or something else and write to logcat. What is the clean way to do it.
Hope i am clear. Thanks.
Got the answer. We need to open /dev/radio and write into them.
Thanks
I'm currently working on the database part of a project where I have to merge the contents of two databases and I would like to ask you if there exists a simple API method
within the Android API/SDK itself that dumps me a database to a SQL text file.
Actually I found no hint in the API documentation about such an implementation myself. And I really doubt there is a single line method somewhere buried behind the curtain.
However I've already made a workaround using the sqlite3 shell tools of Android Linux by invoking:
String[] cmd = {"/system/bin/sh", "-c", ..., "sqlite3 ..."};
Runtime.getRuntime().exec( cmd );
Where I have two choices, a) pipe it directly to an output file or b) write the file using BufferedOutputStream.
Nevertheless, most likely due to compatibility issues am I asking you of a more convenient way within the API itself, rather than using the critical shell trick within the App.
I am also pretty much interested in any fast & pretty clues about merging two databases using Android's database methods.
Thanks.
There's nothing built in to the API that will help you. You could query, and write the INSERT statements yourself. There's a blog entry (http://mgmblog.com/2009/02/06/export-an-android-sqlite-db-to-an-xml-file-on-the-sd-card/) , on building an XML file from the results of your query. A few tweaks, and you coule build your own dump file.
You don't say what kind of merge you need to do, but you might be able to use ORMLite, by doing something like override the equals method on your model objects to compare the records and/or combine data from certain fields (assuming the schema is the same), without having to write a lot of SQL.
In Android, What is the difference between two namespaces,
android.util.Log
java.util.Logging
I am using android.util.Log. Now i am trying to save log to file, but file logging functions are not available in android.util.Log, how to do it ?
-- edit --
I already use Log.d(), Log.e() everywhere in my app, is there any way to redirect them to file, instead of changing code and adding another library ?
In this answer microlog4android is recommended, but i'm not able to find any documentation or examples.
How CatLog is able to grab all logs and save it to a file? I want to do that in my application itself.
Similar questions have been asked here and here. They both suggest to have a look at microlog4android.
Found similar question and answer
use logcat -f in order to dump it to a file in the filesystem.