source: titan/titan/debug.h @ 43450

Last change on this file since 43450 was 24498, checked in by nit, 10 years ago

[titan] add fileerr and fileperr

File size: 4.8 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
16#ifdef BETA
17void __cyg_profile_func_enter(void *this_fn, void* call_size) __attribute__((no_instrument_function));
18void __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
47void __cyg_profile_func_exit(void *this_fn, void* call_size) __attribute__((no_instrument_function));
48void __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__); }
69void 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__); }
85void 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__); }
98void 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"); }}
111void 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//fileerr
127void fileerrfunc(char* file, char* file1, const char* function, int line, char* msg, ...)
128{
129        FILE* fd = fopen(file, "a");
130        if(fd != NULL)
131        {
132                va_list ap;
133                va_start(ap, msg);
134                fprintf(fd, "[%s] error: ", PROGNAME);
135                vfprintf(fd, msg, ap);
136                va_end(ap);
137                fprintf(fd, ", file=%s, func=%s, line=%d\n", file1, function, line);   
138        }
139}
140#define fileerr(file, msg...) fileerrfunc(file, __FILE__, __FUNCTION__, __LINE__, msg);
141
142//fileperr
143void fileperrfunc(char* file, char* file1, const char* function, int line, char* msg, ...)
144{
145        FILE* fd = fopen(file, "a");
146        if(fd != NULL)
147        {
148                va_list ap;
149                va_start(ap, msg);
150                fprintf(fd, "[%s] error: ", PROGNAME);
151                vfprintf(fd, msg, ap);
152                va_end(ap);
153                fprintf(fd, ", err=%m, file=%s, func=%s, line=%d\n", file1, function, line);
154        }
155}
156#define fileperr(file, msg...) fileperrfunc(file, __FILE__, __FUNCTION__, __LINE__, msg);
157
158#endif
Note: See TracBrowser for help on using the repository browser.