In development under Linux, printf prints debugging information to the standard output stdout (screen/terminal) by default. We can also use output redirection to output to a specified file. Output redirection means that information is written to a specified file that would otherwise be output to the screen. Such as:

However, sometimes you need to view the debugging information printed in real time and save the debugging information for others to analyze.

Here are two ways to achieve this requirement:

1. Use script tools

The Script tool is a very useful tool for recording output to a terminal. The usage method is as follows:

(1) Run the script log. TXT command to save the output information of the terminal. Log. TXT is the log file to be written and can be named at will.

(2) Enter exit to exit the Settings.

The demo is as follows:

2. Use the Tee tool

The TEE tool is used to redirect data to a file and, on the other hand, provide a copy of the redirected data as stdin for subsequent commands. Simply redirects data to a given file and screen.

There is a caching mechanism that outputs every 1024 bytes. If input data is received from a pipe, the buffer should be full before data is transferred to the specified file. If the file contains less than 1024 bytes, after receiving the data read from the standard input device, the buffer is flushed and the data is transferred to the specified file.

The demo is as follows:

As you can see, the text input from tee tool is cleaner than the text input from script tool, which is what the program prints out.

The above is the use of the two practical tools to share, I hope you like ~