-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbg.h
More file actions
40 lines (29 loc) · 1.71 KB
/
dbg.h
File metadata and controls
40 lines (29 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef __dbg_h__ //this way you don't include the file twice by accident
#define __dbg_h__
#include <stdio.h>
#include <errno.h>
#include <string.h>
#ifdef NDEBUG //lets yu recompile with dbg log messages removed
#define debug(M, ...)
#else
#define debug(M, ...) fprintf(stderr, "DEBUG %s:%d " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
//translates any use of debug('format', arg1, arg2) to fprintf call to stderr
//##__VA_ARGS__ allows extra arguments to be placed here
#endif
#define clean_errno() (errno == 0 ? "None" : strerror(errno))//cretes safe readable version of 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__)
//3 macros above log messages for end user, can't be compiled out
#define check(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }//if A not tru, logs M and goto error: for cleanup
#define sentinel(M, ...) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }
//sentinel placed inside code you don't want to run, like if and switch cases you don't want eg: default
#define check_mem(A) check((A), "Out of memory,")//check if a pointer is valid
#define check_debug(A, M, ...) if(!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; }//still checks and handles, but doesn't log error. use for common errors
#endif
/* pattern for writing in C:
*call function
*see if return value is an error, must look up if so
*clean up all resouce created so far
*print useful error message
*/