关于unix linux daemon程序编写的文章 e文 Unix Daemon Server Programming
作者 佚名技术
来源 操作系统
浏览
发布时间 2012-06-27
tant and should be logged. A programmer wants to see debug messages or a system operator wants to see error messages. There are several ways to handle those messages. Redirecting all output to standard I/O : This is what ancient servers do, they use stdout and stderr so that messages are written to console, terminal, file or printed on paper. I/O is redirected when starting the server. (to change destination, server must be restarted) In fact this kind of a server is a program running in foreground (not a daemon). # mydaemon 2> error.log This example is a program that prints output (stdout) messages to console and error (stderr) messages to a file named "error.log". Note that this is not a daemon but a normal program. Log file method : All messages are logged to files (to different files as needed). There is a sample logging function below. void log_message(filename,message) char *filename; char *message; { FILE *logfile; logfile=fopen(filename,"a"); if(!logfile) return; fprintf(logfile,"%s\n",message); fclose(logfile); } log_message("conn.log","connection accepted"); log_message("error.log","can not open file"); Log server method : A more flexible logging technique is using log servers. Unix distributions have system log daemon named "syslogd". This daemon groups messages into classes (known as facility) and these classes can be redirected to different places. Syslog uses a configuration file (/etc/syslog.conf) that those redirection rules reside in. openlog("mydaemon",LOG_PID,LOG_DAEMON) syslog(LOG_INFO, "Connection from host %d", callinghostname); syslog(LOG_ALERT, "Database Error !"); closelog(); In openlog call "mydaemon" is a string that identifies our daemon, LOG_PID makes syslogd log the process id with each message and LOG_DAEMON is the message class. When calling syslog call first parameter is the priority and the rest works like printf/sprintf. There are several message classes (or facility names), log options and priority levels. Here are some examples : Message classes : LOG_USER, LOG_DAEMON, LOG_LOCAL0 to LOG_LOCAL7 Log options : LOG_PID, LOG_CONS, LOG_PERROR Priority levels : LOG_EMERG, LOG_ALERT, LOG_ERR, LOG_WARNING, LOG_INFO About This text is written by Levent Karakas /* UNIX Daemon Server Programming Sample Program Levent Karakas To compile: cc -o exampled examped.c To run: ./exampled To test daemon: ps -ef|grep exampled (or ps -aux on BSD systems) To test log: tail -f /tmp/exampled.log To test signal: kill -HUP `cat /tmp/exampled.lock` To terminate: kill `cat /tmp/exampled.lock` */ #include #include #include #include #define RUNNING_DIR "/tmp" #define LOCK_FILE "exampled.lock" |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
上一篇: 2010年企业Linux应用趋势下一篇: 招兼职solaris讲师
关于关于unix linux daemon程序编写的文章 e文 Unix Daemon Server Programming的所有评论