2019-05-12 21:17:24 -05:00
// 12 may 2019
# include <stdarg.h>
# include <stdio.h>
# include "ui.h"
# include "uipriv.h"
2019-05-26 14:41:22 -05:00
# include "testhooks.h"
2019-05-12 21:17:24 -05:00
2019-05-13 20:30:18 -05:00
# define internalErrorPrefix "libui internal error"
// TODO add debugging advice?
# define internalErrorSuffix "This likely means there is a bug in libui itself. Contact the libui authors."
void uiprivInternalError ( const char * fmt , . . . )
{
va_list ap ;
char buf [ 256 ] ;
int n ;
va_start ( ap , fmt ) ;
n = vsnprintf ( buf , 256 , fmt , ap ) ;
va_end ( ap ) ;
if ( n < 0 )
uiprivReportError ( internalErrorPrefix , " internal error string has encoding error " , internalErrorSuffix , true ) ;
if ( n > = 256 )
uiprivReportError ( internalErrorPrefix , " internal error string too long " , internalErrorSuffix , true ) ;
uiprivReportError ( internalErrorPrefix , buf , internalErrorSuffix , true ) ;
}
2019-05-14 10:02:23 -05:00
static const char * messages [ uiprivNumProgrammerErrors ] = {
2019-05-12 21:17:24 -05:00
[ uiprivProgrammerErrorWrongStructSize ] = " wrong size %zu for %s " ,
[ uiprivProgrammerErrorIndexOutOfRange ] = " index %d out of range in %s() " ,
2019-05-13 05:37:19 -05:00
[ uiprivProgrammerErrorNullPointer ] = " invalid null pointer for %s passed into %s() " ,
[ uiprivProgrammerErrorIntIDNotFound ] = " %s identifier %d not found in %s() " ,
2019-05-16 11:27:04 -05:00
[ uiprivProgrammerErrorBadSenderForEvent ] = " attempt to use a %s sender with a %s event in %s() " ,
2019-05-14 10:02:23 -05:00
[ uiprivProgrammerErrorChangingEventDuringFire ] = " attempt to change a uiEvent with %s() while it is firing " ,
[ uiprivProgrammerErrorRecursiveEventFire ] = " attempt to fire a uiEvent while it is already being fired " ,
2019-05-12 21:17:24 -05:00
} ;
static void prepareProgrammerError ( char * buf , int size , unsigned int which , va_list ap )
{
int n ;
2019-05-13 20:30:18 -05:00
if ( which > = uiprivNumProgrammerErrors )
uiprivInternalError ( " bad programmer error value %u " , which ) ;
2019-05-14 10:02:23 -05:00
if ( messages [ which ] = = NULL )
uiprivInternalError ( " programmer error %u has no message " , which ) ;
2019-05-12 21:17:24 -05:00
n = vsnprintf ( buf , size , messages [ which ] , ap ) ;
2019-05-13 20:30:18 -05:00
if ( n < 0 )
uiprivInternalError ( " programmer error string for %u has encoding error " , which ) ;
if ( n > = size )
uiprivInternalError ( " programmer error string for %u too long (%d) " , which , n ) ;
2019-05-12 21:17:24 -05:00
}
2019-05-13 20:30:18 -05:00
# define programmerErrorPrefix "libui programmer error"
// TODO add debugging advice?
# define programmerErrorSuffix "This likely means you are using libui incorrectly. Check your source code and try again. If you have received this warning in error, contact the libui authors."
2019-05-26 14:41:22 -05:00
static uiprivTestHookReportProgrammerErrorFunc reportProgrammerErrorTestHook = uiprivReportError ;
void uiprivTestHookReportProgrammerError ( uiprivTestHookReportProgrammerErrorFunc f )
{
if ( f = = NULL )
f = uiprivReportError ;
reportProgrammerErrorTestHook = f ;
}
2019-05-12 21:17:24 -05:00
void uiprivProgrammerError ( unsigned int which , . . . )
{
va_list ap ;
char buf [ 256 ] ;
va_start ( ap , which ) ;
prepareProgrammerError ( buf , 256 , which , ap ) ;
va_end ( ap ) ;
2019-05-26 14:41:22 -05:00
( * reportProgrammerErrorTestHook ) ( programmerErrorPrefix , buf , programmerErrorSuffix , false ) ;
2019-05-12 21:17:24 -05:00
}