2 * Copyright 2003-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include "DebugSupport.h"
21 \brief Defines debug output function with printf() signature printing
24 \note The initialization is not thread safe!
29 static int32 init_counter
= 0;
30 static sem_id dbg_printf_sem
= -1;
31 static thread_id dbg_printf_thread
= -1;
32 static int dbg_printf_nesting
= 0;
43 status_t error
= B_OK
;
44 if (init_counter
++ == 0) {
47 out
= open(DEBUG_PRINT_FILE
, O_RDWR
| O_CREAT
| O_TRUNC
);
53 // allocate the semaphore
55 dbg_printf_sem
= create_sem(1, "dbg_printf");
56 if (dbg_printf_sem
< 0)
57 error
= dbg_printf_sem
;
61 __out("##################################################\n");
73 status_t error
= B_OK
;
74 if (--init_counter
== 0) {
79 delete_sem(dbg_printf_sem
);
89 thread_id thread
= find_thread(NULL
);
90 if (thread
!= dbg_printf_thread
) {
91 if (acquire_sem(dbg_printf_sem
) != B_OK
)
93 dbg_printf_thread
= thread
;
103 thread_id thread
= find_thread(NULL
);
104 if (thread
!= dbg_printf_thread
)
106 dbg_printf_nesting
--;
107 if (dbg_printf_nesting
== 0) {
108 dbg_printf_thread
= -1;
109 release_sem(dbg_printf_sem
);
131 dbg_printf(const char *format
,...)
133 if (!dbg_printf_lock())
137 va_start(args
, format
);
138 // no vsnprintf() on PPC and in kernel
139 #if defined(__INTEL__) && USER
140 vsnprintf(buffer
, sizeof(buffer
) - 1, format
, args
);
142 vsprintf(buffer
, format
, args
);
145 buffer
[sizeof(buffer
) - 1] = '\0';
146 write(out
, buffer
, strlen(buffer
));
150 #endif // DEBUG_PRINT