syslog function prototypes
#include <syslog.h>
void Openlog (const char *ident, int option, int facility);
void syslog (int priority, const char *format, ...);
void Closelog (void);
#include <stdarg.h>
void Vsyslog (int priority, const char *format, va_list AP);
1#include <stdio.h>2#include <sys/types.h>3#include <unistd.h>4#include <signal.h>5#include <sys/param.h>6#include <sys/stat.h>7#include <time.h>8#include <syslog.h>9 Ten intInit_daemon (void) One { A - intpid; - inti; the -signal (sigttou,sig_ign); -signal (sigttin,sig_ign); -signal (sigtstp,sig_ign); +signal (sighup,sig_ign); - +PID =fork (); A if(PID >0) at { -Exit0);//ends the parent process, making the child process a background process - } - Else if(PID <0) - { - return-1; in } - //set up a new process group, in which the child process becomes the first process in the process group so that the process is detached from all endpoints toSetsid (); + /*Create a new child process again, exit the parent process, ensure that the process is not the process leader, and that the process cannot open a new terminal*/ -PID =fork (); the if(PID >0) * { $Exit0); Panax Notoginseng } - Else if(PID <0) the { + return-1; A } the /*Close any re-required file descriptors inherited from the parent process*/ + for(i=0; i < Nofile; Close (i++)); - $ /*change the working directory so that the process does not contact any file system*/ $ChDir"/"); - - /*Set the text screen Word to 0*/ the -Umask0); Wuyi the /*mandatory slightly sigchld signal*/ -signal (sigchld,sig_ign); Wu return 0; - } About $ - intMain () - { -time_t now; AInit_daemon (); +Syslog (log_user| Log_info,"Test Daemon! \ n"); the while(1) - { $Sleep8); theTime (&Now ); theSyslog (log_user| Log_info,"system time: \t%s\t\t\n", CTime (&Now )); the } the}
Note: Before using syslog, you need to first configure/etc/syslog.conf (/etc/rsyslog.conf in the Linux version) to add the last line of the file
User.*/var/log/test.log, and then restart the Syslog service with the following commands:/etc/init.d/syslog stop (or/etc/init.d/rsyslog stop)/etc/init.d/ Syslog start (or/etc/init.d/rsyslog start)
For syslog Configuration Please refer to the following information
Http://www.lvtao.net/server/linux-syslog.html
http://blog.csdn.net/needle2/article/details/6826523
syslog functions and Syslog configuration