Skip to content

Instantly share code, notes, and snippets.

@stantona
Created June 6, 2013 05:17
Show Gist options
  • Save stantona/5719469 to your computer and use it in GitHub Desktop.
Save stantona/5719469 to your computer and use it in GitHub Desktop.
#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__, ##__VAR_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(), ##__VAR_ARGS__)
#define log_warn(M, ...) fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VAR_ARGS__)
#define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VAR_ARGS__)
#define check(A, M, ...) if(!(A)) { log_err(M, ##__VAR_ARGS__); errno=0; goto error; }
#define sentinel(M, ...) { log_err(M, ##__VAR_ARGS__); errno=0; goto error; }
#define check_mem(A) check((A), "Out of memory.")
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment