With kiss.h:
#include
#include
#include
#include
#include "kiss.h"
int main(void)
{
char *space = NULL;
FILE *fp = NULL;
INFO("Hello");
space = MALLOC(1000);
space = REALLOC(space, 10000);
fp = FOPEN("asdfasdf", "r");
FCLOSE(fp);
FREE(space);
INFO("Bye");
exit(0);
error:
exit(1);
}
Output:
[INFO] eg/test.c:12 main: Hello
[ERROR] eg/test.c:15 main: fopen failed: asdfasdf: No such file or directory
Output when compiled with -DNDEBUG:
Hello
fopen failed: asdfasdf: No such file or directory
|
With kiss.h, only using CHECK and INFO:
#include
#include
#include
#include
#include "kiss.h"
int main(void)
{
char *space = NULL;
char *space2 = NULL;
char *filename = "asdfasdf";
FILE *fp = NULL;
INFO("Hello");
CHECK(space = malloc(1000), "malloc failed");
CHECK(space2 = realloc(space, 10000), "realloc failed");
space = space2;
CHECK(fp = fopen(filename, "r"), "fopen failed: %s", filename);
CHECK(fclose(fp) == 0, "fclose failed");
free(space);
space = NULL;
INFO("Bye");
exit(0);
error:
exit(1);
}
Output:
[INFO] eg/test2.c:14 main: Hello
[ERROR] eg/test2.c:18 main: fopen failed: asdfasdf: No such file or directory
Output when compiled with -DNDEBUG:
Hello
fopen failed: asdfasdf: No such file or directory
|
Without kiss.h, minimal
#include
#include
#include
#include
int main(void)
{
char *space = NULL;
char *space2 = NULL;
FILE *fp = NULL;
fprintf(stderr, "Hello\n");
space = malloc(1000);
if (!space)
goto error;
space2 = realloc(space, 10000);
if (!space2)
goto error;
space = space2;
fp = fopen("asdfasdf", "r");
if (!fp)
goto error;
if (fclose(fp) < 0)
goto error;
fp = NULL;
free(space);
space = NULL;
fprintf(stderr, "Bye\n");
exit(0);
error:
perror("error");
exit(1);
}
Output:
Hello
error: No such file or directory
|