A
Andrew Poelstra
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.text = error;
adl_err->err.file = file;
adl_err->err.line = line;
adl_err->err.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 */
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.text = error;
adl_err->err.file = file;
adl_err->err.line = line;
adl_err->err.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 */