设为首页 加入收藏

TOP

嵌入式系统C编程之错误处理(八)
2015-01-22 20:57:58 来源: 作者: 【 】 浏览:45
Tags:嵌入式 系统 编程 错误 处理
?Fork()函数出错退出时依赖系统清理资源。若还需清理其他资源(如已创建的临时文件),可增加一个负责清理的回调函数。
?
? ? ?注意,并非所有系统函数都可封装,应根据具体业务逻辑确定。
?
? ? ?2) 封装错误输出
?
? ? ?通常需要使用ISO C变长参数表特性。例如《Unix网络编程》中将输出至标准出错文件的代码封装如下:
?
复制代码
?1 #include
?2 #include
?3 #define HAVE_VSNPRINTF ?1
?4 #define MAXLINE ? ? ? ? 4096 ?/* max text line length */
?5 int daemon_proc; ?/* set nonzero by daemon_init() */
?6 static void err_doit(int errnoflag, int level, const char * fmt, va_list ap)
?7 {
?8 ? ? int errno_save, n;
?9 ? ? char buf[MAXLINE + 1];
10 ? ??
11 ? ? errno_save = errno; ? ?/* Value caller might want printed. */
12 #ifdef HAVE_VSNPRINTF
13 ? ? vsnprintf(buf, MAXLINE, fmt, ap);
14 #else
15 ? ? vsprintf(buf, fmt, ap); ? ?/* This is not safe */
16 #endif
17 ? ? n = strlen(buf);
18 ? ? if (errnoflag) {
19 ? ? ? ? snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save));
20 ? ? }
21 ? ? strcat(buf, "\n");
22 ? ??
23 ? ? if (daemon_proc) {
24 ? ? ? ? syslog(level, buf);
25 ? ? } else {
26 ? ? ? ? fflush(stdout); ? ?/* In case stdout and stderr are the same */
27 ? ? ? ? fputs(buf, stderr);
28 ? ? ? ? fflush(stderr);
29 ? ? }
30 ? ??
31 ? ? return;
32 }
33?
34 void err_ret(const char * fmt, ...)
35 {
36 ? ? va_list ap;
37 ? ??
38 ? ? va_start(ap, fmt);
39 ? ? err_doit(1, LOG_INFO, fmt, ap);
40 ? ? va_end(ap);
41 ? ??
42 ? ? return;
43 }
首页 上一页 5 6 7 8 下一页 尾页 8/8/8
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Objective-C中的继承和多态 下一篇objective-c 复制对象

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: