- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
今天在工作中处理相对简单的 C 代码时,我开始做白日梦并编写了以下库以有效地生成错误消息。陪审团仍然不在 - 至少在我看来 - 这是否是一种比简单地将 perror
和 errno
适应你的目的更有效的方法......它不是甚至需要成为一个“库”,而不仅仅是一组宏。
现在,我调用库elib
:
#ifndef _ELIB_H_
#define _ELIB_H_
struct ErrorMap {
void (*fp);
int errorCode;
char * message;
};
#define eperror(...) fprintf(stderr, ##__VA_ARGS__)
char * getErrorMsg(struct ErrorMap *, void (*fp), int);
#endif /* _ELIB_H_ */
#include <stdlib.h>
#include "elib.h"
char * getErrorMsg(struct ErrorMap * errorMap, void (*fp), int code)
{
int i;
// TODO: Replace naive search
for (i=0; errorMap[i].fp != NULL; i++)
{
if (errorMap[i].fp == fp && errorMap[i].errorCode == code)
{
return errorMap[i].message;
}
}
return NULL;
}
#include <stdio.h>
#include <string.h>
#include "elib.h"
int xyzlib_openFile(int);
int xyzlib_putStuff(char *);
static struct ErrorMap xyzlib_ErrorMap [] = {
{xyzlib_openFile, -1, "Argument is less than 3"},
{xyzlib_putStuff, -1, "Argument is NULL"},
{xyzlib_putStuff, -2, "Length of argument is 0"},
{xyzlib_putStuff, -3, "Stuff is too long"},
{NULL, 0, NULL}
};
int xyzlib_openFile(int fd)
{
if (fd > 3)
{
return (-1);
}
// open a file.. or something.
return 0;
}
int xyzlib_putStuff(char * stuff)
{
if (stuff == NULL)
{
return (-1);
}
if (strlen(stuff) == 0)
{
return (-2);
}
if (strlen(stuff) > 3)
{
return (-3);
}
// do something ...
return (0);
}
int main(int argc, char ** argv)
{
int code;
if (argc != 2)
{
printf("Usage: %s <arg>\n", argv[0]);
return 1;
}
code = xyzlib_putStuff(argv[1]);
if (code < 0)
{
eperror("Error: %s\n", getErrorMsg(xyzlib_ErrorMap, xyzlib_openFile, code));
}
}
基本上,您在 ErrorMap 表中定义/注册返回代码,如果您收到错误指示(响应值 < 0),则 getErrorMsg
将查看已注册的表相应的代码和函数来获取正确的消息。我认为其值(value)在于,对于采用这种错误报告方法的给定库,可以为每个函数定义错误代码(而不是全局定义)——简化所有代码的管理。
但是,除了查找适当消息的小开销外,所有采用这种方法的 C 代码都需要每个函数(不返回非负整数)返回 int
,然后使用第一个参数作为指向返回值的指针——有点不合惯用但很常用。
假设目标市场是用于高可靠性嵌入式设备的软件(但不是非常受资源限制)。
有什么想法吗?提前致谢。
最佳答案
抱歉,我真的看不出这有什么意义。也许那是因为我在 C 中处理错误代码的方式。基本上我将错误值定义为单个模块接口(interface)的一部分。模块之间的错误值确实会发生冲突,即我不在乎两个模块的不同错误代码是否具有相同的数值。为了对不同的枚举进行强类型检查,我通常将它们包装到一个结构中。但我从不真正关心为每个函数制作唯一的错误代码,这似乎是您的方法。
同样,这是我的具体情况,但我从来没有真正觉得有必要做其他事情。为整个模块定义的错误代码通常适合在实现该模块的过程之间传递。现在,如果您的方法实际上可以映射到模块边界而不是函数怎么办? :)
关于c - 请批评 C 中的错误报告方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10592010/
这是建议的解决方案(我确实搜索了相同的解决方案 - 没有成功) public abstract class AsyncCache { /** * an ato
在挖掘一些旧的源代码时,我看到了以下内容: my $module = $some{module}; eval "require $module"; die "Bad module\n$@" if $@
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 3年前关闭。 锁定。这个问题及其答案是loc
我设计了以下方法来捕获整个AS3应用程序中的错误: 在Document类中,定义以下方法: //This is the handler for listening for errors protect
我想知道是否有人看到任何可能导致此代码出现问题的内容。我知道我可以使用其他方法/API 调用来完成此操作,但我正在尝试为我自己的平台独立性奠定基础?/跨平台互斥框架。 显然我需要做一些#ifdef 并
我一直在阅读大量有关实现响应式移动下拉菜单的不同方法的资料。纯 CSS 解决方案对我来说似乎有点笨拙,但我讨厌依赖 javascript 来处理像导航菜单这样重要的事情。我想到的是使用 javascr
我即将构建我的第一个基于 Lisp 的严肃项目:一个需要扩展到数以万计的用户(不是同时的 - 最多可能是数百个同时)的 Web 应用程序。 我打算使用的堆栈如下所示: Weblocks具有 Berke
我正在寻找 PerlCritic 的等价物对于 PHP。 PerlCritc 是一个静态源代码分析器,它对代码进行 qritiques,并就从未使用的变量到处理数据的不安全方式到几乎所有内容的所有内容
我正在编写一个 perl 脚本,该脚本需要在调用外部程序之前设置许多环境变量。我的代码有形式 $ENV{'VAR1'} = "value1"; $ENV{'VAR2'} = "value2
我是一名优秀的程序员,十分优秀!