Do automatic line breaking in debug

darcs-hash:20051012103421-ac50b-d3cba7cb40287fd97cccc246a19dcb2273bf8b82.gz
This commit is contained in:
axel 2005-10-12 20:34:21 +10:00
parent b90e670d6f
commit 4636b783a0
3 changed files with 79 additions and 6 deletions

View File

@ -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,

13
util.c
View File

@ -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;
}

5
util.h
View File

@ -6,6 +6,7 @@
#define FISH_UTIL_H
#include <wchar.h>
#include <stdarg.h>
/**
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