Extra Credit: ex19

Am I on the right track in outputting the name of the function in the log error macros?

  1 #ifndef __dbg_h__
  2 #define __dbg_h__
  3 
  4 #include <stdio.h>
  5 #include <errno.h>
  6 #include <string.h>
  7 
  8 #ifdef NDEBUG
  9 #define debug(M, ...)
 10 #else
 11 #define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
 12 #endif
 13 
 14 #define clean_errno() (errno == 0 ? "None" : strerror(errno))
 15 #define log_err(M, ...)  fprintf(stderr, "[ERROR] (%s:%d: errno: %s in function: %s) " M "\n", __FILE__, __LINE__    , __func__, clean_errno(), ##__VA_ARGS__)
 16 #define log_warn(M,...) fprintf(stderr, "[WARN] (%s:%d: errno: %s in function: %s) " M "\n", __FILE__, __LINE__,     __func__, clean_errno(), ##__VA_ARGS__)
 17 #define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d in function: %s) " M "\n", __FILE__, __LINE__, __func__,     ##__VA_ARGS__)
 18 #define check(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); errno = 0; goto error; }
 19 #define sentinel(M, ...) { log_err(M, ##__VA_ARGS__); errno = 0; goto error; }
 20 #define check_mem(A) check((A), "Out of memory.")
 21 #define check_debug(A, M, ...) if(!(A)) { debug(M, ##__VA_ARGS__); errno = 0; goto error; }
 22 
 23 #endif
indent preformatted text by 4 spaces

I guess I did it! That was a very cool exercise!

1 Like