1 /*
2  *  Debugging macro calls.
3  */
4 
5 #include "duk_internal.h"
6 
7 #if defined(DUK_USE_DEBUG)
8 
9 /*
10  *  Debugging enabled
11  */
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdarg.h>
16 
17 #if !defined(DUK_USE_DEBUG_WRITE)
18 #error debugging enabled (DUK_USE_DEBUG) but DUK_USE_DEBUG_WRITE not defined
19 #endif
20 
21 #define DUK__DEBUG_BUFSIZE  DUK_USE_DEBUG_BUFSIZE
22 
23 #if defined(DUK_USE_VARIADIC_MACROS)
24 
duk_debug_log(duk_int_t level,const char * file,duk_int_t line,const char * func,const char * fmt,...)25 DUK_INTERNAL void duk_debug_log(duk_int_t level, const char *file, duk_int_t line, const char *func, const char *fmt, ...) {
26 	va_list ap;
27 	long arg_level;
28 	const char *arg_file;
29 	long arg_line;
30 	const char *arg_func;
31 	const char *arg_msg;
32 	char buf[DUK__DEBUG_BUFSIZE];
33 
34 	va_start(ap, fmt);
35 
36 	duk_memzero((void *) buf, (size_t) DUK__DEBUG_BUFSIZE);
37 	duk_debug_vsnprintf(buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap);
38 
39 	arg_level = (long) level;
40 	arg_file = (const char *) file;
41 	arg_line = (long) line;
42 	arg_func = (const char *) func;
43 	arg_msg = (const char *) buf;
44 	DUK_USE_DEBUG_WRITE(arg_level, arg_file, arg_line, arg_func, arg_msg);
45 
46 	va_end(ap);
47 }
48 
49 #else  /* DUK_USE_VARIADIC_MACROS */
50 
51 DUK_INTERNAL char duk_debug_file_stash[DUK_DEBUG_STASH_SIZE];
52 DUK_INTERNAL duk_int_t duk_debug_line_stash;
53 DUK_INTERNAL char duk_debug_func_stash[DUK_DEBUG_STASH_SIZE];
54 DUK_INTERNAL duk_int_t duk_debug_level_stash;
55 
duk_debug_log(const char * fmt,...)56 DUK_INTERNAL void duk_debug_log(const char *fmt, ...) {
57 	va_list ap;
58 	long arg_level;
59 	const char *arg_file;
60 	long arg_line;
61 	const char *arg_func;
62 	const char *arg_msg;
63 	char buf[DUK__DEBUG_BUFSIZE];
64 
65 	va_start(ap, fmt);
66 
67 	duk_memzero((void *) buf, (size_t) DUK__DEBUG_BUFSIZE);
68 	duk_debug_vsnprintf(buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap);
69 
70 	arg_level = (long) duk_debug_level_stash;
71 	arg_file = (const char *) duk_debug_file_stash;
72 	arg_line = (long) duk_debug_line_stash;
73 	arg_func = (const char *) duk_debug_func_stash;
74 	arg_msg = (const char *) buf;
75 	DUK_USE_DEBUG_WRITE(arg_level, arg_file, arg_line, arg_func, arg_msg);
76 
77 	va_end(ap);
78 }
79 
80 #endif  /* DUK_USE_VARIADIC_MACROS */
81 
82 #else  /* DUK_USE_DEBUG */
83 
84 /*
85  *  Debugging disabled
86  */
87 
88 #endif  /* DUK_USE_DEBUG */
89