Reputation: 3543
I have this lines in my code (.c) :
#define LOGI(x...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG,x)
#define LOGE(x...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,x)
I use them for outputing in LogCat, how to modify these lines to write messages to a file, or how can I get certain messages from LogCat in my code ? Thanks in advance.
Upvotes: 3
Views: 5546
Reputation: 459
If you are under an application, you can use /data/data//
so dry lab:
FILE f = fopen("/data/data/<application name>/<file name>", 'w');
fprintf(f, "my message\n");
fclose(f);
should work.
Upvotes: 0
Reputation: 36
If your end-goal is just to "record" certain logcat outputs, I would highly recommend installing the Catlog app on the phone/device (if thats what you are testing on) and using the "Record" feature that Catlog provides. Catlog allows you to view and filter the logcat output based on the tags and search keywords, and the resulting output can be saved to a file or emailed as an attachment. If you are testing on the emulator that comes with the SDK, just save the output from the Logcat window of Eclipse.
Upvotes: 0
Reputation: 4941
If this is for debugging purpose, I use this macro:
#define WRITE_LOG(s, ...) do { \
FILE *f = fopen("/data/local/tmp/log.txt", "a+"); \
fprintf(f, s, __VA_ARGS__); \
fflush(f); \
fclose(f); \
} while (0)
It has the advantage of being totally independent from the Android SDK/NDK, which allows to debug native code without having to modify all your build system and includes in order to link on the logcat functions.
A small warning though: I've taken the habit of creating the file using touch /data/local/tmp/log.txt
in adb-shell before launching the program, as in most cases the system prevents you from creating new files. And by the way, the location /data/local/tmp/ has the advantage of being accessible even without root privileges.
Upvotes: 9
Reputation: 40397
You can write a variable argument list to a file using the vfprintf() function.
This is probably cleaner than re-capturing your own log messages from the Android log system and writing those to a file, unless you need to include the messages generated by Java code as well.
Upvotes: 0