📄 00000020.htm
字号:
{ <BR> va_list ap; <BR> va_start(ap, fmt); <BR> err_doit(1, fmt, ap); <BR> va_end(ap); <BR> abort(); /* dump core and terminate */ <BR> exit(1); /* shouldn't get here */ <BR>} <BR>/* Nonfatal error unrelated to a system call. <BR> * Caller specifies "errnoflag". */ <BR>static void <BR>err_doit(int errnoflag, const char *fmt, va_list ap) <BR>{ <BR> int errno_save; <BR> char buf[MAXLINE]; <BR> errno_save = errno; /* value caller might want printed */ <BR> vsprintf(buf, fmt, ap); <BR> if (errnoflag) <BR> sprintf(buf+strlen(buf), ": %s", strerror(errno_save)); <BR> strcat(buf, "\n"); <BR> fflush(stdout); /* in case stdout and stderr are the same */ <BR> fputs(buf, stderr); <BR> fflush(stderr); /* SunOS 4.1.* doesn't grok NULL argument */ <BR> return; <BR>} <BR>程序B.2 输出至标准出错文件的出错处理函数 <BR> 程序B.3包括了各log_xxx出错处理函数。若进程不以精灵进程方式进行,那么调 <BR>用者应当定义变量debug,并将其设置为非0值。在这种情况下,出错消息被送至标 <BR>准出错文件。若debug标志为0,则使用syslog功能(见13.4.2节)。 <BR>/* Error routines for programs that can run as a daemon. */ <BR>#include <errno.h> /* for definition of errno */ <BR>#include <stdarg.h> /* ANSI C header file */ <BR>#include <syslog.h> <BR>#include "ourhdr.h" <BR>static void log_doit(int, int, const char *, va_list ap); <BR>extern int debug; /* caller must define and set this: <BR> nonzero if interactive, zero if daemon */ <BR>/* Initialize syslog(), if running as daemon. */ <BR>void <BR>log_open(const char *ident, int option, int facility) <BR>{ <BR> if (debug == 0) <BR> openlog(ident, option, facility); <BR>} <BR>/* Nonfatal error related to a system call. <BR> * Print a message with the system's errno value and return. */ <BR>void <BR>log_ret(const char *fmt, ...) <BR>{ <BR> va_list ap; <BR> va_start(ap, fmt); <BR> log_doit(1, LOG_ERR, fmt, ap); <BR> va_end(ap); <BR> return; <BR>} <BR>/* Fatal error related to a system call. <BR> * Print a message and terminate. */ <BR>void <BR>log_sys(const char *fmt, ...) <BR>{ <BR> va_list ap; <BR> va_start(ap, fmt); <BR> log_doit(1, LOG_ERR, fmt, ap); <BR> va_end(ap); <BR> exit(2); <BR>} <BR>/* Nonfatal error unrelated to a system call. <BR> * Print a message and return. */ <BR>void <BR>log_msg(const char *fmt, ...) <BR>{ <BR> va_list ap; <BR> va_start(ap, fmt); <BR> log_doit(0, LOG_ERR, fmt, ap); <BR> va_end(ap); <BR> return; <BR>} <BR>/* Fatal error unrelated to a system call. <BR> * Print a message and terminate. */ <BR>void <BR>log_quit(const char *fmt, ...) <BR>{ <BR> va_list ap; <BR> va_start(ap, fmt); <BR> log_doit(0, LOG_ERR, fmt, ap); <BR> va_end(ap); <BR> exit(2); <BR>} <BR>/* Print a message and return to caller. <BR> * Caller specifies "errnoflag" and "priority". */ <BR>static void <BR>log_doit(int errnoflag, int priority, const char *fmt, va_list ap) <BR>{ <BR> int errno_save; <BR> char buf[MAXLINE]; <BR> errno_save = errno; /* value caller might want printed */ <BR> vsprintf(buf, fmt, ap); <BR> <BR> if (errnoflag) <BR> sprintf(buf+strlen(buf), ": %s", strerror(errno_save)); <BR> strcat(buf, "\n"); <BR> if (debug) { <BR> fflush(stdout); <BR> fputs(buf, stderr); <BR> fflush(stderr); <BR> } else <BR> syslog(priority, buf); <BR> return; <BR>} <BR>程序B.3 用于精灵进程的处理函数 <BR> <BR> <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -