improve message printing from redirection module

This commit is contained in:
daan 2019-08-11 16:38:58 -07:00
parent 0c912445c4
commit f35c2c5201
3 changed files with 26 additions and 14 deletions

View file

@ -22,6 +22,7 @@ terms of the MIT license. A copy of the license can be found in the file
// "options.c" // "options.c"
void _mi_fputs(FILE* out, const char* prefix, const char* message);
void _mi_fprintf(FILE* out, const char* fmt, ...); void _mi_fprintf(FILE* out, const char* fmt, ...);
void _mi_error_message(const char* fmt, ...); void _mi_error_message(const char* fmt, ...);
void _mi_warning_message(const char* fmt, ...); void _mi_warning_message(const char* fmt, ...);

View file

@ -421,7 +421,9 @@ static void mi_process_load(void) {
// show message from the redirector (if present) // show message from the redirector (if present)
const char* msg = NULL; const char* msg = NULL;
mi_allocator_init(&msg); mi_allocator_init(&msg);
if (msg != NULL) _mi_verbose_message(msg); if (msg != NULL && (mi_option_is_enabled(mi_option_verbose) || mi_option_is_enabled(mi_option_show_errors))) {
_mi_fputs(stderr,NULL,msg);
}
} }
// Initialize the process; called by thread_init or the process loader // Initialize the process; called by thread_init or the process loader

View file

@ -115,32 +115,41 @@ static uintptr_t error_count = 0; // when MAX_ERROR_COUNT stop emitting errors
// inside the C runtime causes another message. // inside the C runtime causes another message.
static mi_decl_thread bool recurse = false; static mi_decl_thread bool recurse = false;
// Define our own limited `fprintf` that avoids memory allocation.
// We do this using `snprintf` with a limited buffer. void _mi_fputs(FILE* out, const char* prefix, const char* message) {
static void mi_vfprintf( FILE* out, const char* prefix, const char* fmt, va_list args ) { if (out==NULL) out = stdout;
char buf[256];
if (fmt==NULL) return;
if (_mi_preloading() || recurse) return; if (_mi_preloading() || recurse) return;
recurse = true; recurse = true;
if (out==NULL) out = stdout;
vsnprintf(buf,sizeof(buf)-1,fmt,args);
#ifdef _WIN32 #ifdef _WIN32
// on windows with redirection, the C runtime cannot handle locale dependent output // on windows with redirection, the C runtime cannot handle locale dependent output
// after the main thread closes so use direct console output. // after the main thread closes so use direct console output.
if (out==stderr) { if (out==stderr) {
if (prefix != NULL) _cputs(prefix); if (prefix != NULL) _cputs(prefix);
_cputs(buf); _cputs(message);
} }
else else
#endif #endif
{ {
if (prefix != NULL) fputs(prefix,out); if (prefix != NULL) fputs(prefix, out);
fputs(buf,out); fputs(message, out);
} }
recurse = false; recurse = false;
return; return;
} }
// Define our own limited `fprintf` that avoids memory allocation.
// We do this using `snprintf` with a limited buffer.
static void mi_vfprintf( FILE* out, const char* prefix, const char* fmt, va_list args ) {
char buf[512];
if (fmt==NULL) return;
if (_mi_preloading() || recurse) return;
recurse = true;
vsnprintf(buf,sizeof(buf)-1,fmt,args);
recurse = false;
_mi_fputs(out,prefix,buf);
}
void _mi_fprintf( FILE* out, const char* fmt, ... ) { void _mi_fprintf( FILE* out, const char* fmt, ... ) {
va_list args; va_list args;
va_start(args,fmt); va_start(args,fmt);