2019-05-12 21:17:24 -05:00
// 12 may 2019
# include "uipriv.h"
2019-05-26 14:41:22 -05:00
# include "testhooks.h"
2019-05-12 21:17:24 -05:00
2020-02-09 17:27:19 -06:00
# define maxErrorBuf 256
# define conststrlen(s) ((sizeof (s) / sizeof (char)) - 1)
# define errorBufSize(prefix, suffix) (conststrlen(prefix) + maxErrorBuf + conststrlen(suffix) + 1)
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."
2020-02-09 17:27:19 -06:00
# define internalErrorBufSize errorBufSize(internalErrorPrefix, internalErrorSuffix)
2019-05-13 20:30:18 -05:00
void uiprivInternalError ( const char * fmt , . . . )
{
va_list ap ;
2020-02-09 17:27:19 -06:00
char buf [ internalErrorBufSize ] ;
2019-05-13 20:30:18 -05:00
int n ;
va_start ( ap , fmt ) ;
2020-02-09 17:27:19 -06:00
n = uiprivVsnprintf ( buf , internalErrorBufSize , fmt , ap ) ;
2019-05-13 20:30:18 -05:00
va_end ( ap ) ;
if ( n < 0 )
uiprivReportError ( internalErrorPrefix , " internal error string has encoding error " , internalErrorSuffix , true ) ;
2020-02-09 17:27:19 -06:00
if ( n > = internalErrorBufSize )
2019-05-13 20:30:18 -05:00
uiprivReportError ( internalErrorPrefix , " internal error string too long " , internalErrorSuffix , true ) ;
uiprivReportError ( internalErrorPrefix , buf , internalErrorSuffix , true ) ;
}
# 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."
2020-02-09 17:27:19 -06:00
# define programmerErrorBufSize errorBufSize(programmerErrorPrefix, programmerErrorSuffix)
2019-05-13 20:30:18 -05:00
2019-06-09 12:49:53 -05:00
static uiprivTestHookReportProgrammerErrorFunc reportProgrammerErrorTestHook = NULL ;
static void * reportProgrammerErrorTestHookData = NULL ;
2019-05-26 14:41:22 -05:00
2019-06-09 12:49:53 -05:00
void uiprivTestHookReportProgrammerError ( uiprivTestHookReportProgrammerErrorFunc f , void * data )
2019-05-26 14:41:22 -05:00
{
reportProgrammerErrorTestHook = f ;
2019-06-09 12:49:53 -05:00
reportProgrammerErrorTestHookData = data ;
2019-05-26 14:41:22 -05:00
}
2019-06-02 01:23:12 -05:00
void uiprivProgrammerError ( const char * fmt , . . . )
2019-05-12 21:17:24 -05:00
{
va_list ap ;
2020-02-09 17:27:19 -06:00
char buf [ programmerErrorBufSize ] ;
2019-06-02 01:23:12 -05:00
int n ;
2019-05-12 21:17:24 -05:00
2019-06-02 01:23:12 -05:00
va_start ( ap , fmt ) ;
2020-02-09 17:27:19 -06:00
n = uiprivVsnprintf ( buf , programmerErrorBufSize , fmt , ap ) ;
2019-06-02 01:23:12 -05:00
if ( n < 0 )
uiprivInternalError ( " programmer error has encoding error " ) ;
2020-02-09 17:27:19 -06:00
if ( n > = programmerErrorBufSize )
2019-06-02 01:23:12 -05:00
uiprivInternalError ( " programmer error string too long (%d) " , n ) ;
2019-05-12 21:17:24 -05:00
va_end ( ap ) ;
2019-06-09 12:49:53 -05:00
if ( reportProgrammerErrorTestHook ! = NULL ) {
( * reportProgrammerErrorTestHook ) ( buf , reportProgrammerErrorTestHookData ) ;
return ;
}
uiprivReportError ( programmerErrorPrefix , buf , programmerErrorSuffix , false ) ;
2019-05-12 21:17:24 -05:00
}