13.4. Error LoggingOne problem a daemon has is how to handle error messages. It can't simply write to standard error, since it shouldn't have a controlling terminal. We don't want all the daemons writing to the console device, since on many workstations, the console device runs a windowing system. We also don't want each daemon writing its own error messages into a separate file. It would be a headache for anyone administering the system to keep up with which daemon writes to which log file and to check these files on a regular basis. A central daemon error-logging facility is required.The BSD syslog facility was developed at Berkeley and used widely in 4.2BSD. Most systems derived from BSD support syslog.Until SVR4, System V never had a central daemon logging facility.The syslog function is included as an XSI extension in the Single UNIX Specification.The BSD syslog facility has been widely used since 4.2BSD. Most daemons use this facility. Figure 13.2 illustrates its structure. Figure 13.2. The BSD syslog facility![]() void openlog(const char *ident , int option , int ![]() void syslog(int priority , const char *format , ...); void closelog(void); int setlogmask(int maskpri ); |
Returns: previous log priority mask value |
option | XSI | Description |
---|---|---|
LOG_CONS | • | If the log message can't be sent to syslogd via the UNIX domain datagram, the message is written to the console instead. |
LOG_NDELAY | • | Open the UNIX domain datagram socket to the syslogd daemon immediately; don't wait until the first message is logged. Normally, the socket is not opened until the first message is logged. |
LOG_NOWAIT | • | Do not wait for child processes that might have been created in the process of logging the message. This prevents conflicts with applications that catch SIGCHLD, since the application might have retrieved the child's status by the time that syslog calls wait. |
LOG_ODELAY | • | Delay the open of the connection to the syslogd daemon until the first message is logged. |
LOG_PERROR | Write the log message to standard error in addition to sending it to syslogd. (Unavailable on Solaris.) | |
LOG_PID | • | Log the process ID with each message. This is intended for daemons that fork a child process to handle different requests (as compared to daemons, such as syslogd, that never call fork). |
facility | XSI | Description |
---|---|---|
LOG_AUTH | authorization programs: login, su, getty, ... | |
LOG_AUTHPRIV | same as LOG_AUTH, but logged to file with restricted permissions | |
LOG_CRON | cron and at | |
LOG_DAEMON | system daemons: inetd, routed, ... | |
LOG_FTP | the FTP daemon (ftpd) | |
LOG_KERN | messages generated by the kernel | |
LOG_LOCAL0 | • | reserved for local use |
LOG_LOCAL1 | • | reserved for local use |
LOG_LOCAL2 | • | reserved for local use |
LOG_LOCAL3 | • | reserved for local use |
LOG_LOCAL4 | • | reserved for local use |
LOG_LOCAL5 | • | reserved for local use |
LOG_LOCAL6 | • | reserved for local use |
LOG_LOCAL7 | • | reserved for local use |
LOG_LPR | line printer system: lpd, lpc, ... | |
LOG_MAIL | the mail system | |
LOG_NEWS | the Usenet network news system | |
LOG_SYSLOG | the syslogd daemon itself | |
LOG_USER | • | messages from other user processes (default) |
LOG_UUCP | the UUCP system |
level | Description |
---|---|
LOG_EMERG | emergency (system is unusable) (highest priority) |
LOG_ALERT | condition that must be fixed immediately |
LOG_CRIT | critical condition (e.g., hard device error) |
LOG_ERR | error condition |
LOG_WARNING | warning condition |
LOG_NOTICE | normal, but significant condition |
LOG_INFO | informational message |
LOG_DEBUG | debug message (lowest priority) |
Example
In a (hypothetical) line printer spooler daemon, you might encounter the sequenceopenlog("lpd", LOG_PID, LOG_LPR);
syslog(LOG_ERR, "open error for %s: %m", filename);
The first call sets the ident string to the program name, specifies that the process ID should always be printed, and sets the default facility to the line printer system. The call to syslog specifies an error condition and a message string. If we had not called openlog, the second call could have beensyslog(LOG_ERR | LOG_LPR, "open error for %s: %m", filename);
Here, we specify the priority argument as a combination of a level and a facility .In addition to syslog, many platforms provide a variant that handles variable argument lists.[View full width]#include <syslog.h>
#include <stdarg.h>
void vsyslog(int priority , const char *format ,

All four platforms described in this book provide vsyslog, but it is not included in the Single UNIX Specification.Most syslogd implementations will queue messages for a short time. If a duplicate message arrives during this time, the syslog daemon will not write it to the log. Instead, the daemon will print out a message similar to "last message repeated N times."
