The Linux application uses the method of writing files to debug the program

  • 2020-12-21 18:16:12
  • OfStack

Linux, 1 all files, in the Android system itself, is Linux+java, also in the Linux running environment.

In general, we use printf when debugging programs.

In Android, we will use logcat, now, to introduce a common debugging method, debug write file debugging method.

In the Android system, it is extremely inconvenient to debug one C application. In order to preserve the integrity of log, the write-file debugging method can play an important role. Here is an example:


#include <stdio.h>
FILE * wirte_debug_file=NULL;
// Write debugging information to a file  
void write_Debug_to_file(char *debug_log)
{
 if(wirte_debug_file==NULL)
 wirte_debug_file = fopen("debug.txt","wt");
 if(wirte_debug_file==NULL)
 return;
 fputs(debug_log,wirte_debug_file);
}
int main(void)
{
 int i ;
 char buf[100];
 for(i = 0 ; i < 10 ; i++)
 {
 sprintf(buf,"i:%d\n",i);
 // Write debugging information  
 write_Debug_to_file(buf);
 }
 if(wirte_debug_file != NULL)
 fclose(wirte_debug_file);
 return 0 ;
}

Operation results:

When we open ES24en. txt, we can see:

[

i:0
i:1
i:2
i:3
i:4
i:5
i:6
i:7
i:8
i:9

]

The printed data is stored in this file, and we can even write the code snippet into log to whichever sentence it executes, and we'll come back to the problem.

This is a very good debugging tool.

conclusion


Related articles: