Thoughts on log output during Embedded C/C ++ Development

Source: Internet
Author: User

During the development process, logs need to be output frequently to help debug and analyze problems. A good log output can help us quickly locate and analyze problems. In the embedded development process of some linux platforms, single-step debugging is not supported in the development process. A large part of debugging is completed through logs. I personally think about the reflection and summary of embedded development in the recent period. when outputting logs, I try to do the following: 1. I think the log content can be divided into: locating logs and Error Report logs, other debugging logs must be deleted when the code is submitted after debugging. This reduces the output of irrelevant logs, allowing you to view the log Content and locate problems, at the same time, it can reduce the amount of unnecessary code, increase the Definition of the code, and facilitate code maintenance. Some redundant debugging logs are shown in code segment 1. for development that is not convenient for single-step debugging, the single-step debugging mainly relies on logs, so such log output will be more. [Cpp] code segment 1 int Open (char * pFileName/* IN */) {printf ("into func: Open "); // ------------ additional debugging logs if (NULL = pFileName) {printf ("pointer of file name is NULL"); return-1 ;}if (NULL! = G_logfile) {printf ("logfile has been opened"); return 2;} printf ("into func: Open, before fopen "); // ------------ redundant debug log g_logfile = fopen (pFileName, "rw +"); if (NULL = g_logfile) {printf ("logfile open fail "); return-2;} return 1;} locate log: indicates that a function has been started or completed. This type of log can be used to isolate the problem domain. Once a program has a problem, you can narrow down the scope of troubleshooting Based on the positioning log, thus improving the problem resolution speed. Error Log: indicates that a specific problem occurs in the Code. For unexpected errors in the Code and try... all exceptions caught by catch must be output effectively and clearly. For example, if the file fails to be opened, log output should be available before it is returned. For more information, see section 3 in entry 2. Do not directly use library functions (such as printf and cout) output, but a log output function is customized for each cpp file (if it is object-oriented, a private log output member function is defined for each class ), call the output library function in the custom log output function, so that you can change the log output in a unified manner. For example, if you want to introduce log4cpp in the future, you only need to modify the custom log output function, without modifying the output of each log: for example, the original code: [cpp] Code Segment 2: int Open (char * pFileName/* IN */) {if (NULL = pFileName) {printf ("pointer of file name is NULL"); return-1 ;}if (NULL! = G_logfile) {printf ("logfile has been opened"); return 2;} g_logfile = fopen (pFileName, "rw +"); if (NULL = g_logfile) {printf ("logfile open fail"); return-2;} return 1;} modify it to a form that does not directly reference the output library function printf, for example: [cpp] Code Segment 3: int Open (char * pFileName/* IN */) {if (NULL = pFileName) {_ DisplayLog ("pointer of file name is NULL "); return-1;} if (NULL! = G_logfile) {_ DisplayLog ("logfile has been opened"); return 2;} g_logfile = fopen (pFileName, "rw +"); if (NULL = g_logfile) {_ DisplayLog ("logfile open fail"); return-2;} return 1;} the preceding custom log output function is: [cpp] code segment 3 void _ DisplayLog (char * pMsg/* IN */) {if (g_bUseLog & NULL! = PMsg) {www.2cto.com printf ("[test. cpp]: "); printf (" % s ", pMsg); printf (" \ n ") ;}3. log files should be able to be switched, that is, you can enable or disable log output without modifying the code or re-Compiling. This can be achieved by defining a switch variable, in the custom log output function, you can determine whether to output logs based on this switch, as shown in section 4. 4. Add the default file name to each log in the custom log output function of each cpp file, for example, [test. cpp]: when the project is large, it is easier to locate the location of each log when there are many logs, as shown in code segment 4. 5. log output should be simple and clear. You can simply describe and clear the problem. In previous code development, I have seen many people (in fact I have done this before) Write logs like this: printf ("***********************************"); in this way, the output is intended to increase the log's eye-catching level. However, it is often added here, which will be increased later. Soon, we will find a mess of things on full screen, debugging is not conducive. 6. Each log occupies one line.

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.