Advance understanding of software Development (15) Program debugging tool: Log

Source: Internet
Author: User
Tags definition integer log variables printf

If there is one person in the world who can guarantee that a written code is absolutely correct, then there is no doubt that he must be the best programmer in the world, not one. Why do I need to do a full self-test (including unit tests and integration tests) after the code is written? Just because people make mistakes, there are bugs in the program. As a software developer, you must learn to test the program, that is to learn the program debugging.

Generally speaking, there are several ways to debug your code:

First, see by the naked eye. In the development phase, we write every line of code need to use our "eyes" more than a few times review. If you want to ask, what is the best code debugging tool? I think it's human eyes. Whether it's a code or a document, you need to pass through our eyes before you check with tools.

Second, compile the code to discover syntax errors. The compiler can help us find syntax errors in the code, but there is nothing to do about those hidden errors, such as logic errors.

Third, use code inspection tools (such as pclint, etc.) to check the code. If the code compiles, it does not mean that it has no problem. In school, we generally think that as long as the program can be run. But in the actual software development project, the program can run up, just "Long March go through the first step." Code-checking tools allow you to discover errors that many compilers cannot discover, such as variables that have not been referenced, values are assigned to each other, and functions are invoked without declaration.

Four, debug the code. For a program that is running normally and the output is incorrect, we can find out the problems in it by setting breakpoints and using a single step tracking debugging method. For example, in VC + + 6.0, you can achieve a single step of the code debugging, and output variables in a step to produce the value, you can judge the correctness of the logic of the program.

The log file of the program is analyzed. Single-Step debugging of code is only more appropriate when the number of lines of code is small, such as the program above in school textbooks. But in the actual software project, the code is less than thousands of lines, more than tens of thousands of lines, with a single step debugging method is obviously inappropriate. In order to track changes in the value of a variable, this method can take several hours, which has a serious impact on productivity. In order to solve the problem of debugging large program file code, the log system came into being. Print the log in an important place in the program, and then analyze the resulting log to find the corresponding code problem. Therefore, log file analysis is the main means of code debugging in large software projects.

This article gives a detailed description of the log-related content.

1. What is a log file?

In the business software system to use a lot of log, log can play the role of "netizens", which for fault location and system normal operation and maintenance plays an important role.

A log file is a file that records the execution of a program from a write log function in a program. The Write log function is also written in C, which is called as C function. Call the function in the appropriate place, can have a comprehensive understanding to the whole program's running condition, convenient to track and debug the program.

2. About log level and log configuration instructions

(1) Log level

Things have priorities, and log information is important and unimportant points. Generally according to the importance level, the log grade is divided into several categories. In the software development project that the author has participated in, there are 7 levels, which are represented by the macro definition as follows:

Log level definition

#define LOG_FATAL (int) 1//Critical Error

#define LOG_ERROR (int) 2//General error

#define LOG_WARN (int) 3//warning message

#define LOG_INFO (int) 4/general Information

#define LOG_TRACE (int) 5//tracking information

#define LOG_DEBUG (int) 6//debug information

#define LOG_ALL (int) 7//All

The developer takes a different log level based on the specific log that is being printed.

(2) Log configuration

Due to different product program lines, deployment, implementation functions, etc., the requirements for log printing are also different, so there is a need for configuration to control the number of logs and display.

In the configuration file, there is a dedicated [LOG] configuration section with the following configuration entries:

[LOG]

; log level, 0-fatal 1-error 2-warn 3-info 4-trace 5-debug

Loglevel=

; The maximum capacity of each log file

Logmaxsize=

; Whether to output the number of rows in the code for the log, 1-yes 0-no

logposition=

Where loglevel is used to control the level of the print log, log information in the code that is larger than the configured value is not displayed in the log file, Logmaxsize is used to control the upper limit of the size of the generated log file, and the file is regenerated after the value is exceeded Logposition is used to control whether the number of lines of code is displayed in the log file to facilitate the mapping of the log to the code.

3. How do I invoke the Write log function?

The call to the log function follows the calling rule of the generic function. There are two types of write-log functions, as follows:

(1) The first kind of form: Writelog (LogLevel, Loginfo). Where the parameter loglevel refers to the log level (see the Description in section 2nd), the parameter loginfo is the specific log information to be printed, and we check the operation of the program accordingly. An invocation of the function is such as: Writelog (Log_info, "The value of this integer is 3."), the log level is Log_info, and the log information is "The value of this integer is 3." (This information is exported to the log file).

(2) Type II: Writelogex (LogLevel, Loginfo, Parainfo). This is an extended log function that can not only output log information, but also display the value of the variable in the log information. The invocation of the function, for example: Writelogex (Log_info, "The value of integer iint is%d.", Iint), which outputs the value of the integer variable iint, can compare the call of the function with the call to the printf function ( You can assume that the Writelogex function simply adds a log level parameter to the printf function.

Related Article

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.