diff --git a/common.c b/common.c index a387b82ed..579b94b70 100644 --- a/common.c +++ b/common.c @@ -799,16 +799,75 @@ void die_mem() void debug( int level, wchar_t *msg, ... ) { va_list va; - + string_buffer_t sb; + wchar_t *start, *pos; + int line_width = 0; + int tok_width = 0; + int screen_width = 80; + if( level > debug_level ) return; + sb_init( &sb ); va_start( va, msg ); - fwprintf( stderr, L"%ls: ", program_name ); - vfwprintf( stderr, msg, va ); + sb_printf( &sb, L"%ls: ", program_name ); + sb_vprintf( &sb, msg, va ); va_end( va ); - fwprintf( stderr, L"\n" ); + + start = pos = (wchar_t *)sb.buff; + while( 1 ) + { + int overflow = 0; + + tok_width=0; + + while( *pos && ( !wcschr( L" \n\r\t", *pos ) ) ) + { + if((tok_width + wcwidth(*pos)) >= (screen_width-1)) + { + overflow = 1; + break; + + } + + tok_width += wcwidth( *pos ); + pos++; + } + + if( pos == start ) + { + start = pos = pos+1; + } + else if( overflow ) + { + wchar_t *token = wcsndup( start, pos-start ); + if( line_width != 0 ) + putwc( L'\n', stderr ); + fwprintf( stderr, L"%ls-\n", token ); + free( token ); + line_width=0; + } + else + { + wchar_t *token = wcsndup( start, pos-start ); + if( (line_width + (line_width!=0?1:0) + tok_width) > screen_width ) + { + putwc( L'\n', stderr ); + line_width=0; + } + fwprintf( stderr, L"%ls%ls", line_width?L" ":L"", token ); + free( token ); + line_width += (line_width!=0?1:0) + tok_width; + } + + if( !*pos ) + break; + + start=pos; + } + putwc( L'\n', stderr ); + } wchar_t *escape( const wchar_t *in, diff --git a/util.c b/util.c index 14e841bf2..7eb73ec31 100644 --- a/util.c +++ b/util.c @@ -872,6 +872,13 @@ void sb_append2( string_buffer_t *b, ... ) int sb_printf( string_buffer_t *buffer, const wchar_t *format, ... ) { va_list va; + va_start( va, format ); + sb_vprintf( buffer, format, va ); + va_end( va ); +} + +int sb_vprintf( string_buffer_t *buffer, const wchar_t *format, va_list va_orig ) +{ int res; if( !buffer->length ) @@ -885,13 +892,16 @@ int sb_printf( string_buffer_t *buffer, const wchar_t *format, ... ) while( 1 ) { + va_list va; + va_copy( va, va_orig ); - va_start( va, format ); res = vswprintf( (wchar_t *)((char *)buffer->buff+buffer->used), (buffer->length-buffer->used)/sizeof(wchar_t), format, va ); + + va_end( va ); if( res >= 0 ) { buffer->used+= res*sizeof(wchar_t); @@ -919,7 +929,6 @@ int sb_printf( string_buffer_t *buffer, const wchar_t *format, ... ) die_mem(); buffer->length *= 2; } - va_end( va ); return res; } diff --git a/util.h b/util.h index f498811ea..7bbd39708 100644 --- a/util.h +++ b/util.h @@ -6,6 +6,7 @@ #define FISH_UTIL_H #include +#include /** Data structure for an automatically resizing dynamically allocated queue, @@ -442,6 +443,10 @@ void sb_append2( string_buffer_t *, ... ); */ int sb_printf( string_buffer_t *buffer, const wchar_t *format, ... ); +/** + Vararg version of sb_printf +*/ +int sb_vprintf( string_buffer_t *buffer, const wchar_t *format, va_list va_orig ); /** Destroy the buffer and free it's memory