错误处理库 [英] Error handling library

查看:105
本文介绍了错误处理库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早上我解决了这个问题,以解决我的

程序处理错误的方式不一致的问题。代码本身很好,因为它编译了理查德希思菲尔德的gcc标签(加上-c,因为它没有主要的)。


有评论吗?


/ *标题开头* /

#ifndef _ERROR_H_

#define _ERROR_H_


#include< stdlib.h> / *需要size_t * /


/ *诊断严重性* /

#define ADL_INFO 0

#define ADL_WARN 1

#define ADL_BENIGN 2

#define ADL_FATAL 3

struct error_t;

struct error_l;


int adl_set_buffer(size_t size);

int adl_register_error(char * file,char * error,int line,int fatal);

void adl_terminate(int display);


struct error_t

{

char * text;

char * file;

int line;

int severity;

};


struct error_l

{

struct error_t * err;

size_t ctr;

size_t max;

int wrap;

};


const char * adl_message [] = {" Info"," Warning"," Error" ;,致命错误};

struct error_l * adl_err = NULL;


#endif

/ *结束标题* /


/ *源码错误开始。* /

#include" error.h"

#include< stdio.h>

#include < stdlib.h>

int adl_set_buffer(size_t size)

{

adl_err = malloc(sizeof * adl_err);

if(adl_err == NULL)

返回-1;


adl_err-> err = malloc(sizeof * adl_err-> err *尺寸);

adl_err-> max =尺寸;

adl_err-> ctr = 0;


if( adl_err-> err == NULL)

{

免费(adl_err);

adl_err = NULL;

返回-1;

}


返回0;

}

int adl_register_error(char * file,char * error,int line,int sev)

{

unsigned i = adl_err-> ctr; / *这只是为了便于阅读。 * /


if(adl_err == NULL)

返回-1;


adl_err-> err [i] .text =错误;

adl_err-> err [i] .file = file;

adl_err-> err [i] .line = line;

adl_err-> err [i] .severity = sev;


i ++;


if(i > adl_err-> max)

{

adl_err-> ctr = 0;

adl_err-> wrap = 1; < br $>
}

其他

adl_err-> ctr = i;


返回0;

}

void adl_terminate(int display)

{

unsigned ctr;

struct error_t * cur; / *替换adl_err-> err [ctr]以提高可读性。 * /


/ *如果我们没有在屏幕上写任何东西,只需终止。 * /

if(adl_err == NULL ||!display)

退出(EXIT_FAILURE);


/ *如果我们'已经包裹,立即开始上次错误。 * /

ctr = adl_err->换行? (adl_err-> ctr + 1):0;


/ *循环在我们达到上次错误时结束* /

while(ctr!= adl_err- > ctr)

{

cur =& adl_err-> err [ctr];

printf("%s:% s(%s中的行%d)。\ n",

adl_message [cur-> severity],

cur-> text,cur-> line,cur-> file);


ctr ++;

if(ctr == adl_err-> max)/ *如果我们'我们需要这个' '包装* /

ctr = 0;

}

}

/ *来源结束* /

-

Andrew Poelstra< http://www.wpsoftware.net/blog >

给我发电子邮件,请使用apoelstra。在上面的地址。

我知道那个小镇的区域就像我的脑袋一样。

I hammered this out this morning to fix inconsistancies with the way my
programs handle errors. The code itself is fine, in that it compiles with
Richard Heathfield''s gcc tags (plus -c because it doesn''t have a main).

Any comments?

/* Start of header */
#ifndef _ERROR_H_
#define _ERROR_H_

#include <stdlib.h> /* Needed for size_t */

/* Diagnostic severity */
#define ADL_INFO 0
#define ADL_WARN 1
#define ADL_BENIGN 2
#define ADL_FATAL 3

struct error_t;
struct error_l;

int adl_set_buffer (size_t size);
int adl_register_error (char *file, char *error, int line, int fatal);
void adl_terminate (int display);

struct error_t
{
char *text;
char *file;
int line;
int severity;
};

struct error_l
{
struct error_t *err;
size_t ctr;
size_t max;
int wrap;
};

const char *adl_message[] = {"Info", "Warning", "Error", "Fatal error"};
struct error_l *adl_err = NULL;

#endif
/* End of header */

/* Start of source error.c */
#include "error.h"
#include <stdio.h>
#include <stdlib.h>
int adl_set_buffer (size_t size)
{
adl_err = malloc (sizeof *adl_err);
if (adl_err == NULL)
return -1;

adl_err->err = malloc (sizeof *adl_err->err * size);
adl_err->max = size;
adl_err->ctr = 0;

if (adl_err->err == NULL)
{
free (adl_err);
adl_err = NULL;
return -1;
}

return 0;
}
int adl_register_error (char *file, char *error, int line, int sev)
{
unsigned i = adl_err->ctr; /* This is here for readability only. */

if (adl_err == NULL)
return -1;

adl_err->err[i].text = error;
adl_err->err[i].file = file;
adl_err->err[i].line = line;
adl_err->err[i].severity = sev;

i++;

if (i > adl_err->max)
{
adl_err->ctr = 0;
adl_err->wrap = 1;
}
else
adl_err->ctr = i;

return 0;
}
void adl_terminate (int display)
{
unsigned ctr;
struct error_t *cur; /* Replaces adl_err->err[ctr] for readability. */

/* If we aren''t writing anything to the screen, simply terminate. */
if (adl_err == NULL || !display)
exit (EXIT_FAILURE);

/* If we''ve wrapped, start immediately past last error. */
ctr = adl_err->wrap ? (adl_err->ctr + 1) : 0;

/* Loop ends when we reach last error */
while (ctr != adl_err->ctr)
{
cur = &adl_err->err[ctr];
printf ("%s: %s (line %d in %s).\n",
adl_message[cur->severity],
cur->text, cur->line, cur->file);

ctr++;
if (ctr == adl_err->max) /* We need this if we''re wrapping*/
ctr = 0;
}
}
/* End of source */
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.

推荐答案

Andrew Poelstra写道:
Andrew Poelstra wrote:

今天早上我敲定了这个
来解决我的
程序处理错误的方式不一致的问题。代码本身很好,
因为它编译了理查德希思菲尔德的gcc标签
(加上-c,因为它没有主要的)。

有任何意见吗?

/ *标题开头* /
#ifndef _ERROR_H_
#define _ERROR_H_


N869

7.1.3保留标识符

[#1]每个标题声明或定义其关联子条款中列出的所有标识符

,并可选择声明或

定义其关联的未来库中列出的标识符

指示子条款和标识符,它们总是保留用于任何用途或用作文件范围
/>
标识符。

- 所有以下划线开头的标识符和

大写字母或另一个下划线是

总是保留用于任何用途。

#include< stdlib.h> / *需要size_t * /

I hammered this out this morning
to fix inconsistancies with the way my
programs handle errors. The code itself is fine,
in that it compiles with
Richard Heathfield''s gcc tags
(plus -c because it doesn''t have a main).

Any comments?

/* Start of header */
#ifndef _ERROR_H_
#define _ERROR_H_
N869
7.1.3 Reserved identifiers
[#1] Each header declares or defines all identifiers listed
in its associated subclause, and optionally declares or
defines identifiers listed in its associated future library
directions subclause and identifiers which are always
reserved either for any use or for use as file scope
identifiers.
-- All identifiers that begin with an underscore and
either an uppercase letter or another underscore are
always reserved for any use.

#include <stdlib.h> /* Needed for size_t */




您可以使用< stddef.h>如果你想要更多的极简主义,而不是< stdlib.h>,




-

pete



You can use <stddef.h> instead of <stdlib.h>,
if you want more minimalism.

--
pete


2006-06-26,pete< pf ***** @ mindspring.com>写道:
On 2006-06-26, pete <pf*****@mindspring.com> wrote:
Andrew Poelstra写道:
Andrew Poelstra wrote:

今天早上我敲定了
以解决我的
程序处理错误的方式不一致。代码本身很好,
因为它编译了理查德希思菲尔德的gcc标签
(加上-c,因为它没有主要的)。

有任何意见吗?

/ *标题开头* /
#ifndef _ERROR_H_
#define _ERROR_H _

I hammered this out this morning
to fix inconsistancies with the way my
programs handle errors. The code itself is fine,
in that it compiles with
Richard Heathfield''s gcc tags
(plus -c because it doesn''t have a main).

Any comments?

/* Start of header */
#ifndef _ERROR_H_
#define _ERROR_H_



N869
[#1]每个标题声明或定义其相关子条款中列出的所有标识符,并可选择声明或定义其相关未来库中列出的标识符
指示子条款和标识符,它们始终保留用于任何用途或用作文件范围
标识符。
- 所有以下划线开头的标识符和
大写字母或另一个下划线总是保留用于任何用途。



N869
7.1.3 Reserved identifiers
[#1] Each header declares or defines all identifiers listed
in its associated subclause, and optionally declares or
defines identifiers listed in its associated future library
directions subclause and identifiers which are always
reserved either for any use or for use as file scope
identifiers.
-- All identifiers that begin with an underscore and
either an uppercase letter or another underscore are
always reserved for any use.




我知道双下划线规则。我不知道下划线 -

资本规则。哎呀。



I knew the double-underscore rule. I didn''t know about the underscore-
capital rule. Oops.


#include< stdlib.h> / *需要size_t * /

#include <stdlib.h> /* Needed for size_t */



您可以使用< stddef.h>而不是< stdlib.h>,
如果你想要更多的极简主义。



You can use <stddef.h> instead of <stdlib.h>,
if you want more minimalism.




谢谢!


- -

Andrew Poelstra< http://www.wpsoftware.net/blog >

给我发电子邮件,请使用apoelstra。在上面的地址。

我知道那个小镇的区域就像我的脑袋一样。



Thanks!

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.


Andrew Poelstra写道:
Andrew Poelstra wrote:

2006-06-26,pete< pf ***** @ mindspring.com>写道:

On 2006-06-26, pete <pf*****@mindspring.com> wrote:
Andrew Poelstra写道:
Andrew Poelstra wrote:

今天早上我敲定了
以解决我的
程序处理错误的方式不一致。代码本身很好,
因为它编译了理查德希思菲尔德的gcc标签
(加上-c,因为它没有主要的)。

有任何意见吗?

/ *标题开头* /
#ifndef _ERROR_H_
#define _ERROR_H _

I hammered this out this morning
to fix inconsistancies with the way my
programs handle errors. The code itself is fine,
in that it compiles with
Richard Heathfield''s gcc tags
(plus -c because it doesn''t have a main).

Any comments?

/* Start of header */
#ifndef _ERROR_H_
#define _ERROR_H_



N869
[#1]每个标题声明或定义其相关子条款中列出的所有标识符,并可选择声明或定义其相关未来库中列出的标识符
指示子条款和标识符,它们始终保留用于任何用途或用作文件范围
标识符。
- 所有以下划线开头的标识符和
大写字母或另一个下划线总是保留用于任何用途。



N869
7.1.3 Reserved identifiers
[#1] Each header declares or defines all identifiers listed
in its associated subclause, and optionally declares or
defines identifiers listed in its associated future library
directions subclause and identifiers which are always
reserved either for any use or for use as file scope
identifiers.
-- All identifiers that begin with an underscore and
either an uppercase letter or another underscore are
always reserved for any use.



我知道双下划线规则。我不知道下划线 -
资本规则。哎呀。



I knew the double-underscore rule. I didn''t know about the underscore-
capital rule. Oops.




我现在正在使用这个表格:H_ERROR_H

表头卫。


更难以创建任何带有领先

下划线的标识符,而不是记住

关于领先下划线的完整规则。


-

pete



I''m currently using this form: H_ERROR_H
for header guards.

It''s easier to not create any identifiers with a leading
underscore than it is to memorize
the complete set of rules regarding leading underscores.

--
pete


这篇关于错误处理库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆