[cexpr] Add optional error message prefix string

The prefix gives more context to the error messages, making the system a
lot easier to use (it was especially helpful when getting my cvar revamp
into shape).
This commit is contained in:
Bill Currie 2022-04-24 15:16:15 +09:00
parent d68db74049
commit 021ec4962b
2 changed files with 12 additions and 2 deletions

View file

@ -99,6 +99,7 @@ typedef struct exprctx_s {
exprval_t *result;
exprtab_t *symtab; // directly accessible symbols
exprtab_t *external_variables; // accessible via $id
const char *msg_prefix; // optional prefix for error messages
struct memsuper_s *memsuper;
const struct plitem_s *item;
struct plitem_s *messages;

View file

@ -94,9 +94,18 @@ cexpr_error(exprctx_t *ctx, const char *fmt, ...)
va_end (args);
if (ctx->messages) {
PL_Message (ctx->messages, ctx->item, "%s", string->str);
if (ctx->msg_prefix) {
PL_Message (ctx->messages, ctx->item, "%s:%s",
ctx->msg_prefix, string->str);
} else {
PL_Message (ctx->messages, ctx->item, "%s", string->str);
}
} else {
Sys_Printf ("%s\n", string->str);
if (ctx->msg_prefix) {
Sys_Printf ("%s:%s\n", ctx->msg_prefix, string->str);
} else {
Sys_Printf ("%s\n", string->str);
}
}
dstring_delete (string);
}