Skip to content

Instantly share code, notes, and snippets.

@sunfuze
Last active August 29, 2015 14:24
Show Gist options
  • Save sunfuze/9190dd229881c35ffddc to your computer and use it in GitHub Desktop.
Save sunfuze/9190dd229881c35ffddc to your computer and use it in GitHub Desktop.
zed's debug macros
//copy: http://c.learncodethehardway.org/book/ex20.html
#ifndef __dbg_h
#define __dbg_h
#include <stdio.h>
#include <errno.h>
#include <string.h>
#ifndef NDEBUG
#define debug(M, ...)
#else
#define debug(M, ...) fprintf(stderr, "[DEBUG] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#endif
#define clear_errno() (errno == 0 ? "NONE" : strerror(errno))
#define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clear_errno(), ##__VA_ARGS__)
#define log_warn(M, ...) fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clear_errno(), ##__VA_ARGS__)
#define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clear_errno(), ##__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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment