| [ Team LiB ] |
|
Recipe 9.34 Writing Log Entries via C9.34.1 ProblemYou want to add information to the system log from a C program. 9.34.2 SolutionUse the system library functions openlog , syslog, and closelog (see The syslog API): syslog-demo.c:
#define _GNU_SOURCE /* for basename( ) in <string.h> */
#include <syslog.h>
#include <string.h>
int count = 0;
char *host = "some-machine ";
int main(int argc, char *argv[]) {
openlog(basename(argv[0]), LOG_PID, LOG_LOCAL3);
syslog(LOG_WARNING, "%d connection attempts from %s", count, host);
syslog(LOG_AUTHPRIV|LOG_ERR, "intruder alert!");
syslog(LOG_ERR, "can't open configuration file: %m");
closelog( );
return(0);
}
9.34.3 DiscussionLike Perl scripts [Recipe 9.33], C programs can pass the %m format specifier to syslog to include system error messages, corresponding to strerror(errno). Be sure to use %m only when a system error has occurred, to avoid misleading messages. 9.34.4 See Alsosyslog(3). |
| [ Team LiB ] |
|