Changed uiprivProgrammerError() to use format strings directly, so errors can be more easily caught. We'll make things even more robust when I change the programmer error macros to be functions.

This commit is contained in:
Pietro Gagliardi 2019-06-02 02:23:12 -04:00
parent 671c2031bf
commit 623575f25a
2 changed files with 22 additions and 45 deletions

View File

@ -22,33 +22,6 @@ void uiprivInternalError(const char *fmt, ...)
uiprivReportError(internalErrorPrefix, buf, internalErrorSuffix, true);
}
static const char *messages[uiprivNumProgrammerErrors] = {
[uiprivProgrammerErrorNotInitialized] = "attempt to call %s() before uiInit()",
[uiprivProgrammerErrorWrongThread] = "attempt to call %s() on a thread other than the GUI thread",
[uiprivProgrammerErrorWrongStructSize] = "wrong size %" uiprivSizetPrintf " for %s",
[uiprivProgrammerErrorIndexOutOfRange] = "index %d out of range in %s()",
[uiprivProgrammerErrorNullPointer] = "invalid null pointer for %s passed into %s()",
[uiprivProgrammerErrorIntIDNotFound] = "%s identifier %d not found in %s()",
[uiprivProgrammerErrorBadSenderForEvent] = "attempt to use a %s sender with a %s event in %s()",
[uiprivProgrammerErrorChangingEventDuringFire] = "attempt to change a uiEvent with %s() while it is firing",
[uiprivProgrammerErrorRecursiveEventFire] = "attempt to fire a uiEvent while it is already being fired",
};
static void prepareProgrammerError(char *buf, int size, unsigned int which, va_list ap)
{
int n;
if (which >= uiprivNumProgrammerErrors)
uiprivInternalError("bad programmer error value %u", which);
if (messages[which] == NULL)
uiprivInternalError("programmer error %u has no message", which);
n = uiprivVsnprintf(buf, size, messages[which], ap);
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);
}
#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."
@ -62,13 +35,18 @@ void uiprivTestHookReportProgrammerError(uiprivTestHookReportProgrammerErrorFunc
reportProgrammerErrorTestHook = f;
}
void uiprivProgrammerError(unsigned int which, ...)
void uiprivProgrammerError(const char *fmt, ...)
{
va_list ap;
int n;
char buf[256];
va_start(ap, which);
prepareProgrammerError(buf, 256, which, ap);
va_start(ap, fmt);
n = uiprivVsnprintf(buf, 256, fmt, ap);
if (n < 0)
uiprivInternalError("programmer error has encoding error");
if (n >= 256)
uiprivInternalError("programmer error string too long (%d)", n);
va_end(ap);
(*reportProgrammerErrorTestHook)(programmerErrorPrefix, buf, programmerErrorSuffix, false);
}

View File

@ -60,21 +60,20 @@ extern bool uiprivSysCheckThread(void);
uiprivPrintfFunc(
extern void uiprivInternalError(const char *fmt, ...),
1, 2);
enum {
uiprivProgrammerErrorNotInitialized, // arguments: uiprivFunc
uiprivProgrammerErrorWrongThread, // arguments: uiprivFunc
uiprivProgrammerErrorWrongStructSize, // arguments: size_t badSize, const char *structName
uiprivProgrammerErrorIndexOutOfRange, // arguments: int badIndex, uiprivFunc
uiprivProgrammerErrorNullPointer, // arguments: const char *paramDesc, uiprivFunc
uiprivProgrammerErrorIntIDNotFound, // arguments: const char *idDesc, int badID, uiprivFunc
// TODO type mismatch
uiprivProgrammerErrorBadSenderForEvent, // arguments: const char *senderDesc, const char *eventDesc, uiprivFunc
uiprivProgrammerErrorChangingEventDuringFire, // arguments: uiprivFunc
uiprivProgrammerErrorRecursiveEventFire, // no arguments
uiprivNumProgrammerErrors,
};
// TODO drop the enum and make the above all format strings
extern void uiprivProgrammerError(unsigned int which, ...);
// TODO turn each of these into function-like macros
#define uiprivProgrammerErrorNotInitialized "attempt to call %s() before uiInit()" // arguments: uiprivFunc
#define uiprivProgrammerErrorWrongThread "attempt to call %s() on a thread other than the GUI thread" // arguments: uiprivFunc
#define uiprivProgrammerErrorWrongStructSize "wrong size %" uiprivSizetPrintf " for %s" // arguments: size_t badSize, const char *structName
#define uiprivProgrammerErrorIndexOutOfRange "index %d out of range in %s()" // arguments: int badIndex, uiprivFunc
#define uiprivProgrammerErrorNullPointer "invalid null pointer for %s passed into %s()" // arguments: const char *paramDesc, uiprivFunc
#define uiprivProgrammerErrorIntIDNotFound "%s identifier %d not found in %s()" // arguments: const char *idDesc, int badID, uiprivFunc
// TODO type mismatch
#define uiprivProgrammerErrorBadSenderForEvent "attempt to use a %s sender with a %s event in %s()" // arguments: const char *senderDesc, const char *eventDesc, uiprivFunc
#define uiprivProgrammerErrorChangingEventDuringFire "attempt to change a uiEvent with %s() while it is firing" // arguments: uiprivFunc
#define uiprivProgrammerErrorRecursiveEventFire "attempt to fire a uiEvent while it is already being fired" // no arguments
uiprivPrintfFunc(
extern void uiprivProgrammerError(const char *fmt, ...),
1, 2);
extern void uiprivReportError(const char *prefix, const char *msg, const char *suffix, bool internal);
#ifdef __cplusplus