Embedded debugging: redirecting printf to a file

Source: Internet
Author: User
Embedded debugging printf and rintf allocation

In addition to manual analysis, the simplest and most direct debugging method is printf. However, the int printf (const char * format,...) function is not recommended for beginners in C language ,...), it is a slightly more complex fprintf () function, because it makes it easier for us to redirect the error output information to the specified device. The fprintf () function is prototype as follows:

Int fprintf (File * stream, const char * format ,...)

As you can see, compared with the printf () function, the first parameter file * stream exists, meaning that the printed content is output to the stream pointed to by the file stream pointer. A stream usually refers to a continuous byte sequence input or output by a program, such as a mouse, keyboard, disk, screen, modem, or printer) the input and output are all processed by streams. in C language, all streams appear in the form of files-not necessarily physical disk files, it can also be a logical file corresponding to an input/output source. The C language provides 5 standard streams that your program can use at any time without opening or closing them. The five standard streams are listed below.
------------------------------------------------
Name Description
------------------------------------------------
Stdin Standard Input Keyboard
Stdout standard output screen
Stderr standard error screen
Stdprn standard printer LPT1 Port
Stdaux standard serial device COM1 port
------------------------------------------------
Among them, stdprn and stdaux are not always pre-defined, because LPT1 and COM1 ports are meaningless in some operating systems, while stdin, stdout, and stderr are always pre-defined. In addition, stdin is not necessarily from the keyboard, and stdout is not necessarily displayed on the screen. They can all be redirected to disk files or other devices. In the header file stdio. H, we can find stdin, stdout, and stderr as follows:

/* Standard streams. */extern struct _ io_file * stdin;/* standard input stream. */extern struct _ io_file * stdout;/* standard output stream. */extern struct _ io_file * stderr;/* standard error output stream. */

When using the fprintf () function, we can usually set the first parameter to stdout or stderr. stderr rather than stdout is recommended when error debugging information is printed, at the same time, because the kernel has different priorities when processing stdout and stderr, the latter has a higher priority. Therefore, sometimes stderr can get output when the program exits abnormally, but stdout cannot. Printf (...) is actually equivalent to fprintf (stdout,...), which is why we do not recommend it. When outputting debugging information, we recommend that you use fprintf (stderr ,...), Or use a specified file stream fprintf (some_stream ,...). So how to redirect the debugging information in fprintf () when necessary? Let's take a look at the following methods:

When debugging information is large and requires some time or other auxiliary tools to search and filter, it is not enough to use the display screen to output debugging information, at this time, we often output this information to the so-called log file, and then carefully analyze the log file to find the problem. ØUse shell I/O redirectionThe simple log writing method can be implemented through the shell I/O redirection mechanism, for example, the following code:

1 # include <stdio. h>

2 3 int main () 4 {5 fprintf (stdout, "This is a standard output info! \ N "); 6 fprintf (stderr," This is a standard error output info! \ N "); 7 return 0; 8}

 

By default, the result of compilation and running is that all the printed information is output on the screen:

$ GCC fprint. C-o fprint

$./Fprintthis is a standard output info! This is a standard error output info!

 

This is because the stdout and stderr devices opened by shell are displayed on the screen by default. However, we can write the printed information to the file through the shell redirection function. For example:

$./Fprint> output. Log

This is a standard error output info! $ Cat output. logthis is a standard output info!

 

In this way, the stdout output is written to the output. log file, but the stderr output is still on the screen. How to redirect stderr? This requires the file descriptor defined by shell. In shell, the file descriptors of stdin, stdout, and stderr are 0, 1, and 2 respectively. We can use the following method to redirect:

$./Fprint> output. log 2> error. Log

$ Cat output. logthis is a standard output info! $ Cat error. logthis is a standard error output info! $./Fprint> output. log 2> & 1 $ cat output. logthis is a standard error output info! This is a standard output info!

 

Where. /fprint> output. log 2> error. log writes stdout and stderr output to the file output. log and error. log, and. /fprint> output. log 2> & 1 indicates to append the stderr output to the stdout file output. log (the result is output. log contains both stdout output and stderr output ). Some common shell I/O syntaxes are as follows: cmd> file redirects stdout to file
CMD> file: redirects stdout to the file (append)
CMD 1> Fiel redirects stdout to file
CMD> file 2> & 1 redirect stdout and stderr together to the file.
CMD 2> file: redirects stderr to the file.
CMD 2> file redirects stderr to the file (append)
CMD> file 2> & 1 redirects stderr and stderr together to the file (append). In simple debugging, we can use these methods to quickly obtain log files. Ø Use freopen () for redirectionSometimes we need to be able to control the redirection of the standard stream in the program, then we can use the standard C library function freopen (). The function prototype of freopen () is as follows:

File * freopen (const char * filename, const char * mode, file * Stream)

 

 

The following code is used to test stderr redirection using the freopen () function:

1 # include <stdio. h>

2 3 int main () 4 {5 If (freopen ("Err. log "," W ", stderr) = NULL) 6 fprintf (stderr," error redirecting stderr \ n "); 7 fprintf (stdout," This is a standard output info! \ N "); 8 fprintf (stderr," This is a standard error output info! \ N "); 9 fclose (stderr); 10 return 0; 11}

 

In row 3, we use the freopen () function to redirect stderr to the "Err. log" file. The result is as follows:

$ GCC print_log.c-O print_log

$./Print_logthis is a standard output info! $ Cat err. logthis is a standard error output info!

 

It can be seen that the information printed from row 3 to stderr is redirected to the err. Log File, while the printing information of Row 3 to stdout is still output to the screen.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.