This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interceptor_debug.c
80 lines (61 loc) · 1.91 KB
/
interceptor_debug.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef INTERCEPTOR_DEBUG_C_INCL
#define INTERCEPTOR_DEBUG_C_INCL
#include "interceptor_pragmas.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "interceptor_conf.c"
////// Debug messages:
// TODO: I guess these are all reserved names.
#define _DEFAULT_LOG_FILE stderr
#define _DEBUG_PRINT_L_unique(ID, LEVEL, ...) \
if (debug_level() >= LEVEL) { \
FILE* _log_file##ID = open_log_file(); \
fprintf(_log_file##ID, "%s", log_prefix()); \
fprintf(_log_file##ID, "DEBUG: "); \
fprintf(_log_file##ID, __VA_ARGS__); \
if (_log_file##ID != _DEFAULT_LOG_FILE) { \
fclose(_log_file##ID); \
} \
}
#define _DEBUG_PRINT_L_unique_expander(ID, LEVEL, ...) \
_DEBUG_PRINT_L_unique(ID, LEVEL, __VA_ARGS__)
#define DEBUG_PRINT_L(LEVEL, ...) \
_DEBUG_PRINT_L_unique_expander(__LINE__, LEVEL, __VA_ARGS__)
#define DEBUG_PRINT(...) \
DEBUG_PRINT_L(2, __VA_ARGS__)
#define _LOG_PRINT_unique(ID, ...) \
if (debug_level() >= 1) { \
FILE* _log_file##ID = open_log_file(); \
fprintf(_log_file##ID, "%s", log_prefix()); \
fprintf(_log_file##ID, __VA_ARGS__); \
if (_log_file##ID != _DEFAULT_LOG_FILE) { \
fclose(_log_file##ID); \
} \
}
#define _LOG_PRINT_unique_expander(ID, ...) \
_LOG_PRINT_unique(ID, __VA_ARGS__)
#define LOG_PRINT(...) \
_LOG_PRINT_unique_expander(__LINE__, __VA_ARGS__)
static inline int debug_level() {
GET_AND_CACHE_ENV(debug_print_flag, "_PATH_INTERCEPTOR_DEBUG");
if (debug_print_flag && strlen(debug_print_flag)) {
return strtol(debug_print_flag, NULL, 0);
}
return 1;
}
static inline FILE* open_log_file() {
GET_AND_CACHE_ENV(log_filepath, "_PATH_INTERCEPTOR_LOG_FILE");
if (log_filepath && strlen(log_filepath)) {
return fopen(log_filepath, "a");
}
return _DEFAULT_LOG_FILE;
}
static inline const char* log_prefix() {
GET_AND_CACHE_ENV(_log_prefix, "_PATH_INTERCEPTOR_LOG_PREFIX");
if (!_log_prefix) {
return "STATUS: ";
}
return _log_prefix;
}
#endif