20 #include <sys/types.h> 31 #pragma warning(disable : 271 310) 38 kmp_bootstrap_lock_t __kmp_stdio_lock = KMP_BOOTSTRAP_LOCK_INITIALIZER(
40 kmp_bootstrap_lock_t __kmp_console_lock = KMP_BOOTSTRAP_LOCK_INITIALIZER(
47 static HANDLE __kmp_stdout = NULL;
49 static HANDLE __kmp_stderr = NULL;
50 static int __kmp_console_exists = FALSE;
51 static kmp_str_buf_t __kmp_console_buf;
53 static int is_console(
void) {
61 rc = GetConsoleTitle(buffer,
sizeof(buffer));
68 return rc > 0 || err == 0;
71 void __kmp_close_console(
void) {
74 if (__kmp_console_exists) {
80 __kmp_str_buf_free(&__kmp_console_buf);
81 __kmp_console_exists = FALSE;
87 static void __kmp_redirect_output(
void) {
88 __kmp_acquire_bootstrap_lock(&__kmp_console_lock);
90 if (!__kmp_console_exists) {
97 __kmp_str_buf_init(&__kmp_console_buf);
106 ho = GetStdHandle(STD_OUTPUT_HANDLE);
107 if (ho == INVALID_HANDLE_VALUE || ho == NULL) {
109 DWORD err = GetLastError();
118 he = GetStdHandle(STD_ERROR_HANDLE);
119 if (he == INVALID_HANDLE_VALUE || he == NULL) {
121 DWORD err = GetLastError();
129 __kmp_console_exists = TRUE;
131 __kmp_release_bootstrap_lock(&__kmp_console_lock);
135 #define __kmp_stderr (stderr) 138 void __kmp_vprintf(
enum kmp_io __kmp_io,
char const *format, va_list ap) {
140 if (!__kmp_console_exists) {
141 __kmp_redirect_output();
143 if (!__kmp_stderr && __kmp_io == kmp_err) {
147 if (!__kmp_stdout && __kmp_io == kmp_out) {
153 if (__kmp_debug_buf && __kmp_debug_buffer != NULL) {
155 int dc = (__kmp_debug_buf_atomic ? KMP_TEST_THEN_INC32(&__kmp_debug_count)
156 : __kmp_debug_count++) %
157 __kmp_debug_buf_lines;
158 char *db = &__kmp_debug_buffer[dc * __kmp_debug_buf_chars];
161 #ifdef KMP_DEBUG_PIDS 162 chars = KMP_SNPRINTF(db, __kmp_debug_buf_chars,
"pid=%d: ",
163 (kmp_int32)getpid());
165 chars += KMP_VSNPRINTF(db, __kmp_debug_buf_chars, format, ap);
167 if (chars + 1 > __kmp_debug_buf_chars) {
168 if (chars + 1 > __kmp_debug_buf_warn_chars) {
171 __kmp_str_buf_print(&__kmp_console_buf,
"OMP warning: Debugging buffer " 172 "overflow; increase " 173 "KMP_DEBUG_BUF_CHARS to %d\n",
175 WriteFile(__kmp_stderr, __kmp_console_buf.str, __kmp_console_buf.used,
177 __kmp_str_buf_clear(&__kmp_console_buf);
179 fprintf(__kmp_stderr,
"OMP warning: Debugging buffer overflow; " 180 "increase KMP_DEBUG_BUF_CHARS to %d\n",
182 fflush(__kmp_stderr);
184 __kmp_debug_buf_warn_chars = chars + 1;
187 db[__kmp_debug_buf_chars - 2] =
'\n';
188 db[__kmp_debug_buf_chars - 1] =
'\0';
193 #ifdef KMP_DEBUG_PIDS 194 __kmp_str_buf_print(&__kmp_console_buf,
"pid=%d: ", (kmp_int32)getpid());
196 __kmp_str_buf_vprint(&__kmp_console_buf, format, ap);
197 WriteFile(__kmp_stderr, __kmp_console_buf.str, __kmp_console_buf.used,
199 __kmp_str_buf_clear(&__kmp_console_buf);
201 #ifdef KMP_DEBUG_PIDS 202 fprintf(__kmp_stderr,
"pid=%d: ", (kmp_int32)getpid());
204 vfprintf(__kmp_stderr, format, ap);
205 fflush(__kmp_stderr);
210 void __kmp_printf(
char const *format, ...) {
212 va_start(ap, format);
214 __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock);
215 __kmp_vprintf(kmp_err, format, ap);
216 __kmp_release_bootstrap_lock(&__kmp_stdio_lock);
221 void __kmp_printf_no_lock(
char const *format, ...) {
223 va_start(ap, format);
225 __kmp_vprintf(kmp_err, format, ap);