/src/libwebsockets/lib/core/logs.c
Line | Count | Source |
1 | | /* |
2 | | * libwebsockets - small server side websockets and web server implementation |
3 | | * |
4 | | * Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com> |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | * of this software and associated documentation files (the "Software"), to |
8 | | * deal in the Software without restriction, including without limitation the |
9 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
10 | | * sell copies of the Software, and to permit persons to whom the Software is |
11 | | * furnished to do so, subject to the following conditions: |
12 | | * |
13 | | * The above copyright notice and this permission notice shall be included in |
14 | | * all copies or substantial portions of the Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
21 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
22 | | * IN THE SOFTWARE. |
23 | | */ |
24 | | |
25 | | #include "private-lib-core.h" |
26 | | |
27 | | #ifdef LWS_HAVE_SYS_TYPES_H |
28 | | #include <sys/types.h> |
29 | | #endif |
30 | | |
31 | | #if defined(LWS_PLAT_OPTEE) |
32 | | void lwsl_emit_optee(int level, const char *line); |
33 | | #endif |
34 | | |
35 | | lws_log_cx_t log_cx = { |
36 | | #if !defined(LWS_PLAT_OPTEE) |
37 | | .u.emit = lwsl_emit_stderr, |
38 | | #else |
39 | | .u.emit = lwsl_emit_optee, |
40 | | #endif |
41 | | .lll_flags = LLL_ERR | LLL_WARN | LLL_NOTICE, |
42 | | }; |
43 | | |
44 | | #if !defined(LWS_PLAT_OPTEE) && !defined(LWS_WITH_NO_LOGS) |
45 | | static const char * log_level_names ="EWNIDPHXCLUT??"; |
46 | | #endif |
47 | | |
48 | | /* |
49 | | * Name an instance tag and attach to a group |
50 | | */ |
51 | | |
52 | | void |
53 | | __lws_lc_tag(struct lws_context *context, lws_lifecycle_group_t *grp, |
54 | | lws_lifecycle_t *lc, const char *format, ...) |
55 | 0 | { |
56 | 0 | va_list ap; |
57 | 0 | int n = 1; |
58 | |
|
59 | 0 | if (*lc->gutag == '[') { |
60 | | /* appending inside [] */ |
61 | |
|
62 | 0 | char *cp = (char *)strchr(lc->gutag, ']'); |
63 | 0 | char rend[96]; |
64 | 0 | size_t ll, k; |
65 | 0 | int n; |
66 | |
|
67 | 0 | if (!cp) |
68 | 0 | return; |
69 | | |
70 | | /* length of closing brace and anything else after it */ |
71 | 0 | k = strlen(cp); |
72 | | |
73 | | /* compute the remaining gutag unused */ |
74 | 0 | ll = sizeof(lc->gutag) - lws_ptr_diff_size_t(cp, lc->gutag) - k - 1; |
75 | 0 | if (ll > sizeof(rend) - 1) |
76 | 0 | ll = sizeof(rend) - 1; |
77 | 0 | va_start(ap, format); |
78 | 0 | n = vsnprintf(rend, ll, format, ap); |
79 | 0 | va_end(ap); |
80 | |
|
81 | 0 | if ((unsigned int)n > ll) |
82 | 0 | n = (int)ll; |
83 | | |
84 | | /* shove the trailer up by what we added */ |
85 | 0 | memmove(cp + n, cp, k); |
86 | 0 | assert(k + (unsigned int)n < sizeof(lc->gutag)); |
87 | 0 | cp[k + (unsigned int)n] = '\0'; |
88 | | /* copy what we added into place */ |
89 | 0 | memcpy(cp, rend, (unsigned int)n); |
90 | |
|
91 | 0 | return; |
92 | 0 | } |
93 | | |
94 | 0 | assert(grp); |
95 | 0 | assert(grp->tag_prefix); /* lc group must have a tag prefix string */ |
96 | |
|
97 | 0 | lc->gutag[0] = '['; |
98 | |
|
99 | | #if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) /* ie, will have getpid if set */ |
100 | | n += lws_snprintf(&lc->gutag[n], sizeof(lc->gutag) - |
101 | | (unsigned int)n - 1u, "%u|", getpid()); |
102 | | #endif |
103 | 0 | n += lws_snprintf(&lc->gutag[n], sizeof(lc->gutag) - |
104 | 0 | (unsigned int)n - 1u, "%s|%lx|", |
105 | 0 | grp->tag_prefix, |
106 | 0 | (unsigned long)grp->ordinal++); |
107 | |
|
108 | 0 | va_start(ap, format); |
109 | 0 | n += vsnprintf(&lc->gutag[n], sizeof(lc->gutag) - (unsigned int)n - |
110 | 0 | 1u, format, ap); |
111 | 0 | va_end(ap); |
112 | |
|
113 | 0 | if (n < (int)sizeof(lc->gutag) - 2) { |
114 | 0 | lc->gutag[n++] = ']'; |
115 | 0 | lc->gutag[n++] = '\0'; |
116 | 0 | } else { |
117 | 0 | lc->gutag[sizeof(lc->gutag) - 2] = ']'; |
118 | 0 | lc->gutag[sizeof(lc->gutag) - 1] = '\0'; |
119 | 0 | } |
120 | |
|
121 | 0 | lc->us_creation = (uint64_t)lws_now_usecs(); |
122 | 0 | lws_dll2_add_tail(&lc->list, &grp->owner); |
123 | |
|
124 | 0 | lwsl_refcount_cx(lc->log_cx, 1); |
125 | |
|
126 | 0 | #if defined(LWS_LOG_TAG_LIFECYCLE) |
127 | 0 | lwsl_cx_info(context, " ++ %s (%d)", lc->gutag, (int)grp->owner.count); |
128 | 0 | #endif |
129 | 0 | } |
130 | | |
131 | | /* |
132 | | * Normally we want to set the tag one time at creation. But sometimes we |
133 | | * don't have enough information at that point to give it a meaningful tag, eg, |
134 | | * it's an accepted, served connection but we haven't read data from it yet |
135 | | * to find out what it wants to be. |
136 | | * |
137 | | * This allows you to append some extra info to the tag in those cases, the |
138 | | * initial tag remains the same on the lhs so it can be tracked correctly. |
139 | | */ |
140 | | |
141 | | void |
142 | | __lws_lc_tag_append(lws_lifecycle_t *lc, const char *app) |
143 | 0 | { |
144 | 0 | int n = (int)strlen(lc->gutag); |
145 | |
|
146 | 0 | if (n && lc->gutag[n - 1] == ']') |
147 | 0 | n--; |
148 | |
|
149 | 0 | if (!lc->recycle_len) |
150 | 0 | lc->recycle_len = (uint8_t)n; |
151 | 0 | else |
152 | 0 | n = lc->recycle_len; |
153 | |
|
154 | 0 | n += lws_snprintf(&lc->gutag[n], sizeof(lc->gutag) - 2u - |
155 | 0 | (unsigned int)n, "|%s]", app); |
156 | |
|
157 | 0 | if ((unsigned int)n >= sizeof(lc->gutag) - 2u) { |
158 | 0 | lc->gutag[sizeof(lc->gutag) - 2] = ']'; |
159 | 0 | lc->gutag[sizeof(lc->gutag) - 1] = '\0'; |
160 | 0 | } |
161 | 0 | } |
162 | | |
163 | | /* |
164 | | * Remove instance from group |
165 | | */ |
166 | | |
167 | | void |
168 | | __lws_lc_untag(struct lws_context *context, lws_lifecycle_t *lc) |
169 | 0 | { |
170 | | //lws_lifecycle_group_t *grp; |
171 | 0 | char buf[24]; |
172 | |
|
173 | 0 | if (!lc->gutag[0]) { /* we never tagged this object... */ |
174 | 0 | lwsl_cx_err(context, "%s never tagged", lc->gutag); |
175 | 0 | assert(0); |
176 | 0 | return; |
177 | 0 | } |
178 | | |
179 | 0 | if (!lc->list.owner) { /* we already untagged this object... */ |
180 | 0 | lwsl_cx_err(context, "%s untagged twice", lc->gutag); |
181 | 0 | assert(0); |
182 | 0 | return; |
183 | 0 | } |
184 | | |
185 | | //grp = lws_container_of(lc->list.owner, lws_lifecycle_group_t, owner); |
186 | | |
187 | 0 | #if defined(LWS_LOG_TAG_LIFECYCLE) |
188 | 0 | if (lws_humanize(buf, sizeof(buf), |
189 | 0 | (uint64_t)lws_now_usecs() - lc->us_creation, |
190 | 0 | humanize_schema_us) > 0) |
191 | | |
192 | 0 | lwsl_cx_info(context, " -- %s (%d) %s", lc->gutag, |
193 | 0 | (int)lc->list.owner->count - 1, buf); |
194 | 0 | #endif |
195 | |
|
196 | 0 | lws_dll2_remove(&lc->list); |
197 | |
|
198 | 0 | lwsl_refcount_cx(lc->log_cx, -1); |
199 | 0 | } |
200 | | |
201 | | const char * |
202 | | lws_lc_tag(lws_lifecycle_t *lc) |
203 | 0 | { |
204 | 0 | return lc->gutag; |
205 | 0 | } |
206 | | |
207 | | |
208 | | int |
209 | | lwsl_timestamp(int level, char *p, size_t len) |
210 | 82 | { |
211 | 82 | #if !defined(LWS_PLAT_OPTEE) && !defined(LWS_WITH_NO_LOGS) |
212 | 82 | time_t o_now; |
213 | 82 | unsigned long long now; |
214 | 82 | struct timeval tv; |
215 | 82 | struct tm *ptm = NULL; |
216 | 82 | #if defined(LWS_HAVE_LOCALTIME_R) |
217 | 82 | struct tm tm; |
218 | 82 | #endif |
219 | 82 | int n; |
220 | | |
221 | 82 | gettimeofday(&tv, NULL); |
222 | 82 | o_now = tv.tv_sec; |
223 | 82 | now = ((unsigned long long)tv.tv_sec * 10000) + |
224 | 82 | (unsigned int)(tv.tv_usec / 100); |
225 | | |
226 | 82 | #if defined(LWS_HAVE_LOCALTIME_R) |
227 | 82 | ptm = localtime_r(&o_now, &tm); |
228 | | #else |
229 | | ptm = localtime(&o_now); |
230 | | #endif |
231 | 82 | p[0] = '\0'; |
232 | 82 | for (n = 0; n < LLL_COUNT; n++) { |
233 | 82 | if (level != (1 << n)) |
234 | 0 | continue; |
235 | | |
236 | 82 | if (ptm) |
237 | 82 | n = lws_snprintf(p, len, |
238 | 82 | "[%04d/%02d/%02d %02d:%02d:%02d:%04d] %c: ", |
239 | 82 | ptm->tm_year + 1900, |
240 | 82 | ptm->tm_mon + 1, |
241 | 82 | ptm->tm_mday, |
242 | 82 | ptm->tm_hour, |
243 | 82 | ptm->tm_min, |
244 | 82 | ptm->tm_sec, |
245 | 82 | (int)(now % 10000), log_level_names[n]); |
246 | 0 | else |
247 | 0 | n = lws_snprintf(p, len, "[%llu:%04d] %c: ", |
248 | 0 | (unsigned long long) now / 10000, |
249 | 0 | (int)(now % 10000), log_level_names[n]); |
250 | | |
251 | | #if defined(LWS_PLAT_FREERTOS) |
252 | | n += lws_snprintf(p + n, len - n, "%6u: ", |
253 | | #if defined(LWS_AMAZON_RTOS) |
254 | | (unsigned int)xPortGetFreeHeapSize()); |
255 | | #else |
256 | | (unsigned int)esp_get_free_heap_size()); |
257 | | #endif |
258 | | #endif |
259 | | |
260 | 82 | return n; |
261 | 82 | } |
262 | | #else |
263 | | p[0] = '\0'; |
264 | | #endif |
265 | | |
266 | 0 | return 0; |
267 | 82 | } |
268 | | |
269 | | uint32_t |
270 | | lws_log_ratelimit_check(lws_log_ratelimit_t *rl, int64_t interval_us) |
271 | 0 | { |
272 | 0 | lws_usec_t now = lws_now_usecs(); |
273 | |
|
274 | 0 | if (now >= rl->next_log_us) { |
275 | 0 | uint32_t r = rl->dropped + 1; |
276 | 0 | rl->next_log_us = now + interval_us; |
277 | 0 | rl->dropped = 0; |
278 | 0 | return r; |
279 | 0 | } |
280 | | |
281 | 0 | rl->dropped++; |
282 | |
|
283 | 0 | return 0; |
284 | 0 | } |
285 | | |
286 | | |
287 | | #ifndef LWS_PLAT_OPTEE |
288 | | static const char * const colours[] = { |
289 | | "[31;1m", /* LLL_ERR */ |
290 | | "[36;1m", /* LLL_WARN */ |
291 | | "[35;1m", /* LLL_NOTICE */ |
292 | | "[32;1m", /* LLL_INFO */ |
293 | | "[34;1m", /* LLL_DEBUG */ |
294 | | "[33;1m", /* LLL_PARSER */ |
295 | | "[33m", /* LLL_HEADER */ |
296 | | "[33m", /* LLL_EXT */ |
297 | | "[33m", /* LLL_CLIENT */ |
298 | | "[33;1m", /* LLL_LATENCY */ |
299 | | "[0;1m", /* LLL_USER */ |
300 | | "[31m", /* LLL_THREAD */ |
301 | | }; |
302 | | |
303 | | static char tty; |
304 | | |
305 | | static void |
306 | | _lwsl_emit_stderr(int level, const char *line) |
307 | 49 | { |
308 | 49 | int n, m = LWS_ARRAY_SIZE(colours) - 1; |
309 | | |
310 | 49 | if (!tty) |
311 | 1 | tty = (char)(isatty(2) | 2); |
312 | | |
313 | 49 | if (tty == 3) { |
314 | 0 | n = 1 << (LWS_ARRAY_SIZE(colours) - 1); |
315 | 0 | while (n) { |
316 | 0 | if (level & n) |
317 | 0 | break; |
318 | 0 | m--; |
319 | 0 | n >>= 1; |
320 | 0 | } |
321 | 0 | fprintf(stderr, "%c%s%s%c[0m", 27, colours[m], line, 27); |
322 | 0 | } else |
323 | 49 | fprintf(stderr, "%s", line); |
324 | 49 | } |
325 | | |
326 | | void |
327 | | lwsl_emit_stderr(int level, const char *line) |
328 | 49 | { |
329 | 49 | _lwsl_emit_stderr(level, line); |
330 | 49 | } |
331 | | |
332 | | void |
333 | | lwsl_emit_stderr_notimestamp(int level, const char *line) |
334 | 0 | { |
335 | 0 | _lwsl_emit_stderr(level, line); |
336 | 0 | } |
337 | | |
338 | | #if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE) && !defined(LWS_PLAT_BAREMETAL) |
339 | | |
340 | | /* |
341 | | * Helper to emit to a file |
342 | | */ |
343 | | |
344 | | void |
345 | | lws_log_emit_cx_file(struct lws_log_cx *cx, int level, const char *line, |
346 | | size_t len) |
347 | 0 | { |
348 | 0 | int fd = (int)(intptr_t)cx->stg; |
349 | |
|
350 | 0 | if (fd >= 0) |
351 | 0 | if (write(fd, line, (unsigned int)len) != (ssize_t)len) |
352 | 0 | fprintf(stderr, "Unable to write log to file\n"); |
353 | 0 | } |
354 | | |
355 | | /* |
356 | | * Helper to use a .refcount_cb to store logs in a file |
357 | | */ |
358 | | |
359 | | void |
360 | | lws_log_use_cx_file(struct lws_log_cx *cx, int _new) |
361 | 0 | { |
362 | 0 | int fd; |
363 | |
|
364 | 0 | if (_new > 0 && cx->refcount == 1) { |
365 | 0 | fd = open((const char *)cx->opaque, |
366 | 0 | LWS_O_CREAT | LWS_O_TRUNC | LWS_O_WRONLY, 0600); |
367 | 0 | if (fd < 0) |
368 | 0 | fprintf(stderr, "Unable to open log %s: errno %d\n", |
369 | 0 | (const char *)cx->opaque, errno); |
370 | 0 | cx->stg = (void *)(intptr_t)fd; |
371 | |
|
372 | 0 | return; |
373 | 0 | } |
374 | | |
375 | 0 | fd = (int)(intptr_t)cx->stg; |
376 | |
|
377 | 0 | if (_new <= 0 && cx->refcount == 0 && fd >= 0) { |
378 | 0 | close(fd); |
379 | 0 | cx->stg = (void *)(intptr_t)-1; |
380 | 0 | } |
381 | 0 | } |
382 | | |
383 | | #endif |
384 | | |
385 | | #endif |
386 | | |
387 | | #if !(defined(LWS_PLAT_OPTEE) && !defined(LWS_WITH_NETWORK)) |
388 | | void |
389 | | __lws_logv(lws_log_cx_t *cx, lws_log_prepend_cx_t prep, void *obj, |
390 | | int filter, uint32_t dropped, const char *_fun, const char *format, va_list vl) |
391 | 11.3k | { |
392 | 11.3k | #if LWS_MAX_SMP == 1 && !defined(LWS_WITH_THREADPOOL) |
393 | | /* this is incompatible with multithreaded logging */ |
394 | 11.3k | static char buf[256], prev_buf[256]; |
395 | | #else |
396 | | char buf[1024]; |
397 | | static char prev_buf[1024]; |
398 | | #endif |
399 | 11.3k | static uint32_t log_dupes; |
400 | 11.3k | static lws_usec_t last_log_dupe_emit; |
401 | 11.3k | char *p = buf, *end = p + sizeof(buf) - 1, *body_start; |
402 | 11.3k | lws_log_cx_t *cxp; |
403 | 11.3k | int n, back = 0; |
404 | | |
405 | | /* |
406 | | * We need to handle NULL wsi etc at the wrappers as gracefully as |
407 | | * possible |
408 | | */ |
409 | | |
410 | 11.3k | if (!cx) { |
411 | 0 | lws_strncpy(p, "NULL log cx: ", sizeof(buf) - 1); |
412 | 0 | p += 13; |
413 | | /* use the processwide one for lack of anything better */ |
414 | 0 | cx = &log_cx; |
415 | 0 | } |
416 | | |
417 | 11.3k | cxp = cx; |
418 | | |
419 | 11.3k | if (!(cx->lll_flags & (uint32_t)filter)) |
420 | | /* |
421 | | * logs may be produced and built in to the code but disabled |
422 | | * at runtime |
423 | | */ |
424 | 11.2k | return; |
425 | | |
426 | | #if !defined(LWS_LOGS_TIMESTAMP) |
427 | | if (cx->lll_flags & LLLF_LOG_TIMESTAMP) |
428 | | #endif |
429 | 82 | { |
430 | 82 | buf[0] = '\0'; |
431 | 82 | lwsl_timestamp(filter, buf, sizeof(buf)); |
432 | 82 | p += strlen(buf); |
433 | 82 | } |
434 | | |
435 | 82 | body_start = p; |
436 | | |
437 | | /* |
438 | | * prepend parent log ctx content first |
439 | | * top level cx also gets an opportunity to prepend |
440 | | */ |
441 | | |
442 | 82 | while (cxp->parent) { |
443 | 0 | cxp = cxp->parent; |
444 | 0 | back++; |
445 | 0 | } |
446 | | |
447 | 82 | do { |
448 | 82 | int b = back; |
449 | | |
450 | 82 | cxp = cx; |
451 | 82 | while (b--) |
452 | 0 | cxp = cxp->parent; |
453 | 82 | if (cxp->prepend) |
454 | 0 | cxp->prepend(cxp, NULL, &p, end); |
455 | | |
456 | 82 | back--; |
457 | 82 | } while (back > 0); |
458 | | |
459 | 82 | if (prep) |
460 | 0 | prep(cxp, obj, &p, end); |
461 | | |
462 | 82 | if (_fun) |
463 | 0 | p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "%s: ", _fun); |
464 | | |
465 | | /* |
466 | | * The actual log content |
467 | | */ |
468 | | |
469 | 82 | n = vsnprintf(p, lws_ptr_diff_size_t(end, p), format, vl); |
470 | | |
471 | | /* vnsprintf returns what it would have written, even if truncated */ |
472 | 82 | if (p + n > end - 2) { |
473 | 0 | p = end - 5; |
474 | 0 | *p++ = '.'; |
475 | 0 | *p++ = '.'; |
476 | 0 | *p++ = '.'; |
477 | 0 | *p++ = '\n'; |
478 | 0 | *p++ = '\0'; |
479 | 82 | } else { |
480 | 82 | if (n > 0) { |
481 | 82 | p += n; |
482 | 82 | if (p[-1] == '\n') |
483 | 82 | p--; |
484 | 82 | if (dropped > 1) |
485 | 0 | p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), " (dropped %u logs)", (unsigned int)(dropped - 1)); |
486 | 82 | if (p < end - 1) { |
487 | 82 | *p++ = '\n'; |
488 | 82 | *p = '\0'; |
489 | 82 | } |
490 | 82 | } |
491 | 82 | } |
492 | | |
493 | 82 | if (!strcmp(body_start, prev_buf)) { |
494 | 33 | log_dupes++; |
495 | 33 | if (lws_now_usecs() - last_log_dupe_emit < 1000000) |
496 | 33 | return; |
497 | | |
498 | 0 | p = body_start + strlen(body_start); |
499 | 0 | if (p > buf && p[-1] == '\n') |
500 | 0 | p--; |
501 | 0 | p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), |
502 | 0 | " (swallowed %u dupes)\n", (unsigned int)log_dupes); |
503 | 0 | log_dupes = 0; |
504 | 0 | last_log_dupe_emit = lws_now_usecs(); |
505 | 49 | } else { |
506 | 49 | lws_strncpy(prev_buf, body_start, sizeof(prev_buf)); |
507 | 49 | log_dupes = 0; |
508 | 49 | last_log_dupe_emit = lws_now_usecs(); |
509 | 49 | } |
510 | | |
511 | | /* |
512 | | * The actual emit |
513 | | */ |
514 | | |
515 | 49 | if (cx->lll_flags & LLLF_LOG_CONTEXT_AWARE) |
516 | 0 | cx->u.emit_cx(cx, filter, buf, lws_ptr_diff_size_t(p, buf)); |
517 | 49 | else |
518 | 49 | cx->u.emit(filter, buf); |
519 | 49 | } |
520 | | |
521 | | void _lws_logv(int filter, const char *format, va_list vl) |
522 | 0 | { |
523 | 0 | __lws_logv(&log_cx, NULL, NULL, filter, 0, NULL, format, vl); |
524 | 0 | } |
525 | | |
526 | | void _lws_log(int filter, const char *format, ...) |
527 | 11.3k | { |
528 | 11.3k | va_list ap; |
529 | | |
530 | 11.3k | va_start(ap, format); |
531 | 11.3k | __lws_logv(&log_cx, NULL, NULL, filter, 0, NULL, format, ap); |
532 | 11.3k | va_end(ap); |
533 | 11.3k | } |
534 | | |
535 | | void _lws_log_rl(int filter, uint32_t dropped, const char *format, ...) |
536 | 0 | { |
537 | 0 | va_list ap; |
538 | |
|
539 | 0 | va_start(ap, format); |
540 | 0 | __lws_logv(&log_cx, NULL, NULL, filter, dropped, NULL, format, ap); |
541 | 0 | va_end(ap); |
542 | 0 | } |
543 | | |
544 | | void _lws_log_cx(lws_log_cx_t *cx, lws_log_prepend_cx_t prep, void *obj, |
545 | | int filter, const char *_fun, const char *format, ...) |
546 | 0 | { |
547 | 0 | va_list ap; |
548 | |
|
549 | 0 | if (!cx) |
550 | 0 | cx = &log_cx; |
551 | |
|
552 | 0 | va_start(ap, format); |
553 | 0 | __lws_logv(cx, prep, obj, filter, 0, _fun, format, ap); |
554 | 0 | va_end(ap); |
555 | 0 | } |
556 | | |
557 | | void _lws_log_cx_rl(lws_log_cx_t *cx, lws_log_prepend_cx_t prep, void *obj, |
558 | | int filter, uint32_t dropped, const char *_fun, const char *format, ...) |
559 | 0 | { |
560 | 0 | va_list ap; |
561 | |
|
562 | 0 | if (!cx) |
563 | 0 | cx = &log_cx; |
564 | |
|
565 | 0 | va_start(ap, format); |
566 | 0 | __lws_logv(cx, prep, obj, filter, dropped, _fun, format, ap); |
567 | 0 | va_end(ap); |
568 | 0 | } |
569 | | #endif |
570 | | |
571 | | void |
572 | | lws_set_log_level(int flags, lws_log_emit_t func) |
573 | 0 | { |
574 | 0 | log_cx.lll_flags = (uint32_t)(flags & (~LLLF_LOG_CONTEXT_AWARE)); |
575 | |
|
576 | 0 | if (func) |
577 | 0 | log_cx.u.emit = func; |
578 | 0 | } |
579 | | |
580 | | int lwsl_visible(int level) |
581 | 0 | { |
582 | 0 | return !!(log_cx.lll_flags & (uint32_t)level); |
583 | 0 | } |
584 | | |
585 | | int lwsl_visible_cx(lws_log_cx_t *cx, int level) |
586 | 0 | { |
587 | 0 | return !!(cx->lll_flags & (uint32_t)level); |
588 | 0 | } |
589 | | |
590 | | void |
591 | | lwsl_refcount_cx(lws_log_cx_t *cx, int _new) |
592 | 0 | { |
593 | | #if LWS_MAX_SMP > 1 |
594 | | volatile lws_log_cx_t *vcx = (volatile lws_log_cx_t *)cx; |
595 | | #endif |
596 | |
|
597 | 0 | if (!cx) |
598 | 0 | return; |
599 | | |
600 | | #if LWS_MAX_SMP > 1 |
601 | | if (!vcx->inited) { |
602 | | vcx->inited = 1; |
603 | | lws_pthread_mutex_init(&cx->refcount_lock); |
604 | | vcx->inited = 2; |
605 | | } |
606 | | while (vcx->inited != 2) |
607 | | ; |
608 | | lws_pthread_mutex_lock(&cx->refcount_lock); |
609 | | #endif |
610 | | |
611 | 0 | if (_new > 0) |
612 | 0 | cx->refcount++; |
613 | 0 | else { |
614 | 0 | assert(cx->refcount); |
615 | 0 | cx->refcount--; |
616 | 0 | } |
617 | |
|
618 | 0 | if (cx->refcount_cb) |
619 | 0 | cx->refcount_cb(cx, _new); |
620 | |
|
621 | | #if LWS_MAX_SMP > 1 |
622 | | lws_pthread_mutex_unlock(&cx->refcount_lock); |
623 | | #endif |
624 | 0 | } |
625 | | |
626 | | void |
627 | | lwsl_hexdump_level_cx(lws_log_cx_t *cx, lws_log_prepend_cx_t prep, void *obj, |
628 | | int hexdump_level, const void *vbuf, size_t len) |
629 | 0 | { |
630 | 0 | unsigned char *buf = (unsigned char *)vbuf; |
631 | 0 | unsigned int n; |
632 | |
|
633 | 0 | if (!lwsl_visible_cx(cx, hexdump_level)) |
634 | 0 | return; |
635 | | |
636 | 0 | if (!len) { |
637 | 0 | _lws_log_cx(cx, prep, obj, hexdump_level, NULL, |
638 | 0 | "(hexdump: zero length)\n"); |
639 | 0 | return; |
640 | 0 | } |
641 | | |
642 | 0 | if (!vbuf) { |
643 | 0 | _lws_log_cx(cx, prep, obj, hexdump_level, NULL, |
644 | 0 | "(hexdump: NULL ptr)\n"); |
645 | 0 | return; |
646 | 0 | } |
647 | | |
648 | 0 | _lws_log_cx(cx, prep, obj, hexdump_level, NULL, "\n"); |
649 | |
|
650 | 0 | for (n = 0; n < len;) { |
651 | 0 | unsigned int start = n, m; |
652 | 0 | char line[80], *p = line; |
653 | |
|
654 | 0 | p += lws_snprintf(p, 10, "%04X: ", start); |
655 | |
|
656 | 0 | for (m = 0; m < 16 && n < len; m++) |
657 | 0 | p += lws_snprintf(p, 5, "%02X ", buf[n++]); |
658 | 0 | while (m++ < 16) |
659 | 0 | p += lws_snprintf(p, 5, " "); |
660 | |
|
661 | 0 | p += lws_snprintf(p, 6, " "); |
662 | |
|
663 | 0 | for (m = 0; m < 16 && (start + m) < len; m++) { |
664 | 0 | if (buf[start + m] >= ' ' && buf[start + m] < 127) |
665 | 0 | *p++ = (char)buf[start + m]; |
666 | 0 | else |
667 | 0 | *p++ = '.'; |
668 | 0 | } |
669 | 0 | while (m++ < 16) |
670 | 0 | *p++ = ' '; |
671 | |
|
672 | 0 | *p++ = '\n'; |
673 | 0 | *p = '\0'; |
674 | 0 | _lws_log_cx(cx, prep, obj, hexdump_level, NULL, "%s", line); |
675 | 0 | (void)line; |
676 | 0 | } |
677 | |
|
678 | 0 | _lws_log_cx(cx, prep, obj, hexdump_level, NULL, "\n"); |
679 | 0 | } |
680 | | |
681 | | void |
682 | | lwsl_hexdump_level(int hexdump_level, const void *vbuf, size_t len) |
683 | 0 | { |
684 | 0 | lwsl_hexdump_level_cx(&log_cx, NULL, NULL, hexdump_level, vbuf, len); |
685 | 0 | } |
686 | | |
687 | | void |
688 | | lwsl_hexdump(const void *vbuf, size_t len) |
689 | 0 | { |
690 | 0 | #if defined(_DEBUG) |
691 | 0 | lwsl_hexdump_level(LLL_DEBUG, vbuf, len); |
692 | 0 | #endif |
693 | 0 | } |