roza/lib/err.c

86 lines
1.6 KiB
C

#include "err.h"
RZ_ENUM_C(ErrType, ERR_TYPES);
void err_init(err_t* err)
{
assert(err);
err->size = 0;
err->total = 0;
err->quiet = 0;
}
void err_free(err_t* err)
{
assert(err);
for (size_t i=0; i<err->size; i++)
{
free(err->errors[i]->what);
free(err->errors[i]);
}
err->size = 0;
}
void err_of_type(err_t* err, char* what, int line, ErrType type)
{
assert(err);
assert(err->size + 1 < RZ_ERROR_STACK_SIZE);
err->errors[err->size] = malloc(sizeof(err_msg_t));
err->errors[err->size]->what = strdup(what);
err->errors[err->size]->line = line;
err->errors[err->size]->type = type;
err->size++;
err->total++;
}
void err_fatal(err_t* err, char* what, int line)
{
err_of_type(err, what, line, ERR_FATAL);
}
int err_dump(err_t* err)
{
assert(err);
int is_fatal = 0;
for (size_t i=0; i<err->size; i++)
{
if (err->errors[i]->type == ERR_WARNING)
{
if (!err->quiet)
{
fprintf(stderr, "\33[33mWARNING\33[0m[:%d] %s\n",
err->errors[i]->line,
err->errors[i]->what);
}
}
else if (err->errors[i]->type == ERR_FATAL)
{
if (!err->quiet)
{
fprintf(stderr, "\33[31mERROR\33[0m[:%d] %s\n",
err->errors[i]->line,
err->errors[i]->what);
}
is_fatal = 1;
}
}
int total = err->total;
int quiet = err->quiet;
err_free(err);
err_init(err);
err->total = total;
err->quiet = quiet;
return is_fatal;
}