Add a utility function for creating errors

This commit is contained in:
Nick Chambers 2022-07-01 12:41:50 -05:00
parent 26525b24fb
commit db8c84bbe0
2 changed files with 28 additions and 1 deletions

View File

@ -15,5 +15,10 @@ struct gargoyle_err {
};
void gargoyle_emit_err(struct gargoyle_err *err);
void gargoyle_fill_err(struct gargoyle_err *err, gargoyle_err_type code, int errno_code, const char *func, const char *errno_func, const char *fmt, ...);
#define GARGOYLE_NEW_ERR(err) gargoyle_fill_err(err, 0, 0, __func__, NULL, "")
#define GARGOYLE_MK_ERR(err, code, fmt, ...) gargoyle_fill_err(err, code, 0, __func__, NULL, fmt, __VA_ARGS__)
#define GARGOYLE_SYS_ERR(err, code, errno_code, errno_func, fmt, ...) gargoyle_fill_err(err, code, errno_code, __func__, errno_func, fmt, __VA_ARGS__)
#endif

View File

@ -1,5 +1,6 @@
#include <gargoyle/delusion.h>
#include <gargoyle/twine.h>
#include <stdarg.h>
#include <stdio.h>
void gargoyle_emit_err(struct gargoyle_err *err) {
@ -11,3 +12,24 @@ void gargoyle_emit_err(struct gargoyle_err *err) {
fprintf(stderr, "%s\n", err->msg);
}
void gargoyle_fill_err(struct gargoyle_err *err, gargoyle_err_type code, int errno_code, const char *func, const char *errno_func, const char *fmt, ...) {
err->code = code;
err->errno = errno_code;
if(func) {
gargoyle_cpy(err->func, func, GARGOYLE_MAX_FUNC_SZ, GARGOYLE_FLG_FILL0);
}
if(errno_func) {
gargoyle_cpy(err->errno_func, errno_func, GARGOYLE_MAX_FUNC_SZ, GARGOYLE_FLG_FILL0);
}
if(fmt) {
va_list args;
va_start(args, fmt);
vsnprintf(err->msg, GARGOYLE_MAX_ERR_SZ, fmt, args);
va_end(args);
}
}