2 * Debugging macro calls.
5 #include "duk_internal.h"
17 #define DUK__DEBUG_BUFSIZE DUK_USE_DEBUG_BUFSIZE
18 DUK_LOCAL
char duk__debug_buf
[DUK__DEBUG_BUFSIZE
];
20 DUK_LOCAL
const char *duk__get_level_string(duk_small_int_t level
) {
21 switch ((int) level
) {
24 case DUK_LEVEL_DDEBUG
:
26 case DUK_LEVEL_DDDEBUG
:
32 #ifdef DUK_USE_DPRINT_COLORS
34 /* http://en.wikipedia.org/wiki/ANSI_escape_code */
35 #define DUK__TERM_REVERSE "\x1b[7m"
36 #define DUK__TERM_BRIGHT "\x1b[1m"
37 #define DUK__TERM_RESET "\x1b[0m"
38 #define DUK__TERM_BLUE "\x1b[34m"
39 #define DUK__TERM_RED "\x1b[31m"
41 DUK_LOCAL
const char *duk__get_term_1(duk_small_int_t level
) {
43 return (const char *) DUK__TERM_RED
;
46 DUK_LOCAL
const char *duk__get_term_2(duk_small_int_t level
) {
47 switch ((int) level
) {
49 return (const char *) (DUK__TERM_RESET DUK__TERM_BRIGHT
);
50 case DUK_LEVEL_DDEBUG
:
51 return (const char *) (DUK__TERM_RESET
);
52 case DUK_LEVEL_DDDEBUG
:
53 return (const char *) (DUK__TERM_RESET DUK__TERM_BLUE
);
55 return (const char *) DUK__TERM_RESET
;
58 DUK_LOCAL
const char *duk__get_term_3(duk_small_int_t level
) {
60 return (const char *) DUK__TERM_RESET
;
65 DUK_LOCAL
const char *duk__get_term_1(duk_small_int_t level
) {
67 return (const char *) "";
70 DUK_LOCAL
const char *duk__get_term_2(duk_small_int_t level
) {
72 return (const char *) "";
75 DUK_LOCAL
const char *duk__get_term_3(duk_small_int_t level
) {
77 return (const char *) "";
80 #endif /* DUK_USE_DPRINT_COLORS */
82 #ifdef DUK_USE_VARIADIC_MACROS
84 DUK_INTERNAL
void duk_debug_log(duk_small_int_t level
, const char *file
, duk_int_t line
, const char *func
, const char *fmt
, ...) {
89 DUK_MEMZERO((void *) duk__debug_buf
, (size_t) DUK__DEBUG_BUFSIZE
);
90 duk_debug_vsnprintf(duk__debug_buf
, DUK__DEBUG_BUFSIZE
- 1, fmt
, ap
);
92 DUK_FPRINTF(DUK_STDERR
, "%s[%s] %s:%ld (%s):%s %s%s\n",
93 (const char *) duk__get_term_1(level
),
94 (const char *) duk__get_level_string(level
),
98 (const char *) duk__get_term_2(level
),
99 (const char *) duk__debug_buf
,
100 (const char *) duk__get_term_3(level
));
101 DUK_FFLUSH(DUK_STDERR
);
106 #else /* DUK_USE_VARIADIC_MACROS */
108 DUK_INTERNAL
char duk_debug_file_stash
[DUK_DEBUG_STASH_SIZE
];
109 DUK_INTERNAL
char duk_debug_line_stash
[DUK_DEBUG_STASH_SIZE
];
110 DUK_INTERNAL
char duk_debug_func_stash
[DUK_DEBUG_STASH_SIZE
];
111 DUK_INTERNAL duk_small_int_t duk_debug_level_stash
;
113 DUK_INTERNAL
void duk_debug_log(const char *fmt
, ...) {
115 duk_small_int_t level
= duk_debug_level_stash
;
119 DUK_MEMZERO((void *) duk__debug_buf
, (size_t) DUK__DEBUG_BUFSIZE
);
120 duk_debug_vsnprintf(duk__debug_buf
, DUK__DEBUG_BUFSIZE
- 1, fmt
, ap
);
122 DUK_FPRINTF(DUK_STDERR
, "%s[%s] %s:%s (%s):%s %s%s\n",
123 (const char *) duk__get_term_1(level
),
124 (const char *) duk__get_level_string(duk_debug_level_stash
),
125 (const char *) duk_debug_file_stash
,
126 (const char *) duk_debug_line_stash
,
127 (const char *) duk_debug_func_stash
,
128 (const char *) duk__get_term_2(level
),
129 (const char *) duk__debug_buf
,
130 (const char *) duk__get_term_3(level
));
131 DUK_FFLUSH(DUK_STDERR
);
136 #endif /* DUK_USE_VARIADIC_MACROS */
138 #else /* DUK_USE_DEBUG */
144 #endif /* DUK_USE_DEBUG */