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

How to match the size of text in web design: small text, big experience

With the rise of mobile terminals such as iPad, p...

This article will show you how to use Vue 3.0 responsive

Table of contents Use Cases Reactive API related ...

How to deploy k8s in docker

K8s k8s is a cluster. There are multiple Namespac...

React implements the sample code of Radio component

This article aims to use the clearest structure t...

960 Grid System Basic Principles and Usage

Of course, there are many people who hold the oppo...

A simple example of creating a thin line table in html

Regarding how to create this thin-line table, a s...

SQL function to merge a field together

Recently, I need to query all the fields in a rel...

Use JS to zoom in and out when you put the mouse on the image

Use JS to zoom in and out when the mouse is on th...

Detailed explanation of Vue router routing guard

Table of contents 1. Global beforeEach 1. Global ...

MySQL 5.7.18 installation and configuration tutorial under Windows

This article shares the installation and configur...

How to Fix File System Errors in Linux Using ‘fsck’

Preface The file system is responsible for organi...

Example code for achieving hollowing effect with pure CSS

I have recently studied the hollowing effect. bac...

Analysis of several situations where MySQL index fails

1. Best left prefix principle - If multiple colum...

Example code for implementing page floating box based on JS

When the scroll bar is pulled down, the floating ...

Data storage implementation method in WeChat applet

Table of contents Global variable globalData Page...