1 | /*****************************************************/ |
---|
2 | /* this file is part of the tiTan / tiTanNIT Project */ |
---|
3 | /* and allowed only for use with this. */ |
---|
4 | /* */ |
---|
5 | /* copyright by NIT */ |
---|
6 | /*****************************************************/ |
---|
7 | |
---|
8 | #ifndef DEBUG_H |
---|
9 | #define DEBUG_H |
---|
10 | |
---|
11 | short debug_level = 10; |
---|
12 | |
---|
13 | // mc debug_level = 50; |
---|
14 | // panel debug_level = 60; |
---|
15 | |
---|
16 | #ifdef BETA |
---|
17 | void __cyg_profile_func_enter(void *this_fn, void* call_size) __attribute__((no_instrument_function)); |
---|
18 | void __cyg_profile_func_enter(void *this_fn, void* call_size) |
---|
19 | { |
---|
20 | int i = 0; |
---|
21 | pthread_t threadid = pthread_self(); |
---|
22 | |
---|
23 | for(i = 0; i < MAXSTACKTRACE; i++) |
---|
24 | { |
---|
25 | if(stacktrace[i].thread == threadid) |
---|
26 | { |
---|
27 | stacktrace[i].func[stacktrace[i].pos] = this_fn; |
---|
28 | stacktrace[i].pos++; |
---|
29 | if(stacktrace[i].pos >= MAXSTACKTRACEFUNC) stacktrace[i].pos = MAXSTACKTRACEFUNC - 1; |
---|
30 | return; |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | for(i = 0; i < MAXSTACKTRACE; i++) |
---|
35 | { |
---|
36 | if(stacktrace[i].pos == 0) |
---|
37 | { |
---|
38 | stacktrace[i].thread = threadid; |
---|
39 | stacktrace[i].func[stacktrace[i].pos] = this_fn; |
---|
40 | stacktrace[i].pos++; |
---|
41 | if(stacktrace[i].pos >= MAXSTACKTRACEFUNC) stacktrace[i].pos = MAXSTACKTRACEFUNC - 1; |
---|
42 | return; |
---|
43 | } |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | void __cyg_profile_func_exit(void *this_fn, void* call_size) __attribute__((no_instrument_function)); |
---|
48 | void __cyg_profile_func_exit(void *this_fn, void* call_size) |
---|
49 | { |
---|
50 | int i = 0; |
---|
51 | pthread_t threadid = pthread_self(); |
---|
52 | |
---|
53 | for(i = 0; i < MAXSTACKTRACE; i++) |
---|
54 | { |
---|
55 | if(stacktrace[i].thread == threadid) |
---|
56 | { |
---|
57 | if(stacktrace[i].pos > 0) stacktrace[i].pos--; |
---|
58 | return; |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|
62 | #endif |
---|
63 | |
---|
64 | //debug |
---|
65 | //first line shows greater/same debuglevel |
---|
66 | //second line shows only same debuglevel |
---|
67 | //#define debug(level, fmt, args...) if(debug_level >= level) { do { printf("[%s] " fmt, PROGNAME, ##args); } while (0); printf(", file=%s, func=%s, line=%d\n", __FILE__, __FUNCTION__, __LINE__); } |
---|
68 | //#define debug(level, fmt, args...) if(debug_level == level) { do { printf("[%s] " fmt, PROGNAME, ##args); } while (0); printf(", file=%s, func=%s, line=%d\n", __FILE__, __FUNCTION__, __LINE__); } |
---|
69 | void debugfunc(int level, char* file, const char* function, int line, char* msg, ...) |
---|
70 | { |
---|
71 | if(debug_level == level) |
---|
72 | { |
---|
73 | va_list ap; |
---|
74 | va_start(ap, msg); |
---|
75 | printf("[%s] ", PROGNAME); |
---|
76 | vfprintf(stdout, msg, ap); |
---|
77 | va_end(ap); |
---|
78 | fprintf(stdout, ", file=%s, func=%s, line=%d\n", file, function, line); |
---|
79 | } |
---|
80 | } |
---|
81 | #define debug(level, msg...) debugfunc(level, __FILE__, __FUNCTION__, __LINE__, msg); |
---|
82 | |
---|
83 | //err |
---|
84 | //#define err(fmt, args...) { do { fprintf(stderr, "[%s] error: " fmt, PROGNAME, ##args); } while (0); fprintf(stderr, ", file=%s, func=%s, line=%d\n", __FILE__, __FUNCTION__, __LINE__); } |
---|
85 | void errfunc(char* file, const char* function, int line, char* msg, ...) |
---|
86 | { |
---|
87 | va_list ap; |
---|
88 | va_start(ap, msg); |
---|
89 | fprintf(stderr, "[%s] error: ", PROGNAME); |
---|
90 | vfprintf(stderr, msg, ap); |
---|
91 | va_end(ap); |
---|
92 | fprintf(stderr, ", file=%s, func=%s, line=%d\n", file, function, line); |
---|
93 | } |
---|
94 | #define err(msg...) errfunc(__FILE__, __FUNCTION__, __LINE__, msg); |
---|
95 | |
---|
96 | //perr |
---|
97 | //#define perr(fmt, args...) { do { fprintf(stderr, "[%s] error: " fmt, PROGNAME, ##args); } while (0); fprintf(stderr, ", err=%m, file=%s, func=%s, line=%d\n", __FILE__, __FUNCTION__, __LINE__); } |
---|
98 | void perrfunc(char* file, const char* function, int line, char* msg, ...) |
---|
99 | { |
---|
100 | va_list ap; |
---|
101 | va_start(ap, msg); |
---|
102 | fprintf(stderr, "[%s] error: ", PROGNAME); |
---|
103 | vfprintf(stderr, msg, ap); |
---|
104 | va_end(ap); |
---|
105 | fprintf(stderr, ", err=%m, file=%s, func=%s, line=%d\n", file, function, line); |
---|
106 | } |
---|
107 | #define perr(msg...) perrfunc(__FILE__, __FUNCTION__, __LINE__, msg); |
---|
108 | |
---|
109 | //filedebug |
---|
110 | //#define filedebug(file, fmt, args...) { FILE* fd = fopen(file, "a"); if(fd != NULL) { do { fprintf(fd, "" fmt, ##args); } while (0); fprintf(fd, "\n"); }} |
---|
111 | void filedebugfunc(char* file, char* msg, ...) |
---|
112 | { |
---|
113 | FILE* fd = fopen(file, "a"); |
---|
114 | if(fd != NULL) |
---|
115 | { |
---|
116 | va_list ap; |
---|
117 | va_start(ap, msg); |
---|
118 | vfprintf(fd, msg, ap); |
---|
119 | va_end(ap); |
---|
120 | fprintf(fd, "\n"); |
---|
121 | fclose(fd); |
---|
122 | } |
---|
123 | } |
---|
124 | #define filedebug(file, msg...) filedebugfunc(file, msg); |
---|
125 | |
---|
126 | #endif |
---|