Browse Source

Make duk_log() support artbirarily long log messages

Internally, use duk_push_vsprintf() for log message formatting, which now
has support for formatting 99% of messages with a stack buffer.
pull/20/head
Sami Vaarala 11 years ago
parent
commit
928341ce7e
  1. 20
      src/duk_api_logging.c

20
src/duk_api_logging.c

@ -8,12 +8,8 @@
#include "duk_internal.h"
/* FIXME: dynamic? shared code with sprintf string pusher? */
#define DUK__LOGFMT_BUFSIZE 256 /* size for formatting buffers */
void duk_log(duk_context *ctx, int level, const char *fmt, ...) {
va_list ap;
char buf[DUK__LOGFMT_BUFSIZE];
/* stridx_logfunc[] must be static to allow initializer with old compilers like BCC */
static const duk_uint16_t stridx_logfunc[6] = {
DUK_STRIDX_LC_TRACE, DUK_STRIDX_LC_DEBUG, DUK_STRIDX_LC_INFO,
@ -26,21 +22,18 @@ void duk_log(duk_context *ctx, int level, const char *fmt, ...) {
level = (int) (sizeof(stridx_logfunc) / sizeof(duk_uint16_t)) - 1;
}
va_start(ap, fmt);
DUK_VSNPRINTF(buf, sizeof(buf), fmt, ap);
buf[sizeof(buf) - 1] = (char) 0;
va_end(ap);
duk_push_hobject_bidx(ctx, DUK_BIDX_LOGGER_CONSTRUCTOR);
duk_get_prop_stridx(ctx, -1, DUK_STRIDX_CLOG);
duk_get_prop_stridx(ctx, -1, stridx_logfunc[level]);
duk_dup(ctx, -2);
/* [ ... Logger clog info ] */
/* [ ... Logger clog logfunc clog ] */
duk_dup(ctx, -2);
duk_push_string(ctx, buf); /* FIXME: duk_push_vsprintf? */
va_start(ap, fmt);
duk_push_vsprintf(ctx, fmt, ap);
va_end(ap);
/* [ ... Logger clog info clog msg ] */
/* [ ... Logger clog logfunc clog(=this) msg ] */
duk_call_method(ctx, 1 /*nargs*/);
@ -48,4 +41,3 @@ void duk_log(duk_context *ctx, int level, const char *fmt, ...) {
duk_pop_3(ctx);
}

Loading…
Cancel
Save