1 #include <stdio.h>
2 #include "duktape.h"
3 #include "duk_logging.h"
4 
init_logging(duk_context * ctx,void * udata)5 static duk_ret_t init_logging(duk_context *ctx, void *udata) {
6 	(void) udata;
7 	duk_logging_init(ctx, 0 /*flags*/);
8 	printf("top after init: %ld\n", (long) duk_get_top(ctx));
9 
10 	/* C API test */
11 	duk_eval_string_noresult(ctx, "Duktape.Logger.clog.l = 0;");
12 	duk_log(ctx, DUK_LOG_TRACE, "c logger test: %d", 123);
13 	duk_log(ctx, DUK_LOG_DEBUG, "c logger test: %d", 123);
14 	duk_log(ctx, DUK_LOG_INFO, "c logger test: %d", 123);
15 	duk_log(ctx, DUK_LOG_WARN, "c logger test: %d", 123);
16 	duk_log(ctx, DUK_LOG_ERROR, "c logger test: %d", 123);
17 	duk_log(ctx, DUK_LOG_FATAL, "c logger test: %d", 123);
18 	duk_log(ctx, -1, "negative level: %s %d 0x%08lx", "arg string", -123, 0xdeadbeefUL);
19 	duk_log(ctx, 6, "level too large: %s %d 0x%08lx", "arg string", 123, 0x1234abcdUL);
20 
21 	return 0;
22 }
23 
main(int argc,char * argv[])24 int main(int argc, char *argv[]) {
25 	duk_context *ctx;
26 	int i;
27 	int exitcode = 0;
28 
29 	ctx = duk_create_heap_default();
30 	if (!ctx) {
31 		return 1;
32 	}
33 
34 	(void) duk_safe_call(ctx, init_logging, NULL, 0, 1);
35 	printf("logging init: %s\n", duk_safe_to_string(ctx, -1));
36 	duk_pop(ctx);
37 
38 	for (i = 1; i < argc; i++) {
39 		printf("Evaling: %s\n", argv[i]);
40 		duk_push_string(ctx, argv[i]);
41 		duk_push_string(ctx, "evalCodeFileName");  /* for automatic logger name testing */
42 		if (duk_pcompile(ctx, DUK_COMPILE_EVAL) != 0) {
43 			exitcode = 1;
44 		} else {
45 			if (duk_pcall(ctx, 0) != 0) {
46 				exitcode = 1;
47 			}
48 		}
49 		printf("--> %s\n", duk_safe_to_string(ctx, -1));
50 		duk_pop(ctx);
51 	}
52 
53 	printf("Done\n");
54 	duk_destroy_heap(ctx);
55 	return exitcode;
56 }
57