forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduk_api_logging.c
More file actions
43 lines (33 loc) · 1.21 KB
/
Copy pathduk_api_logging.c
File metadata and controls
43 lines (33 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* 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"
DUK_EXTERNAL void duk_log(duk_context *ctx, duk_int_t level, const char *fmt, ...) {
va_list ap;
/* 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,
DUK_STRIDX_LC_WARN, DUK_STRIDX_LC_ERROR, DUK_STRIDX_LC_FATAL
};
if (level < 0) {
level = 0;
} else if (level > (int) (sizeof(stridx_logfunc) / sizeof(duk_uint16_t)) - 1) {
level = (int) (sizeof(stridx_logfunc) / sizeof(duk_uint16_t)) - 1;
}
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 logfunc clog ] */
va_start(ap, fmt);
duk_push_vsprintf(ctx, fmt, ap);
va_end(ap);
/* [ ... Logger clog logfunc clog(=this) msg ] */
duk_call_method(ctx, 1 /*nargs*/);
/* [ ... Logger clog res ] */
duk_pop_3(ctx);
}