]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/duktape-1.3.0/src-separate/duk_debug_macros.c
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / civetweb / src / third_party / duktape-1.3.0 / src-separate / duk_debug_macros.c
1 /*
2 * Debugging macro calls.
3 */
4
5 #include "duk_internal.h"
6
7 #ifdef DUK_USE_DEBUG
8
9 /*
10 * Debugging enabled
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdarg.h>
16
17 #define DUK__DEBUG_BUFSIZE DUK_USE_DEBUG_BUFSIZE
18 DUK_LOCAL char duk__debug_buf[DUK__DEBUG_BUFSIZE];
19
20 DUK_LOCAL const char *duk__get_level_string(duk_small_int_t level) {
21 switch ((int) level) {
22 case DUK_LEVEL_DEBUG:
23 return "D";
24 case DUK_LEVEL_DDEBUG:
25 return "DD";
26 case DUK_LEVEL_DDDEBUG:
27 return "DDD";
28 }
29 return "???";
30 }
31
32 #ifdef DUK_USE_DPRINT_COLORS
33
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"
40
41 DUK_LOCAL const char *duk__get_term_1(duk_small_int_t level) {
42 DUK_UNREF(level);
43 return (const char *) DUK__TERM_RED;
44 }
45
46 DUK_LOCAL const char *duk__get_term_2(duk_small_int_t level) {
47 switch ((int) level) {
48 case DUK_LEVEL_DEBUG:
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);
54 }
55 return (const char *) DUK__TERM_RESET;
56 }
57
58 DUK_LOCAL const char *duk__get_term_3(duk_small_int_t level) {
59 DUK_UNREF(level);
60 return (const char *) DUK__TERM_RESET;
61 }
62
63 #else
64
65 DUK_LOCAL const char *duk__get_term_1(duk_small_int_t level) {
66 DUK_UNREF(level);
67 return (const char *) "";
68 }
69
70 DUK_LOCAL const char *duk__get_term_2(duk_small_int_t level) {
71 DUK_UNREF(level);
72 return (const char *) "";
73 }
74
75 DUK_LOCAL const char *duk__get_term_3(duk_small_int_t level) {
76 DUK_UNREF(level);
77 return (const char *) "";
78 }
79
80 #endif /* DUK_USE_DPRINT_COLORS */
81
82 #ifdef DUK_USE_VARIADIC_MACROS
83
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, ...) {
85 va_list ap;
86
87 va_start(ap, fmt);
88
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);
91
92 #ifdef DUK_USE_DPRINT_RDTSC
93 DUK_FPRINTF(DUK_STDERR, "%s[%s] <%llu> %s:%ld (%s):%s %s%s\n",
94 (const char *) duk__get_term_1(level),
95 (const char *) duk__get_level_string(level),
96 (unsigned long long) DUK_USE_RDTSC(), /* match the inline asm in duk_features.h */
97 (const char *) file,
98 (long) line,
99 (const char *) func,
100 (const char *) duk__get_term_2(level),
101 (const char *) duk__debug_buf,
102 (const char *) duk__get_term_3(level));
103 #else
104 DUK_FPRINTF(DUK_STDERR, "%s[%s] %s:%ld (%s):%s %s%s\n",
105 (const char *) duk__get_term_1(level),
106 (const char *) duk__get_level_string(level),
107 (const char *) file,
108 (long) line,
109 (const char *) func,
110 (const char *) duk__get_term_2(level),
111 (const char *) duk__debug_buf,
112 (const char *) duk__get_term_3(level));
113 #endif
114 DUK_FFLUSH(DUK_STDERR);
115
116 va_end(ap);
117 }
118
119 #else /* DUK_USE_VARIADIC_MACROS */
120
121 DUK_INTERNAL char duk_debug_file_stash[DUK_DEBUG_STASH_SIZE];
122 DUK_INTERNAL char duk_debug_line_stash[DUK_DEBUG_STASH_SIZE];
123 DUK_INTERNAL char duk_debug_func_stash[DUK_DEBUG_STASH_SIZE];
124 DUK_INTERNAL duk_small_int_t duk_debug_level_stash;
125
126 DUK_INTERNAL void duk_debug_log(const char *fmt, ...) {
127 va_list ap;
128 duk_small_int_t level = duk_debug_level_stash;
129
130 va_start(ap, fmt);
131
132 DUK_MEMZERO((void *) duk__debug_buf, (size_t) DUK__DEBUG_BUFSIZE);
133 duk_debug_vsnprintf(duk__debug_buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap);
134
135 #ifdef DUK_USE_DPRINT_RDTSC
136 DUK_FPRINTF(DUK_STDERR, "%s[%s] <%llu> %s:%s (%s):%s %s%s\n",
137 (const char *) duk__get_term_1(level),
138 (const char *) duk__get_level_string(duk_debug_level_stash),
139 (unsigned long long) DUK_USE_RDTSC(), /* match duk_features.h */
140 (const char *) duk_debug_file_stash,
141 (const char *) duk_debug_line_stash,
142 (const char *) duk_debug_func_stash,
143 (const char *) duk__get_term_2(level),
144 (const char *) duk__debug_buf,
145 (const char *) duk__get_term_3(level));
146 #else
147 DUK_FPRINTF(DUK_STDERR, "%s[%s] %s:%s (%s):%s %s%s\n",
148 (const char *) duk__get_term_1(level),
149 (const char *) duk__get_level_string(duk_debug_level_stash),
150 (const char *) duk_debug_file_stash,
151 (const char *) duk_debug_line_stash,
152 (const char *) duk_debug_func_stash,
153 (const char *) duk__get_term_2(level),
154 (const char *) duk__debug_buf,
155 (const char *) duk__get_term_3(level));
156 #endif
157 DUK_FFLUSH(DUK_STDERR);
158
159 va_end(ap);
160 }
161
162 #endif /* DUK_USE_VARIADIC_MACROS */
163
164 #else /* DUK_USE_DEBUG */
165
166 /*
167 * Debugging disabled
168 */
169
170 #endif /* DUK_USE_DEBUG */