wmclockmon: update change-log
[dockapps.git] / wmcliphist / debug.c
blob2ef8b8bf026afbca06e74422aa01fdf7da6fe2eb
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include <unistd.h>
9 /* tabulators */
10 char tabs[1024];
13 #ifdef DBG_THREADS
14 #include <pthread.h>
16 static pthread_key_t align_key;
17 static pthread_once_t align_key_once = PTHREAD_ONCE_INIT;
20 static void
21 align_destroy(void *align)
23 free(align);
27 static void
28 align_key_create()
30 pthread_key_create(&align_key, align_destroy);
34 void
35 debug_init_threads()
37 int *new_align;
39 memset(tabs, 9, 1024);
41 if (!(new_align = malloc(sizeof(int))))
42 abort();
43 *new_align = 1;
45 pthread_once(&align_key_once, align_key_create);
47 pthread_setspecific(align_key, new_align);
49 #endif
53 * no threads
56 static int main_align = 1;
58 void
59 debug_init_nothreads()
61 memset(tabs, 9, 1024);
67 void
68 fn_begin(char *format, ...)
70 va_list args;
71 char msg_buf[1024];
72 char final_msg[2048];
73 int *align;
75 #ifdef DBG_THREADS
76 align = pthread_getspecific(align_key);
77 #else
78 align = &main_align;
79 #endif
81 va_start(args, format);
82 vsprintf(msg_buf, format, args);
83 va_end(args);
85 strcat(msg_buf, " {\n");
87 sprintf(final_msg, "%d: %.*s%s", getpid(), *align, tabs, msg_buf);
88 fprintf(stderr, "%s", final_msg);
89 (*align)++;
93 void
94 fn_end()
96 int *align;
98 #ifdef DBG_THREADS
99 align = pthread_getspecific(align_key);
100 #else
101 align = &main_align;
102 #endif
104 (*align)--;
106 fprintf(stderr, "%d: %.*s}\n", getpid(), *align, tabs);
113 void
114 fn2(int i)
116 _D(fn_begin("fn2(i = %d)", i));
117 return_void();
121 fn1()
123 _D(fn_begin("fn1"));
124 fn2(1);
125 return_val(0);
129 main()
131 _D(fn_begin("main"));
132 fn1();
133 fn2(2);
134 return_val(0);