Browse Source

placeholder for C API logging, perhaps easiest to log through a representative logger instance

pull/2/head
Sami Vaarala 11 years ago
parent
commit
77a1d14a62
  1. 1
      Makefile
  2. 41
      src/duk_api_logging.c
  3. 5
      src/duk_initjs.js
  4. 1
      util/make_dist.sh

1
Makefile

@ -100,6 +100,7 @@ DUKTAPE_SOURCES_SEPARATE = \
$(DISTSRCSEP)/duk_api_thread.c \
$(DISTSRCSEP)/duk_api_buffer.c \
$(DISTSRCSEP)/duk_api_var.c \
$(DISTSRCSEP)/duk_api_logging.c \
$(DISTSRCSEP)/duk_api.c \
$(DISTSRCSEP)/duk_lexer.c \
$(DISTSRCSEP)/duk_js_call.c \

41
src/duk_api_logging.c

@ -0,0 +1,41 @@
/*
* Logging
*
* Current logging primitive is a sprintf-style log which is convenient
* for most C code. Another useful primitive would be to log N arguments
* from value stack (like the Ecmascript binding does).
*/
#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, ...) {
duk_hthread *thr = (duk_hthread *) ctx;
va_list ap;
char buf[DUK__LOGFMT_BUFSIZE];
va_start(ap, fmt);
DUK_VSNPRINTF(buf, sizeof(buf), fmt, ap);
buf[sizeof(buf) - 1] = (char) 0;
va_end(ap);
duk_push_hobject(ctx, thr->builtins[DUK_BIDX_LOGGER_CONSTRUCTOR]);
duk_get_prop_string(ctx, -1, "clog"); /*FIXME*/
duk_get_prop_string(ctx, -1, "info"); /*FIXME:level*/
/* [ ... Logger clog info ] */
duk_dup(ctx, -2);
duk_push_string(ctx, buf); /*FIXME: duk_push_vsprintf? */
/* [ ... Logger clog info clog msg ] */
duk_call_method(ctx, 1 /*nargs*/);
/* [ ... Logger clog res ] */
duk_pop_3(ctx);
}

5
src/duk_initjs.js

@ -72,4 +72,9 @@
}
};
}
// logger object for C code
if (true) {
D.Logger.clog = new D.Logger('C');
}
})(this, Duktape);

1
util/make_dist.sh

@ -70,6 +70,7 @@ for i in \
duk_api_string.c \
duk_api_thread.c \
duk_api_var.c \
duk_api_logging.c \
duk_bi_array.c \
duk_bi_boolean.c \
duk_bi_buffer.c \

Loading…
Cancel
Save