In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge of this article "how to print an effective log on C++", so the editor summarizes the following contents for you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how C++ prints an effective log" article.
Introduction
In daily code testing or running, it is necessary to print a log to detect the running status of the code. Let me give you three examples:
When the device is online in the Internet of things system, the online information can be written to the database, or the online record of the device can be printed to the log. With the id of the device as the file name, it is also convenient to find the log, and the online time and IP address can be printed to the text.
In a complex multi-link system, quickly locate the wrong link of the problem, print the data of each system data interface to print the log, if there is a return value to judge whether the execution is successful or not, you can only print the log of the error. when there is a problem, you can check the log file to locate that link.
Print the error information into a log file in exception capture to quickly check for problems with the code or data.
The role of the journal
The general program log comes from the following requirements:
Audit logs that record user actions are sometimes even required by regulatory authorities. The process of quickly locating the root cause of the problem and tracking the execution of the program. Tracking data change data statistics and performance analysis to collect operating environment data
Generally speaking, once an exception occurs after the program is online, the first thing to do is to figure out what happened at that time. What the user did at that time, whether the environment was affected, whether the data changed, whether it happened repeatedly, etc., and then further determine which aspect of the problem. It is determined that it is the problem of the program, and then it is up to the developer to reproduce, study, and propose a solution. At this time, the log provides us with first-hand information.
Log writing requires the readability of the log
Logs are for people to read, not only for themselves to understand, but also for other programmers who have not come into contact with our source code to know at a glance. Some colleagues print special symbols in their logs, such as "+" and "=", which are dazzling. In addition, outputting log categories to different files also helps us to eliminate interference and quickly find the information we need. Moreover, it is best to print English when printing the log to prevent garbled code from being printed if the Chinese language is not supported.
Performance of logs
Whether we write the log to a file or a database, we need to consume IO resources. Proper control of the output of the log also helps to improve the performance of the program. For example, try to avoid printing meaningless log content in large loops. It is best to determine the level of the log before outputting the log (for example. Call isDebugEnabled () before debug to make a judgment).
Take up disk space
Usually, we write the log to the log file on disk. It is good to use scrolling logs properly and to clear old files regularly. I have seen such an example, the program can not run after a few times, the first few times are normal. I couldn't figure out what was wrong with the program, only to find that it was the log files that filled the disk space. The log files of G are often seen in practical applications. It is also a challenge to find information useful for solving problems in log files of this size.
Timeliness of logs
Sometimes we can't find the problem in time. You need to trace the previous log. So we need to keep a log for a period of time to facilitate traceability.
Log level
Usually we log at a level above INFO in a production environment, so we have to make sure that the program still outputs enough information for us to make judgments in this case.
Log content
When we write a log, we need to pay attention to output the appropriate content. First, try to use business-related descriptions. Our program is to implement a certain business, so it is best to describe where the business process has come at this time. Second, avoid outputting sensitive information, such as usernames and passwords, in the log. And, keep the code consistent. If you can't guarantee it, try to use English instead of Chinese. In this way, when we get the log, we won't be confused by seeing a bunch of garbled codes.
Log format
The common log format should contain information for each log, including date, time, log level, code location, log content, error code and so on. The following is part of a working log file:
Code implementation
# include
# include / / CException
# include / / COleDateTime
# include
# include
# include
Using namespace std
Void PrintRunningLog (char * szbuff)
{
/ / AfxMessageBox (szbuff)
Int clientNumber = 1
CString strInfo
CString strCurrentTime
COleDateTime currentTime
CurrentTime = COleDateTime::GetCurrentTime ()
StrCurrentTime=currentTime.Format ("Y-%m-%d H:%M:%S"); / / print time
StrInfo = strCurrentTime
StrInfo + = ""
StrInfo + = szbuff
StrInfo + = "\ r\ n"
CString strLogTxtName
StrLogTxtName.Format ("log%d.txt", clientNumber); / / File name
FILE * stream;// file stream
If ((stream = fopen (strLogTxtName, "r")) = = NULL)
{
Fopen (strLogTxtName, "w"); / / New
}
Try
{
Int fh = _ open (strLogTxtName,_O_RDWR | _ Olympian creator _ S_IWRITE)
If (fh! =-1)
{
_ lseek (fh,0L,SEEK_END)
_ write (fh,strInfo,strlen (strInfo))
_ close (fh)
}
}
Catch (CException * pe)
{
Pe- > Delete ()
}
}
Int main ()
{
PrintRunningLog ("test")
Return 0
}
The test example is relatively simple and the file name is: 1
Screenshot of file name
The log content of the test is also relatively simple. Change the test content according to the actual situation.
The above is about the content of this article on "how C++ prints effective logs". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.