Log File error handling

Source: Internet
Author: User
Tags openlog unix domain socket rsyslog

Log File error handling
This article describes the error handling program for advanced programming in the UNIX environment. The system is based on ubuntu16.04
Before writing an error handling program, we need to know some log file-related knowledge. Log files are basically prepared for the daemon without control terminals. The error records of each daemon are recorded in the corresponding log files. For example, the log file corresponding to cron is/var/log/cron. log, and the log file corresponding to mail is/var/log/mail. log. The detailed knowledge of log files is described in other sections, here, you only need to know that we write an error handler for our own daemon. In fact, calling a function redirects the error information to a file in/var/log. In the past, we may output data in the console or a self-created file. Here we just changed the form.
Why not output it to the console?
If so many daemon processes are output to the console, can you imagine what your console is like? And it is not convenient for later viewing.
Why not output it to our own file?
If an error occurs when you create a file record every time you write a daemon, do you remember that there are many files? What if I want to view it later?
Implementation Mechanism:
The syslog mechanism is responsible for sending and recording the information generated by the system kernel and tools. It consists of the syslog () call, the syslogd daemon process, and the configuration file/etc/rsyslog. conf. When the system kernel and tool generate information, call syslog () to send the information to syslogd. syslogd then sends the information to/etc/rsyslog. the Configuration Requirements in conf are as follows:
Recorded in system logs
Output to System Console
Forward to a specified user
Syslogd forwarded to other hosts through the network
Rsyslog. the conf file has a $ IncludeConfig field, which means to include the file after this field, usually/etc/rsysconfig. d. all the files at the end of conf are included. It is of great benefit to you to learn the contents of the files in this directory.
API:

 
 
  1. #include
  2. void openlog(const char*ident,int option,int facility)
  3. void syslog(int priority,const char*format,...)
  4. void closelog(void)
  5. int setlogmask(int maskpri)
Openlog:
This indicates what type of log files I open,
The first parameter can be set to a string even if it is null. Generally, it is set to the program name because the string will be added to the message that will be sent to the log file, therefore, setting the program name makes it easy for us to identify which program sent the message,
The second parameter is LOG_CONS, LOG_NDELAY, LOG_NOWAIT, LOG_ODELAY, LOG_PERROR, and LOG_PID. I will not write the parameter and check it myself.
The third parameter is used to determine the log file type. I have mentioned cron, mail, and so on before. The syslogd daemon searches for the files to be written by the secondary type in the configuration file based on the log type. If a match is found, the system writes the messages we sent later to this file. Its Parameters include LOG_AUTH, LOG_AUTHPRIV, LOG_CRON, LOG_DAEMON, LOG_FTP, LOG_KERN, LOG_LOCAL0,... LOG_LOCAL7, LOG_LPR, LOG_MAIL, LOG_NEWS, LOG_SYSLOG, LOG_USER, and login.
Openlog can not be called, because if you call syslog, the system will automatically call openlog when you call syslog. At this time, the priority parameter must be a combination of facility and level. Ident is set to null.
Syslog:
This function is called to write the corresponding information to the log file. If openlog is not called before, the priority parameter is a combination of priority and level (or operation). If yes, it is a separate value, the optional parameters are LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, and LOG_DEBUG. The priority ranges from high to low. The format is the same as the format of the printf function. The placeholder in it corresponds to the third parameter. This is the information to be sent.
Closelog:
This function can also not be called. Its function is to disable the descriptor that was used to communicate with the syslogd daemon. We have said that this function is implemented through a UNIX domain socket. This function requires a listener descriptor, after you have learned the unix domain socket, these concepts are clear. We will not discuss it here, as long as you know that this function is not called, it doesn't matter.
Setlogmask:
The value range of this function is the same as that of openlog's facility. Multiple types can be used for or operations. All types added to this function, messages cannot be sent to log files of this type using syslog. For example
 
 
  1. #include
  2. #include
  3. int main(void)
  4. {
  5. setlogmask(LOG_MAIL);
  6. syslog(LOG_MAIL | LOG_ERR,"hello,%s","world");
  7. return 0;
  8. }
The string "hello, world" will never be sent to mail. log, because setlogmask is called to block it.

Finally, we will mention another syslog variant:
 
 
  1. #include
  2. #include

  3. void vsyslog(int priority,const char*format,va_list arg)
For more information about va_list usage, see the stdarg. h header file. There is not much content in this header file.


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.