A problem related to the daemon is how to handle error messages.. Because it does not have a control terminal, it cannot simply write to standard errors. On many workstations, the console device runs a window system, so we do not want all daemon processes to be written to the console device. We do not want every daemon to write its own error messages to a separate file. System Administrators may feel a headache if they are concerned about which daemon writes to which record files and regularly checks these files. ThereforeA centralized daemon error logging facility.
The BSD syslog facility has been widely used since 4.2BSD. Most daemon use this facility. Figure 13-1 shows the detailed organizational structure of the syslog facility.
Openlog (* ident, option, syslog (priority, * closelog (setlogmask (
Openlog is optional. If openlog is not called, openlog is automatically called when syslog is called for the first time. The call to closelog is also optional-it only disables the descriptor that was used to communicate with the syslog daemon.
By calling openlog, we can specify an ident and add it to each log message. Ident is generally the program name (for example, cron, inetd ). The option parameter specifies bitwise blocking for many options. Table 13-1 describes the available options ). If an option is included in the openlog definition of Single UNIX Specification, the XSI column is represented by an ink point.
Table 13-1 option parameters of openlog
, Filename
The first call sets the ident string as the program name, prints the process ID, and sets the default facility of the system to the row printer system. Specify an error status and a message string for a syslog call. If openlog is not called, the second call may take the following form:
syslog(LOG_ERR | LOG_LPR, , filename);
Specify the priority parameter as a combination of level and facility.
In addition to syslog, many platforms also provide a variant for processing the variable parameter list.
#include <syslog.h><stdarg.h> vsyslog( priority, *format, va_list arg);
Most syslog implementations put messages in the queue for a short time. If a duplicate message is reached during this period of time, the syslog daemon will not write it to the log, but print and output a message, similar to "the previous message has been repeated for N times ".
This blog is excerpted from advanced programming for UNIX environments (version 2) and used only for personal learning records. For more information about this book, see:Http://www.apuebook.com/.