source: titan/titan/debug.h @ 23196

Last change on this file since 23196 was 23196, checked in by nit, 11 years ago

[titan] optimize

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