Skip to content

Commit

Permalink
ex20
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Breed committed Feb 20, 2012
1 parent b3f786e commit c72ca93
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,5 +1,5 @@
CFLAGS=-Wall -g
SOURCES=ex1 ex4 ex6 ex7 ex8 ex9 ex10 ex11 ex12 ex13 ex14 ex15 ex16 ex17 ex18
SOURCES=ex1 ex4 ex6 ex7 ex8 ex9 ex10 ex11 ex12 ex13 ex14 ex15 ex16 ex17 ex18 ex20

all: $(SOURCES)

Expand Down
30 changes: 30 additions & 0 deletions dbg.h
@@ -0,0 +1,30 @@
#ifndef __dbg_h__
#define __dbg_h__

#include <stdio.h>
#include <errno.h>
#include <string.h>

#ifdef NDEBUG
#define debug(M, ...)
#else
#define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#endif

#define clean_errno() (errno == 0 ? "None" : strerror(errno))

#define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d: errno %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)

#define log_warn(M, ...) fprintf(stderr, "[WARN] (%s:%d: errno %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)

#define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)

#define check(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }

#define sentinel(M, ...) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }

#define check_mem(A) check((A), "out of memory")

#define check_debug(A, M, ...) if(!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; }

#endif
117 changes: 117 additions & 0 deletions ex20.c
@@ -0,0 +1,117 @@
#include "dbg.h"
#include <stdlib.h>
#include <stdio.h>


void test_debug()
{
debug("I have brown hair");
debug("I am %d years old", 25);
}

void test_log_err()
{
log_err("shit's broke");
log_err("ther are %d problems in %s", 0, "space");
}

void test_log_warn()
{
log_warn("you can safely ignore this");
log_warn("maybe consider having a look at: %s", "/etc/passwd");
}

void test_log_info()
{
log_info("this is something");
log_info("it happened %f times today", 1.3f);
}

int test_check(char *file_name)
{
FILE *input = NULL;
char *block = NULL;

block = malloc(100);
check_mem(block);

input = fopen(file_name, "r");
check(input, "failed to open %s", file_name);

free(block);
fclose(input);
return 0;

error:
if(block) free(block);
if(input) fclose(input);
return -1;
}

int test_sentinel(int code)
{
char *temp = malloc(100);
check_mem(temp);

switch(code) {
case 1:
log_info("it worked");
break;
default:
sentinel("I shouldn't run");
}

free(temp);
return 0;

error:
if(temp) free(temp);
return -1;
}

int test_check_mem()
{
char *test = NULL;
check_mem(test);

free(test);
return 1;

error:
return -1;
}

int test_check_debug()
{
int i = 0;
check_debug(i != 0, "oops, i was 0");

return 0;

error:
return -1;
}

int main(int argc, char *argv[])
{
check(argc == 2, "Need an argument");

test_debug();
test_log_err();
test_log_warn();
test_log_info();

check(test_check("ex20.c") == 0, "failed with ex20.c");
check(test_check(argv[1]) == -1, "failed with argv");
check(test_sentinel(1) == 0, "test_sentinel failed");
check(test_sentinel(100) == -1, "test_sentinel failed");
check(test_check_mem() == -1, "test_check_mem failed");
check(test_check_debug() == -1, "test_check_debug failed");

return 0;

error:
return 1;
}


0 comments on commit c72ca93

Please sign in to comment.