your code generates an error on my emulator. But you say
When I run the Android application for the first time everything is going well, I can see the logs in the Log.txt file
Perhaps you could send us more of your code so that we can reproduce the error.
This is my attempt to reproduce your question.
#include<stdio.h> #include<jni.h> #include<android/log.h>//allow android logging #include<errno.h>//for errors #include<string.h> #define LOG_TAG "DEO MSG"//all my logs are labeled with this #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) void Java_com_deo_MyActivity_writeLogFileUsingC(JNIEnv * env, jobject thisObject) { char filename[]="/data/LogTest/Log.txt"; LOGE("native method started");//is used exactly like the usual printf() FILE * pFile; LOGE("trying to open file for writing"); pFile= fopen(filename, "w"); if(pFile==NULL) { LOGE("Failed to open the file %s in mode 'w'.(DETAILS)%s ",filename,strerror(errno)); } else { LOGE("trying to write to file"); fprintf(pFile,"logExample "); //example of a log. fclose(pFile);//safely close our file LOGE("file writing done"); } }
The error it generates in logcat is
ERROR/DEO MSG(816): Failed to open the file /data/LogTest/Log.txt in mode 'w'.(DETAILS)No such file or directory
I think my problem with your code might be a resolution. Describe this more for us.
PS:
- I personally prefer using logcat for debugging compared to log files
source share