Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duk_bi_logger.c File Reference
#include "duk_internal.h"

Go to the source code of this file.

Functions

DUK_INTERNAL duk_ret_t duk_bi_logger_constructor (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_fmt (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_raw (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_log_shared (duk_context *ctx)
 

Variables

DUK_LOCAL const duk_uint8_t duk__log_level_strings []
 

Function Documentation

◆ duk_bi_logger_constructor()

DUK_INTERNAL duk_ret_t duk_bi_logger_constructor ( duk_context * ctx)

Definition at line 18 of file duktape-1.5.2/src-separate/duk_bi_logger.c.

18 {
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);
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);
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}
DUK_INTERNAL_DECL duk_bool_t duk_put_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx)
DUK_EXTERNAL void duk_push_this(duk_context *ctx)
DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx)
DUK_EXTERNAL void duk_compact(duk_context *ctx, duk_idx_t obj_index)
DUK_EXTERNAL void duk_replace(duk_context *ctx, duk_idx_t to_index)
DUK_EXTERNAL void duk_set_top(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_dup(duk_context *ctx, duk_idx_t from_index)
DUK_EXTERNAL duk_idx_t duk_get_top(duk_context *ctx)
DUK_EXTERNAL duk_bool_t duk_is_constructor_call(duk_context *ctx)
#define DUK_ACT_GET_FUNC(act)
DUK_INTERNAL_DECL void duk_push_hobject(duk_context *ctx, duk_hobject *h)
DUK_EXTERNAL duk_bool_t duk_is_string(duk_context *ctx, duk_idx_t index)

References duk_hthread::callstack, duk_hthread::callstack_top, DUK_ACT_GET_FUNC, duk_compact(), duk_dup(), duk_get_prop_stridx(), duk_get_top(), duk_is_constructor_call(), duk_is_string(), duk_push_hobject(), duk_push_this(), duk_put_prop_stridx(), duk_replace(), DUK_RET_TYPE_ERROR, duk_set_top(), DUK_STRIDX_FILE_NAME, and DUK_STRIDX_LC_N.

◆ duk_bi_logger_prototype_fmt()

DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_fmt ( duk_context * ctx)

Definition at line 75 of file duktape-1.5.2/src-separate/duk_bi_logger.c.

75 {
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}
DUK_EXTERNAL void duk_call_method(duk_context *ctx, duk_idx_t nargs)
#define DUK_STRIDX_TO_LOG_STRING
DUK_EXTERNAL const char * duk_to_string(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_pop(duk_context *ctx)

References duk_call_method(), duk_dup(), duk_get_prop_stridx(), duk_pop(), DUK_STRIDX_TO_LOG_STRING, and duk_to_string().

◆ duk_bi_logger_prototype_log_shared()

DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_log_shared ( duk_context * ctx)

Definition at line 123 of file duktape-1.5.2/src-separate/duk_bi_logger.c.

123 {
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;
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
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
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 */
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, (const void *) date_buf, (size_t) 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, (const void *) q, (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, (size_t) 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, (size_t) 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 */
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}
duk_int_fast32_t duk_int_t
DUK_EXTERNAL const char * duk_get_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len)
DUK_EXTERNAL void duk_call_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs)
DUK_EXTERNAL duk_size_t duk_get_length(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL_DECL void duk_push_hstring_stridx(duk_context *ctx, duk_small_int_t stridx)
DUK_EXTERNAL void * duk_get_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size)
#define DUK_BI_DATE_ISO8601_BUFSIZE
DUK_EXTERNAL const char * duk_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len)
DUK_EXTERNAL duk_int_t duk_get_int(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_bool_t duk_is_object(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL_DECL void duk_bi_date_format_timeval(duk_double_t timeval, duk_uint8_t *out_buf)
DUK_EXTERNAL duk_int_t duk_pcall_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs)
DUK_EXTERNAL duk_int_t duk_get_current_magic(duk_context *ctx)
#define duk_push_fixed_buffer(ctx, size)
DUK_LOCAL const duk_uint8_t duk__log_level_strings[]
#define NULL
Definition gmacros.h:924

References duk__log_level_strings, DUK_ASC_COLON, DUK_ASC_SPACE, DUK_ASSERT, duk_bi_date_format_timeval(), DUK_BI_DATE_ISO8601_BUFSIZE, duk_call_prop(), DUK_DBG_CMD_LOG, duk_dup(), duk_get_buffer(), duk_get_current_magic(), duk_get_int(), duk_get_length(), duk_get_lstring(), duk_get_prop_stridx(), duk_get_top(), duk_is_object(), duk_is_string(), DUK_MEMCPY, duk_pcall_prop(), duk_push_fixed_buffer, duk_push_hstring_stridx(), duk_push_this(), duk_replace(), DUK_STRIDX_FMT, DUK_STRIDX_LC_L, DUK_STRIDX_LC_N, DUK_STRIDX_RAW, DUK_STRLEN, duk_to_lstring(), duk_to_string(), DUK_UNREF, duk_hthread::heap, and NULL.

◆ duk_bi_logger_prototype_raw()

DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_raw ( duk_context * ctx)

Definition at line 99 of file duktape-1.5.2/src-separate/duk_bi_logger.c.

99 {
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);
112#else
113 /* nop */
114#endif
115 return 0;
116}
DUK_EXTERNAL void * duk_require_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size)

References DUK_FFLUSH, DUK_FPUTC, DUK_FWRITE, duk_require_buffer(), DUK_STDERR, and DUK_UNREF.

Variable Documentation

◆ duk__log_level_strings

DUK_LOCAL const duk_uint8_t duk__log_level_strings[]
Initial value:
= {
(duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_C,
(duk_uint8_t) DUK_ASC_UC_D, (duk_uint8_t) DUK_ASC_UC_B, (duk_uint8_t) DUK_ASC_UC_G,
(duk_uint8_t) DUK_ASC_UC_I, (duk_uint8_t) DUK_ASC_UC_N, (duk_uint8_t) DUK_ASC_UC_F,
(duk_uint8_t) DUK_ASC_UC_W, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_N,
(duk_uint8_t) DUK_ASC_UC_E, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_R,
(duk_uint8_t) DUK_ASC_UC_F, (duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_L
}

Definition at line 8 of file duktape-1.5.2/src-separate/duk_bi_logger.c.

8 {
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};

Referenced by duk_bi_logger_prototype_log_shared().