-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdebug.h
More file actions
55 lines (47 loc) · 1.28 KB
/
debug.h
File metadata and controls
55 lines (47 loc) · 1.28 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef DEBUG_H
#define DEBUG_H
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>
#include <unistd.h>
extern int debug_level;
extern int debug_syslog;
extern int debug_console;
#define LOG_SIZE 1024
#define _log_text(_fmt, ...) \
char _text[LOG_SIZE]; \
int _len=0; \
_len+=snprintf(_text+_len, LOG_SIZE-_len, "%d:%s:%d %s: ", getpid(), __FILE__, __LINE__, __func__); \
_len+=snprintf(_text+_len, LOG_SIZE-_len, _fmt, __VA_ARGS__);
#define debug(_level, _fmt, ...) { \
if(_level<=debug_level) { \
_log_text(_fmt, __VA_ARGS__); \
_len+=snprintf(_text+_len, LOG_SIZE-_len, "\n"); \
if(debug_syslog) syslog(LOG_INFO, _text); \
if(debug_console) { \
fprintf(stdout, _text); \
fflush(stdout); \
} \
} \
}
#define error(_fmt, ...) { \
_log_text(_fmt, __VA_ARGS__); \
_len+=snprintf(_text+_len, LOG_SIZE-_len, ": %s (errno=%d)\n", strerror(errno), errno); \
if(debug_syslog) syslog(LOG_ERR, _text); \
if(debug_console) { \
fprintf(stderr, _text); \
fflush(stderr); \
} \
}
#define panic(_fmt, ...) { \
_log_text(_fmt, __VA_ARGS__); \
_len+=snprintf(_text+_len, LOG_SIZE-_len, "\n"); \
if(debug_syslog) syslog(LOG_ERR, _text); \
if(debug_console) { \
fprintf(stderr, _text); \
fflush(stderr); \
} \
}
#endif /* DEBUG */