How to use file writing to debug a Linux application

How to use file writing to debug a Linux application

In Linux, everything is a file, so the Android system itself is just Linux+Java, and it also runs in the Linux environment.

Usually, we use printf when debugging programs.

In Android, we will use logcat. Now, I will introduce you to a commonly used debugging method, the debug writing file debugging method.

In the Android system, debugging a C application is extremely inconvenient. In order to preserve the integrity of the log, the file debugging method can play a significant role. Here is an example:

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

Running results:

After we open debug.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 saved in this file. We can even write any sentence in the code segment into the log and analyze the problem later.

This is a very good debugging tool.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Java remote debugging tutorial (taking Eclipse as an example)
  • Detailed explanation of the steps to debug a program using breakpoints in C#
  • IOS uses NSAssert() and NSParameterAssert to debug the program
  • Using PDB library to debug program in Python
  • Steps to connect Ubuntu to Android debugging program using WiFi
  • Android Development Notes: Learn to use Logcat to debug the program in one minute
  • Debug class for debugging programs under asp
  • Using bat to delete the system script debugger
  • asp:debug class debugger

<<:  Global call implementation of Vue2.x Picker on mobile terminal

>>:  Detailed explanation of IFNULL() and COALESCE() functions to replace null in MySQL

Recommend

Detailed explanation of custom events of Vue components

Table of contents Summarize <template> <...

Tutorial on installing nginx in Linux environment

Table of contents 1. Install the required environ...

How to make your own native JavaScript router

Table of contents Preface Introduction JavaScript...

JavaScript to switch multiple pictures

This article shares the specific code of JavaScri...

Summary of English names of Chinese fonts

When using the font-family property in CSS to ref...

Detailed explanation of nginx optimization in high concurrency scenarios

In daily operation and maintenance work, nginx se...

How to use Node.js to determine whether a png image has transparent pixels

background PNG images take up more storage space ...

Vue makes a simple random roll call

Table of contents Layout part: <div id="a...

Use docker to build kong cluster operation

It is very simple to build a kong cluster under t...

Summary of Linux system user management commands

User and Group Management 1. Basic concepts of us...

Solution to MySQL startup successfully but not listening to the port

Problem Description MySQL is started successfully...

Detailed explanation of the use of Teleport in Vue3

Table of contents Purpose of Teleport How Telepor...

Simple understanding and examples of MySQL index pushdown (ICP)

Preface Index Condition Pushdown (ICP) is a new f...