Variant of C API `error`/`logToConsole` taking variadic parameter list

Hello! I've been experimenting with the C SDK, and have found a minor gap in the API.

The system API implements two useful functions, error and logToConsole, both of which take a format string, and a variadic set of arguments. I believe it would be useful to provide two additional variants...

verror(const char* fmt, va_list args)
vlogToConsole(const char* fmt, va_list args)

1. Describe your reason for requesting this feature. What problems are you running into?
I'm writing a function to parse large files, and will be emitting errors from many code-paths. When the parser encounters an issue, there is some cleanup logic I would like to perform before returning. To me, it seems intuitive to create an int emitParseError(ParseState *state, const char* fmt, ...) function which handles both logging the error to the system, and cleaning up the state of the parser.

This is currently not possible, as there's no way to provide the arguments from my emitParseError to the system error function. The C standard library typically implements alternative versions of variadic functions which take the parameter list directly, for example vprintf, allowing them to be called from other variadic functions, however the PlayDate API does not currently provide this functionality.

2. How would this request improve your experience developing for Playdate?
I recognize that this is an extremely nitpicky request, but it would bring PlayDate's system log a bit closer to the interface C programmers are used to with printf, and would allow a greater degree of flexibility when it comes to logging.

3. Include any other details you have relating to this request.
Variadic argument lists can be finicky and prone to crashes if used incorrectly, so I understand if this doesn't align with the goals of the SDK! I love how friendly the PlayDate is both inside and out, and if this suggestion risks making the C API more confusing, then I can find a different way to write my code :grin:

For sake of API simplicity I'm leaning towards adding just a vasprintf() wrapper so that you can turn the va_list into a string yourself and pass it to error/logToConsole.

pd->system->vaFormatString(const char* fmt, va_list args)

That saves us from having to add separate varargs+va_list versions of any other variadic functions that might pop up in the future, and also lets you make your own printf-style variadic functions for other uses. Would that be enough?

1 Like