]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/duktape-1.3.0/src-separate/duk_bi_logger.c
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / civetweb / src / third_party / duktape-1.3.0 / src-separate / duk_bi_logger.c
1 /*
2 * Logging support
3 */
4
5 #include "duk_internal.h"
6
7 /* 3-letter log level strings */
8 DUK_LOCAL const duk_uint8_t duk__log_level_strings[] = {
9 (duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_C,
10 (duk_uint8_t) DUK_ASC_UC_D, (duk_uint8_t) DUK_ASC_UC_B, (duk_uint8_t) DUK_ASC_UC_G,
11 (duk_uint8_t) DUK_ASC_UC_I, (duk_uint8_t) DUK_ASC_UC_N, (duk_uint8_t) DUK_ASC_UC_F,
12 (duk_uint8_t) DUK_ASC_UC_W, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_N,
13 (duk_uint8_t) DUK_ASC_UC_E, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_R,
14 (duk_uint8_t) DUK_ASC_UC_F, (duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_L
15 };
16
17 /* Constructor */
18 DUK_INTERNAL duk_ret_t duk_bi_logger_constructor(duk_context *ctx) {
19 duk_hthread *thr = (duk_hthread *) ctx;
20 duk_idx_t nargs;
21
22 /* Calling as a non-constructor is not meaningful. */
23 if (!duk_is_constructor_call(ctx)) {
24 return DUK_RET_TYPE_ERROR;
25 }
26
27 nargs = duk_get_top(ctx);
28 duk_set_top(ctx, 1);
29
30 duk_push_this(ctx);
31
32 /* [ name this ] */
33
34 if (nargs == 0) {
35 /* Automatic defaulting of logger name from caller. This would
36 * work poorly with tail calls, but constructor calls are currently
37 * never tail calls, so tail calls are not an issue now.
38 */
39
40 if (thr->callstack_top >= 2) {
41 duk_activation *act_caller = thr->callstack + thr->callstack_top - 2;
42 duk_hobject *func_caller;
43
44 func_caller = DUK_ACT_GET_FUNC(act_caller);
45 if (func_caller) {
46 /* Stripping the filename might be a good idea
47 * ("/foo/bar/quux.js" -> logger name "quux"),
48 * but now used verbatim.
49 */
50 duk_push_hobject(ctx, func_caller);
51 duk_get_prop_stridx(ctx, -1, DUK_STRIDX_FILE_NAME);
52 duk_replace(ctx, 0);
53 }
54 }
55 }
56 /* the stack is unbalanced here on purpose; we only rely on the
57 * initial two values: [ name this ].
58 */
59
60 if (duk_is_string(ctx, 0)) {
61 duk_dup(ctx, 0);
62 duk_put_prop_stridx(ctx, 1, DUK_STRIDX_LC_N);
63 } else {
64 /* don't set 'n' at all, inherited value is used as name */
65 }
66
67 duk_compact(ctx, 1);
68
69 return 0; /* keep default instance */
70 }
71
72 /* Default function to format objects. Tries to use toLogString() but falls
73 * back to toString(). Any errors are propagated out without catching.
74 */
75 DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_fmt(duk_context *ctx) {
76 if (duk_get_prop_stridx(ctx, 0, DUK_STRIDX_TO_LOG_STRING)) {
77 /* [ arg toLogString ] */
78
79 duk_dup(ctx, 0);
80 duk_call_method(ctx, 0);
81
82 /* [ arg result ] */
83 return 1;
84 }
85
86 /* [ arg undefined ] */
87 duk_pop(ctx);
88 duk_to_string(ctx, 0);
89 return 1;
90 }
91
92 /* Default function to write a formatted log line. Writes to stderr,
93 * appending a newline to the log line.
94 *
95 * The argument is a buffer whose visible size contains the log message.
96 * This function should avoid coercing the buffer to a string to avoid
97 * string table traffic.
98 */
99 DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_raw(duk_context *ctx) {
100 const char *data;
101 duk_size_t data_len;
102
103 DUK_UNREF(ctx);
104 DUK_UNREF(data);
105 DUK_UNREF(data_len);
106
107 #ifdef DUK_USE_FILE_IO
108 data = (const char *) duk_require_buffer(ctx, 0, &data_len);
109 DUK_FWRITE((const void *) data, 1, data_len, DUK_STDERR);
110 DUK_FPUTC((int) '\n', DUK_STDERR);
111 DUK_FFLUSH(DUK_STDERR);
112 #else
113 /* nop */
114 #endif
115 return 0;
116 }
117
118 /* Log frontend shared helper, magic value indicates log level. Provides
119 * frontend functions: trace(), debug(), info(), warn(), error(), fatal().
120 * This needs to have small footprint, reasonable performance, minimal
121 * memory churn, etc.
122 */
123 DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_log_shared(duk_context *ctx) {
124 duk_hthread *thr = (duk_hthread *) ctx;
125 duk_double_t now;
126 duk_small_int_t entry_lev = duk_get_current_magic(ctx);
127 duk_small_int_t logger_lev;
128 duk_int_t nargs;
129 duk_int_t i;
130 duk_size_t tot_len;
131 const duk_uint8_t *arg_str;
132 duk_size_t arg_len;
133 duk_uint8_t *buf, *p;
134 const duk_uint8_t *q;
135 duk_uint8_t date_buf[DUK_BI_DATE_ISO8601_BUFSIZE];
136 duk_size_t date_len;
137 duk_small_int_t rc;
138
139 DUK_ASSERT(entry_lev >= 0 && entry_lev <= 5);
140 DUK_UNREF(thr);
141
142 /* XXX: sanitize to printable (and maybe ASCII) */
143 /* XXX: better multiline */
144
145 /*
146 * Logger arguments are:
147 *
148 * magic: log level (0-5)
149 * this: logger
150 * stack: plain log args
151 *
152 * We want to minimize memory churn so a two-pass approach
153 * is used: first pass formats arguments and computes final
154 * string length, second pass copies strings either into a
155 * pre-allocated and reused buffer (short messages) or into a
156 * newly allocated fixed buffer. If the backend function plays
157 * nice, it won't coerce the buffer to a string (and thus
158 * intern it).
159 */
160
161 nargs = duk_get_top(ctx);
162
163 /* [ arg1 ... argN this ] */
164
165 /*
166 * Log level check
167 */
168
169 duk_push_this(ctx);
170
171 duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LC_L);
172 logger_lev = (duk_small_int_t) duk_get_int(ctx, -1);
173 if (entry_lev < logger_lev) {
174 return 0;
175 }
176 /* log level could be popped but that's not necessary */
177
178 now = DUK_USE_DATE_GET_NOW(ctx);
179 duk_bi_date_format_timeval(now, date_buf);
180 date_len = DUK_STRLEN((const char *) date_buf);
181
182 duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LC_N);
183 duk_to_string(ctx, -1);
184 DUK_ASSERT(duk_is_string(ctx, -1));
185
186 /* [ arg1 ... argN this loggerLevel loggerName ] */
187
188 /*
189 * Pass 1
190 */
191
192 /* Line format: <time> <entryLev> <loggerName>: <msg> */
193
194 tot_len = 0;
195 tot_len += 3 + /* separators: space, space, colon */
196 3 + /* level string */
197 date_len + /* time */
198 duk_get_length(ctx, -1); /* loggerName */
199
200 for (i = 0; i < nargs; i++) {
201 /* When formatting an argument to a string, errors may happen from multiple
202 * causes. In general we want to catch obvious errors like a toLogString()
203 * throwing an error, but we don't currently try to catch every possible
204 * error. In particular, internal errors (like out of memory or stack) are
205 * not caught. Also, we expect Error toString() to not throw an error.
206 */
207 if (duk_is_object(ctx, i)) {
208 /* duk_pcall_prop() may itself throw an error, but we're content
209 * in catching the obvious errors (like toLogString() throwing an
210 * error).
211 */
212 duk_push_hstring_stridx(ctx, DUK_STRIDX_FMT);
213 duk_dup(ctx, i);
214 /* [ arg1 ... argN this loggerLevel loggerName 'fmt' arg ] */
215 /* call: this.fmt(arg) */
216 rc = duk_pcall_prop(ctx, -5 /*obj_index*/, 1 /*nargs*/);
217 if (rc) {
218 /* Keep the error as the result (coercing it might fail below,
219 * but we don't catch that now).
220 */
221 ;
222 }
223 duk_replace(ctx, i);
224 }
225 (void) duk_to_lstring(ctx, i, &arg_len);
226 tot_len++; /* sep (even before first one) */
227 tot_len += arg_len;
228 }
229
230 /*
231 * Pass 2
232 */
233
234 /* XXX: There used to be a shared log buffer here, but it was removed
235 * when dynamic buffer spare was removed. The problem with using
236 * bufwriter is that, without the spare, the buffer gets passed on
237 * as an argument to the raw() call so it'd need to be resized
238 * (reallocated) anyway. If raw() call convention is changed, this
239 * could be made more efficient.
240 */
241
242 buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, tot_len);
243 DUK_ASSERT(buf != NULL);
244 p = buf;
245
246 DUK_MEMCPY((void *) p, (void *) date_buf, date_len);
247 p += date_len;
248 *p++ = (duk_uint8_t) DUK_ASC_SPACE;
249
250 q = duk__log_level_strings + (entry_lev * 3);
251 DUK_MEMCPY((void *) p, (void *) q, (duk_size_t) 3);
252 p += 3;
253
254 *p++ = (duk_uint8_t) DUK_ASC_SPACE;
255
256 arg_str = (const duk_uint8_t *) duk_get_lstring(ctx, -2, &arg_len);
257 DUK_MEMCPY((void *) p, (const void *) arg_str, arg_len);
258 p += arg_len;
259
260 *p++ = (duk_uint8_t) DUK_ASC_COLON;
261
262 for (i = 0; i < nargs; i++) {
263 *p++ = (duk_uint8_t) DUK_ASC_SPACE;
264
265 arg_str = (const duk_uint8_t *) duk_get_lstring(ctx, i, &arg_len);
266 DUK_ASSERT(arg_str != NULL);
267 DUK_MEMCPY((void *) p, (const void *) arg_str, arg_len);
268 p += arg_len;
269 }
270 DUK_ASSERT(buf + tot_len == p);
271
272 /* [ arg1 ... argN this loggerLevel loggerName buffer ] */
273
274 #if defined(DUK_USE_DEBUGGER_SUPPORT) && defined(DUK_USE_DEBUGGER_FWD_LOGGING)
275 /* Do debugger forwarding before raw() because the raw() function
276 * doesn't get the log level right now.
277 */
278 if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
279 const char *log_buf;
280 duk_size_t sz_buf;
281 log_buf = (const char *) duk_get_buffer(ctx, -1, &sz_buf);
282 DUK_ASSERT(log_buf != NULL);
283 duk_debug_write_notify(thr, DUK_DBG_CMD_LOG);
284 duk_debug_write_int(thr, (duk_int32_t) entry_lev);
285 duk_debug_write_string(thr, (const char *) log_buf, sz_buf);
286 duk_debug_write_eom(thr);
287 }
288 #endif
289
290 /* Call this.raw(msg); look up through the instance allows user to override
291 * the raw() function in the instance or in the prototype for maximum
292 * flexibility.
293 */
294 duk_push_hstring_stridx(ctx, DUK_STRIDX_RAW);
295 duk_dup(ctx, -2);
296 /* [ arg1 ... argN this loggerLevel loggerName buffer 'raw' buffer ] */
297 duk_call_prop(ctx, -6, 1); /* this.raw(buffer) */
298
299 return 0;
300 }