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 /** \defgroup log Logging
26 *
27 * ##Logging
28 *
29 * Lws provides flexible and filterable logging facilities, which can be
30 * used inside lws and in user code.
31 *
32 * Log categories may be individually filtered bitwise, and directed to built-in
33 * sinks for syslog-compatible logging, or a user-defined function.
34 *
35 * Traditional logs use a single, processwide logging context. New style log
36 * apis (lws_xxx_cx()) can pass the logging context to use in.
37 */
38 ///@{
39
40 #define LLL_ERR (1 << 0)
41 #define LLL_WARN (1 << 1)
42 #define LLL_NOTICE (1 << 2)
43 #define LLL_INFO (1 << 3)
44 #define LLL_DEBUG (1 << 4)
45 #define LLL_PARSER (1 << 5)
46 #define LLL_HEADER (1 << 6)
47 #define LLL_EXT (1 << 7)
48 #define LLL_CLIENT (1 << 8)
49 #define LLL_LATENCY (1 << 9)
50 #define LLL_USER (1 << 10)
51 #define LLL_THREAD (1 << 11)
52
53 #define LLL_COUNT (12) /* set to count of valid flags */
54
55 #define LLLF_SECRECY_PII (1 << 16)
56 /**< contains Personally Identifiable Information */
57 #define LLLF_SECRECY_BEARER (1 << 17)
58 /**< possession of this data allows impersonation */
59
60 #define LLLF_LOG_TIMESTAMP (1 << 18)
61 /**< set to prepend logs with timestamp */
62
63 #define LLLF_LOG_CONTEXT_AWARE (1 << 30)
64 /**< set if the context uses an emit function that takes the logctx, auto-
65 * applied when setting emit using lws_set_log_level_cx() api */
66
67 struct lws_log_cx;
68
69 typedef void (*lws_log_emit_t)(int level, const char *line);
70 typedef void (*lws_log_emit_cx_t)(struct lws_log_cx *cx, int level,
71 const char *line, size_t len);
72 typedef void (*lws_log_prepend_cx_t)(struct lws_log_cx *cx, void *obj,
73 char **p, char *e);
74 typedef void (*lws_log_use_cx_t)(struct lws_log_cx *cx, int _new);
75
76 /*
77 * This is the logging context
78 */
79
80 typedef struct lws_log_cx {
81 union {
82 lws_log_emit_t emit; /* legacy emit function */
83 lws_log_emit_cx_t emit_cx; /* LLLF_LOG_CONTEXT_AWARE */
84 } u;
85 lws_log_use_cx_t refcount_cb;
86 /**< NULL, or a function called after each change to .refcount below,
87 * this enables implementing side-effects like opening and closing
88 * log files when the first and last object binds / unbinds */
89 lws_log_prepend_cx_t prepend;
90 /**< NULL, or a cb to optionally prepend a string to logs we are a
91 * parent of */
92 struct lws_log_cx *parent;
93 /**< NULL, or points to log ctx we are a child of */
94 void *opaque;
95 /**< ignored by lws, used to pass config to emit_cx, eg, filepath */
96 void *stg;
97 /**< ignored by lws, may be used a storage by refcount_cb / emit_cx */
98 uint32_t lll_flags;
99 /**< mask of log levels we want to emit in this context */
100 int32_t refcount;
101 /**< refcount of objects bound to this log context */
102 } lws_log_cx_t;
103
104 /**
105 * lwsl_timestamp: generate logging timestamp string
106 *
107 * \param level: logging level
108 * \param p: char * buffer to take timestamp
109 * \param len: length of p
110 *
111 * returns length written in p
112 */
113 LWS_VISIBLE LWS_EXTERN int
114 lwsl_timestamp(int level, char *p, size_t len);
115
116 #if defined(LWS_PLAT_OPTEE) && !defined(LWS_WITH_NETWORK)
117 #define _lws_log(aaa, ...) SMSG(__VA_ARGS__)
118 #else
119 LWS_VISIBLE LWS_EXTERN void
120 _lws_log(int filter, const char *format, ...) LWS_FORMAT(2);
121 LWS_VISIBLE LWS_EXTERN void
122 _lws_logv(int filter, const char *format, va_list vl);
123 #endif
124
125 struct lws_vhost;
126 struct lws;
127
128 LWS_VISIBLE LWS_EXTERN struct lws_log_cx *
129 lwsl_context_get_cx(struct lws_context *cx);
130 LWS_VISIBLE LWS_EXTERN struct lws_log_cx *
131 lwsl_vhost_get_cx(struct lws_vhost *vh);
132 LWS_VISIBLE LWS_EXTERN struct lws_log_cx *
133 lwsl_wsi_get_cx(struct lws *wsi);
134 #if defined(LWS_WITH_SECURE_STREAMS)
135 struct lws_ss_handle;
136 struct lws_sspc_handle;
137 LWS_VISIBLE LWS_EXTERN struct lws_log_cx *
138 lwsl_ss_get_cx(struct lws_ss_handle *ss);
139 LWS_VISIBLE LWS_EXTERN struct lws_log_cx *
140 lwsl_sspc_get_cx(struct lws_sspc_handle *ss);
141 #endif
142
143 LWS_VISIBLE LWS_EXTERN void
144 lws_log_emit_cx_file(struct lws_log_cx *cx, int level, const char *line,
145 size_t len);
146
147 LWS_VISIBLE LWS_EXTERN void
148 lws_log_use_cx_file(struct lws_log_cx *cx, int _new);
149
150 LWS_VISIBLE LWS_EXTERN void
151 lws_log_prepend_context(struct lws_log_cx *cx, void *obj, char **p, char *e);
152 LWS_VISIBLE LWS_EXTERN void
153 lws_log_prepend_vhost(struct lws_log_cx *cx, void *obj, char **p, char *e);
154 LWS_VISIBLE LWS_EXTERN void
155 lws_log_prepend_wsi(struct lws_log_cx *cx, void *obj, char **p, char *e);
156 #if defined(LWS_WITH_SECURE_STREAMS)
157 LWS_VISIBLE LWS_EXTERN void
158 lws_log_prepend_ss(struct lws_log_cx *cx, void *obj, char **p, char *e);
159 LWS_VISIBLE LWS_EXTERN void
160 lws_log_prepend_sspc(struct lws_log_cx *cx, void *obj, char **p, char *e);
161 #endif
162
163 LWS_VISIBLE LWS_EXTERN void
164 _lws_log_cx(lws_log_cx_t *cx, lws_log_prepend_cx_t prep, void *obj,
165 int filter, const char *_fun, const char *format, ...) LWS_FORMAT(6);
166
167 #define lwsl_cx(_c, _fil, ...) \
168 _lws_log_cx(lwsl_context_get_cx(_c), lws_log_prepend_context, \
169 _c, _fil, __func__, __VA_ARGS__)
170 #define lwsl_vhost(_v, _fil, ...) \
171 _lws_log_cx(lwsl_vhost_get_cx(_v), lws_log_prepend_vhost, _v, \
172 _fil, __func__, __VA_ARGS__)
173 #define lwsl_wsi(_w, _fil, ...) \
174 _lws_log_cx(lwsl_wsi_get_cx(_w), lws_log_prepend_wsi, _w, \
175 _fil, __func__, __VA_ARGS__)
176 #define lwsl_ss(_h, _fil, ...) \
177 _lws_log_cx(lwsl_ss_get_cx(_h), lws_log_prepend_ss, _h, \
178 _fil, __func__, __VA_ARGS__)
179
180 #define lwsl_hexdump_context(_c, _fil, _buf, _len) \
181 lwsl_hexdump_level_cx(lwsl_context_get_cx(_c), \
182 lws_log_prepend_context, \
183 _c, _fil, _buf, _len)
184 #define lwsl_hexdump_vhost(_v, _fil, _buf, _len) \
185 lwsl_hexdump_level_cx(lwsl_vhost_get_cx(_v), \
186 lws_log_prepend_vhost, \
187 _v, _fil, _buf, _len)
188 #define lwsl_hexdump_wsi(_w, _fil, _buf, _len) \
189 lwsl_hexdump_level_cx(lwsl_wsi_get_cx(_w), \
190 lws_log_prepend_wsi, \
191 _w, _fil, _buf, _len)
192 #define lwsl_hexdump_ss(_h, _fil, _buf, _len) \
193 lwsl_hexdump_level_cx(lwsl_ss_get_cx(_h), \
194 lws_log_prepend_ss, \
195 _h, _fil, _buf, _len)
196
197 /*
198 * Figure out which logs to build in or not
199 */
200
201 #if defined(_DEBUG)
202 /*
203 * In DEBUG build, select all logs unless NO_LOGS
204 */
205 #if defined(LWS_WITH_NO_LOGS)
206 #define _LWS_LINIT (LLL_ERR | LLL_USER)
207 #else
208 #define _LWS_LINIT ((1 << LLL_COUNT) - 1)
209 #endif
210 #else /* not _DEBUG */
211 #if defined(LWS_WITH_NO_LOGS)
212 #define _LWS_LINIT (LLL_ERR | LLL_USER)
213 #else
214 #define _LWS_LINIT (LLL_ERR | LLL_USER | LLL_WARN | LLL_NOTICE)
215 #endif
216 #endif /* _DEBUG */
217
218 /*
219 * Create either empty overrides or the ones forced at build-time.
220 * These overrides have the final say... any bits set in
221 * LWS_LOGGING_BITFIELD_SET force the build of those logs, any bits
222 * set in LWS_LOGGING_BITFIELD_CLEAR disable the build of those logs.
223 *
224 * If not defined lws decides based on CMAKE_BUILD_TYPE=DEBUG or not
225 */
226
227 #if defined(LWS_LOGGING_BITFIELD_SET)
228 #define _LWS_LBS (LWS_LOGGING_BITFIELD_SET)
229 #else
230 #define _LWS_LBS 0
231 #endif
232
233 #if defined(LWS_LOGGING_BITFIELD_CLEAR)
234 #define _LWS_LBC (LWS_LOGGING_BITFIELD_CLEAR)
235 #else
236 #define _LWS_LBC 0
237 #endif
238
239 /*
240 * Compute the final active logging bitfield for build
241 */
242 #define _LWS_ENABLED_LOGS (((_LWS_LINIT) | (_LWS_LBS)) & ~(_LWS_LBC))
243
244 /*
245 * Individually enable or disable log levels for build
246 * depending on what was computed
247 */
248
249 /*
250 * Process scope logs
251 */
252
253 #if (_LWS_ENABLED_LOGS & LLL_ERR)
254 #define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
255 #else
256 #define lwsl_err(...) do {} while(0)
257 #endif
258
259 #if (_LWS_ENABLED_LOGS & LLL_WARN)
260 #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
261 #else
262 #define lwsl_warn(...) do {} while(0)
263 #endif
264
265 #if (_LWS_ENABLED_LOGS & LLL_NOTICE)
266 #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
267 #else
268 #define lwsl_notice(...) do {} while(0)
269 #endif
270
271 #if (_LWS_ENABLED_LOGS & LLL_INFO)
272 #define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
273 #else
274 #define lwsl_info(...) do {} while(0)
275 #endif
276
277 #if (_LWS_ENABLED_LOGS & LLL_DEBUG)
278 #define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
279 #else
280 #define lwsl_debug(...) do {} while(0)
281 #endif
282
283 #if (_LWS_ENABLED_LOGS & LLL_PARSER)
284 #define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
285 #else
286 #define lwsl_parser(...) do {} while(0)
287 #endif
288
289 #if (_LWS_ENABLED_LOGS & LLL_HEADER)
290 #define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__)
291 #else
292 #define lwsl_header(...) do {} while(0)
293 #endif
294
295 #if (_LWS_ENABLED_LOGS & LLL_EXT)
296 #define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__)
297 #else
298 #define lwsl_ext(...) do {} while(0)
299 #endif
300
301 #if (_LWS_ENABLED_LOGS & LLL_CLIENT)
302 #define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
303 #else
304 #define lwsl_client(...) do {} while(0)
305 #endif
306
307 #if (_LWS_ENABLED_LOGS & LLL_LATENCY)
308 #define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__)
309 #else
310 #define lwsl_latency(...) do {} while(0)
311 #endif
312
313 #if (_LWS_ENABLED_LOGS & LLL_THREAD)
314 #define lwsl_thread(...) _lws_log(LLL_THREAD, __VA_ARGS__)
315 #else
316 #define lwsl_thread(...) do {} while(0)
317 #endif
318
319 #if (_LWS_ENABLED_LOGS & LLL_USER)
320 #define lwsl_user(...) _lws_log(LLL_USER, __VA_ARGS__)
321 #else
322 #define lwsl_user(...) do {} while(0)
323 #endif
324
325 #define lwsl_hexdump_err(...) lwsl_hexdump_level(LLL_ERR, __VA_ARGS__)
326 #define lwsl_hexdump_warn(...) lwsl_hexdump_level(LLL_WARN, __VA_ARGS__)
327 #define lwsl_hexdump_notice(...) lwsl_hexdump_level(LLL_NOTICE, __VA_ARGS__)
328 #define lwsl_hexdump_info(...) lwsl_hexdump_level(LLL_INFO, __VA_ARGS__)
329 #define lwsl_hexdump_debug(...) lwsl_hexdump_level(LLL_DEBUG, __VA_ARGS__)
330
331 /*
332 * lws_context scope logs
333 */
334
335 #if (_LWS_ENABLED_LOGS & LLL_ERR)
336 #define lwsl_cx_err(_c, ...) lwsl_cx(_c, LLL_ERR, __VA_ARGS__)
337 #else
338 #define lwsl_cx_err(_c, ...) do {} while(0)
339 #endif
340
341 #if (_LWS_ENABLED_LOGS & LLL_WARN)
342 #define lwsl_cx_warn(_c, ...) lwsl_cx(_c, LLL_WARN, __VA_ARGS__)
343 #else
344 #define lwsl_cx_warn(_c, ...) do {} while(0)
345 #endif
346
347 #if (_LWS_ENABLED_LOGS & LLL_NOTICE)
348 #define lwsl_cx_notice(_c, ...) lwsl_cx(_c, LLL_NOTICE, __VA_ARGS__)
349 #else
350 #define lwsl_cx_notice(_c, ...) do {} while(0)
351 #endif
352
353 #if (_LWS_ENABLED_LOGS & LLL_INFO)
354 #define lwsl_cx_info(_c, ...) lwsl_cx(_c, LLL_INFO, __VA_ARGS__)
355 #else
356 #define lwsl_cx_info(_c, ...) do {} while(0)
357 #endif
358
359 #if (_LWS_ENABLED_LOGS & LLL_DEBUG)
360 #define lwsl_cx_debug(_c, ...) lwsl_cx(_c, LLL_DEBUG, __VA_ARGS__)
361 #else
362 #define lwsl_cx_debug(_c, ...) do {} while(0)
363 #endif
364
365 #if (_LWS_ENABLED_LOGS & LLL_PARSER)
366 #define lwsl_cx_parser(_c, ...) lwsl_cx(_c, LLL_PARSER, __VA_ARGS__)
367 #else
368 #define lwsl_cx_parser(_c, ...) do {} while(0)
369 #endif
370
371 #if (_LWS_ENABLED_LOGS & LLL_HEADER)
372 #define lwsl_cx_header(_c, ...) lwsl_cx(_c, LLL_HEADER, __VA_ARGS__)
373 #else
374 #define lwsl_cx_header(_c, ...) do {} while(0)
375 #endif
376
377 #if (_LWS_ENABLED_LOGS & LLL_EXT)
378 #define lwsl_cx_ext(_c, ...) lwsl_cx(_c, LLL_EXT, __VA_ARGS__)
379 #else
380 #define lwsl_cx_ext(_c, ...) do {} while(0)
381 #endif
382
383 #if (_LWS_ENABLED_LOGS & LLL_CLIENT)
384 #define lwsl_cx_client(_c, ...) lwsl_cx(_c, LLL_CLIENT, __VA_ARGS__)
385 #else
386 #define lwsl_cx_client(_c, ...) do {} while(0)
387 #endif
388
389 #if (_LWS_ENABLED_LOGS & LLL_LATENCY)
390 #define lwsl_cx_latency(_c, ...) lwsl_cx(_c, LLL_LATENCY, __VA_ARGS__)
391 #else
392 #define lwsl_cx_latency(_c, ...) do {} while(0)
393 #endif
394
395 #if (_LWS_ENABLED_LOGS & LLL_THREAD)
396 #define lwsl_cx_thread(_c, ...) lwsl_cx(_c, LLL_THREAD, __VA_ARGS__)
397 #else
398 #define lwsl_cx_thread(_c, ...) do {} while(0)
399 #endif
400
401 #if (_LWS_ENABLED_LOGS & LLL_USER)
402 #define lwsl_cx_user(_c, ...) lwsl_cx(_c, LLL_USER, __VA_ARGS__)
403 #else
404 #define lwsl_cx_user(_c, ...) do {} while(0)
405 #endif
406
407 #define lwsl_hexdump_cx_err(_c, ...) lwsl_hexdump_context(_c, LLL_ERR, __VA_ARGS__)
408 #define lwsl_hexdump_cx_warn(_c, ...) lwsl_hexdump_context(_c, LLL_WARN, __VA_ARGS__)
409 #define lwsl_hexdump_cx_notice(_c, ...) lwsl_hexdump_context(_c, LLL_NOTICE, __VA_ARGS__)
410 #define lwsl_hexdump_cx_info(_c, ...) lwsl_hexdump_context(_c, LLL_INFO, __VA_ARGS__)
411 #define lwsl_hexdump_cx_debug(_c, ...) lwsl_hexdump_context(_c, LLL_DEBUG, __VA_ARGS__)
412
413 /*
414 * lws_vhost
415 */
416
417 #if (_LWS_ENABLED_LOGS & LLL_ERR)
418 #define lwsl_vhost_err(_v, ...) lwsl_vhost(_v, LLL_ERR, __VA_ARGS__)
419 #else
420 #define lwsl_vhost_err(_v, ...) do {} while(0)
421 #endif
422
423 #if (_LWS_ENABLED_LOGS & LLL_WARN)
424 #define lwsl_vhost_warn(_v, ...) lwsl_vhost(_v, LLL_WARN, __VA_ARGS__)
425 #else
426 #define lwsl_vhost_warn(_v, ...) do {} while(0)
427 #endif
428
429 #if (_LWS_ENABLED_LOGS & LLL_NOTICE)
430 #define lwsl_vhost_notice(_v, ...) lwsl_vhost(_v, LLL_NOTICE, __VA_ARGS__)
431 #else
432 #define lwsl_vhost_notice(_v, ...) do {} while(0)
433 #endif
434
435 #if (_LWS_ENABLED_LOGS & LLL_INFO)
436 #define lwsl_vhost_info(_v, ...) lwsl_vhost(_v, LLL_INFO, __VA_ARGS__)
437 #else
438 #define lwsl_vhost_info(_v, ...) do {} while(0)
439 #endif
440
441 #if (_LWS_ENABLED_LOGS & LLL_DEBUG)
442 #define lwsl_vhost_debug(_v, ...) lwsl_vhost(_v, LLL_DEBUG, __VA_ARGS__)
443 #else
444 #define lwsl_vhost_debug(_v, ...) do {} while(0)
445 #endif
446
447 #if (_LWS_ENABLED_LOGS & LLL_PARSER)
448 #define lwsl_vhost_parser(_v, ...) lwsl_vhost(_v, LLL_PARSER, __VA_ARGS__)
449 #else
450 #define lwsl_vhost_parser(_v, ...) do {} while(0)
451 #endif
452
453 #if (_LWS_ENABLED_LOGS & LLL_HEADER)
454 #define lwsl_vhost_header(_v, ...) lwsl_vhost(_v, LLL_HEADER, __VA_ARGS__)
455 #else
456 #define lwsl_vhost_header(_v, ...) do {} while(0)
457 #endif
458
459 #if (_LWS_ENABLED_LOGS & LLL_EXT)
460 #define lwsl_vhost_ext(_v, ...) lwsl_vhost(_v, LLL_EXT, __VA_ARGS__)
461 #else
462 #define lwsl_vhost_ext(_v, ...) do {} while(0)
463 #endif
464
465 #if (_LWS_ENABLED_LOGS & LLL_CLIENT)
466 #define lwsl_vhost_client(_v, ...) lwsl_vhost(_v, LLL_CLIENT, __VA_ARGS__)
467 #else
468 #define lwsl_vhost_client(_v, ...) do {} while(0)
469 #endif
470
471 #if (_LWS_ENABLED_LOGS & LLL_LATENCY)
472 #define lwsl_vhost_latency(_v, ...) lwsl_vhost(_v, LLL_LATENCY, __VA_ARGS__)
473 #else
474 #define lwsl_vhost_latency(_v, ...) do {} while(0)
475 #endif
476
477 #if (_LWS_ENABLED_LOGS & LLL_THREAD)
478 #define lwsl_vhost_thread(_v, ...) lwsl_vhost(_v, LLL_THREAD, __VA_ARGS__)
479 #else
480 #define lwsl_vhost_thread(_v, ...) do {} while(0)
481 #endif
482
483 #if (_LWS_ENABLED_LOGS & LLL_USER)
484 #define lwsl_vhost_user(_v, ...) lwsl_vhost(_v, LLL_USER, __VA_ARGS__)
485 #else
486 #define lwsl_vhost_user(_v, ...) do {} while(0)
487 #endif
488
489 #define lwsl_hexdump_vhost_err(_v, ...) lwsl_hexdump_vhost(_v, LLL_ERR, __VA_ARGS__)
490 #define lwsl_hexdump_vhost_warn(_v, ...) lwsl_hexdump_vhost(_v, LLL_WARN, __VA_ARGS__)
491 #define lwsl_hexdump_vhost_notice(_v, ...) lwsl_hexdump_vhost(_v, LLL_NOTICE, __VA_ARGS__)
492 #define lwsl_hexdump_vhost_info(_v, ...) lwsl_hexdump_vhost(_v, LLL_INFO, __VA_ARGS__)
493 #define lwsl_hexdump_vhost_debug(_v, ...) lwsl_hexdump_vhost(_v, LLL_DEBUG, __VA_ARGS__)
494
495
496 /*
497 * lws_wsi
498 */
499
500 #if (_LWS_ENABLED_LOGS & LLL_ERR)
501 #define lwsl_wsi_err(_w, ...) lwsl_wsi(_w, LLL_ERR, __VA_ARGS__)
502 #else
503 #define lwsl_wsi_err(_w, ...) do {} while(0)
504 #endif
505
506 #if (_LWS_ENABLED_LOGS & LLL_WARN)
507 #define lwsl_wsi_warn(_w, ...) lwsl_wsi(_w, LLL_WARN, __VA_ARGS__)
508 #else
509 #define lwsl_wsi_warn(_w, ...) do {} while(0)
510 #endif
511
512 #if (_LWS_ENABLED_LOGS & LLL_NOTICE)
513 #define lwsl_wsi_notice(_w, ...) lwsl_wsi(_w, LLL_NOTICE, __VA_ARGS__)
514 #else
515 #define lwsl_wsi_notice(_w, ...) do {} while(0)
516 #endif
517
518 #if (_LWS_ENABLED_LOGS & LLL_INFO)
519 #define lwsl_wsi_info(_w, ...) lwsl_wsi(_w, LLL_INFO, __VA_ARGS__)
520 #else
521 #define lwsl_wsi_info(_w, ...) do {} while(0)
522 #endif
523
524 #if (_LWS_ENABLED_LOGS & LLL_DEBUG)
525 #define lwsl_wsi_debug(_w, ...) lwsl_wsi(_w, LLL_DEBUG, __VA_ARGS__)
526 #else
527 #define lwsl_wsi_debug(_w, ...) do {} while(0)
528 #endif
529
530 #if (_LWS_ENABLED_LOGS & LLL_PARSER)
531 #define lwsl_wsi_parser(_w, ...) lwsl_wsi(_w, LLL_PARSER, __VA_ARGS__)
532 #else
533 #define lwsl_wsi_parser(_w, ...) do {} while(0)
534 #endif
535
536 #if (_LWS_ENABLED_LOGS & LLL_HEADER)
537 #define lwsl_wsi_header(_w, ...) lwsl_wsi(_w, LLL_HEADER, __VA_ARGS__)
538 #else
539 #define lwsl_wsi_header(_w, ...) do {} while(0)
540 #endif
541
542 #if (_LWS_ENABLED_LOGS & LLL_EXT)
543 #define lwsl_wsi_ext(_w, ...) lwsl_wsi(_w, LLL_EXT, __VA_ARGS__)
544 #else
545 #define lwsl_wsi_ext(_w, ...) do {} while(0)
546 #endif
547
548 #if (_LWS_ENABLED_LOGS & LLL_CLIENT)
549 #define lwsl_wsi_client(_w, ...) lwsl_wsi(_w, LLL_CLIENT, __VA_ARGS__)
550 #else
551 #define lwsl_wsi_client(_w, ...) do {} while(0)
552 #endif
553
554 #if (_LWS_ENABLED_LOGS & LLL_LATENCY)
555 #define lwsl_wsi_latency(_w, ...) lwsl_wsi(_w, LLL_LATENCY, __VA_ARGS__)
556 #else
557 #define lwsl_wsi_latency(_w, ...) do {} while(0)
558 #endif
559
560 #if (_LWS_ENABLED_LOGS & LLL_THREAD)
561 #define lwsl_wsi_thread(_w, ...) lwsl_wsi(_w, LLL_THREAD, __VA_ARGS__)
562 #else
563 #define lwsl_wsi_thread(_w, ...) do {} while(0)
564 #endif
565
566 #if (_LWS_ENABLED_LOGS & LLL_USER)
567 #define lwsl_wsi_user(_w, ...) lwsl_wsi(_w, LLL_USER, __VA_ARGS__)
568 #else
569 #define lwsl_wsi_user(_w, ...) do {} while(0)
570 #endif
571
572 #define lwsl_hexdump_wsi_err(_v, ...) lwsl_hexdump_wsi(_v, LLL_ERR, __VA_ARGS__)
573 #define lwsl_hexdump_wsi_warn(_v, ...) lwsl_hexdump_wsi(_v, LLL_WARN, __VA_ARGS__)
574 #define lwsl_hexdump_wsi_notice(_v, ...) lwsl_hexdump_wsi(_v, LLL_NOTICE, __VA_ARGS__)
575 #define lwsl_hexdump_wsi_info(_v, ...) lwsl_hexdump_wsi(_v, LLL_INFO, __VA_ARGS__)
576 #define lwsl_hexdump_wsi_debug(_v, ...) lwsl_hexdump_wsi(_v, LLL_DEBUG, __VA_ARGS__)
577
578
579 /*
580 * lwsl_ss
581 */
582
583 #if (_LWS_ENABLED_LOGS & LLL_ERR)
584 #define lwsl_ss_err(_w, ...) lwsl_ss(_w, LLL_ERR, __VA_ARGS__)
585 #else
586 #define lwsl_ss_err(_w, ...) do {} while(0)
587 #endif
588
589 #if (_LWS_ENABLED_LOGS & LLL_WARN)
590 #define lwsl_ss_warn(_w, ...) lwsl_ss(_w, LLL_WARN, __VA_ARGS__)
591 #else
592 #define lwsl_ss_warn(_w, ...) do {} while(0)
593 #endif
594
595 #if (_LWS_ENABLED_LOGS & LLL_NOTICE)
596 #define lwsl_ss_notice(_w, ...) lwsl_ss(_w, LLL_NOTICE, __VA_ARGS__)
597 #else
598 #define lwsl_ss_notice(_w, ...) do {} while(0)
599 #endif
600
601 #if (_LWS_ENABLED_LOGS & LLL_INFO)
602 #define lwsl_ss_info(_w, ...) lwsl_ss(_w, LLL_INFO, __VA_ARGS__)
603 #else
604 #define lwsl_ss_info(_w, ...) do {} while(0)
605 #endif
606
607 #if (_LWS_ENABLED_LOGS & LLL_DEBUG)
608 #define lwsl_ss_debug(_w, ...) lwsl_ss(_w, LLL_DEBUG, __VA_ARGS__)
609 #else
610 #define lwsl_ss_debug(_w, ...) do {} while(0)
611 #endif
612
613 #if (_LWS_ENABLED_LOGS & LLL_PARSER)
614 #define lwsl_ss_parser(_w, ...) lwsl_ss(_w, LLL_PARSER, __VA_ARGS__)
615 #else
616 #define lwsl_ss_parser(_w, ...) do {} while(0)
617 #endif
618
619 #if (_LWS_ENABLED_LOGS & LLL_HEADER)
620 #define lwsl_ss_header(_w, ...) lwsl_ss(_w, LLL_HEADER, __VA_ARGS__)
621 #else
622 #define lwsl_ss_header(_w, ...) do {} while(0)
623 #endif
624
625 #if (_LWS_ENABLED_LOGS & LLL_EXT)
626 #define lwsl_ss_ext(_w, ...) lwsl_ss(_w, LLL_EXT, __VA_ARGS__)
627 #else
628 #define lwsl_ss_ext(_w, ...) do {} while(0)
629 #endif
630
631 #if (_LWS_ENABLED_LOGS & LLL_CLIENT)
632 #define lwsl_ss_client(_w, ...) lwsl_ss(_w, LLL_CLIENT, __VA_ARGS__)
633 #else
634 #define lwsl_ss_client(_w, ...) do {} while(0)
635 #endif
636
637 #if (_LWS_ENABLED_LOGS & LLL_LATENCY)
638 #define lwsl_ss_latency(_w, ...) lwsl_ss(_w, LLL_LATENCY, __VA_ARGS__)
639 #else
640 #define lwsl_ss_latency(_w, ...) do {} while(0)
641 #endif
642
643 #if (_LWS_ENABLED_LOGS & LLL_THREAD)
644 #define lwsl_ss_thread(_w, ...) lwsl_ss(_w, LLL_THREAD, __VA_ARGS__)
645 #else
646 #define lwsl_ss_thread(_w, ...) do {} while(0)
647 #endif
648
649 #if (_LWS_ENABLED_LOGS & LLL_USER)
650 #define lwsl_ss_user(_w, ...) lwsl_ss(_w, LLL_USER, __VA_ARGS__)
651 #else
652 #define lwsl_ss_user(_w, ...) do {} while(0)
653 #endif
654
655 #define lwsl_hexdump_ss_err(_v, ...) lwsl_hexdump_ss(_v, LLL_ERR, __VA_ARGS__)
656 #define lwsl_hexdump_ss_warn(_v, ...) lwsl_hexdump_ss(_v, LLL_WARN, __VA_ARGS__)
657 #define lwsl_hexdump_ss_notice(_v, ...) lwsl_hexdump_ss(_v, LLL_NOTICE, __VA_ARGS__)
658 #define lwsl_hexdump_ss_info(_v, ...) lwsl_hexdump_ss(_v, LLL_INFO, __VA_ARGS__)
659 #define lwsl_hexdump_ss_debug(_v, ...) lwsl_hexdump_ss(_v, LLL_DEBUG, __VA_ARGS__)
660
661
662
663 /**
664 * lwsl_hexdump_level() - helper to hexdump a buffer at a selected debug level
665 *
666 * \param level: one of LLL_ constants
667 * \param vbuf: buffer start to dump
668 * \param len: length of buffer to dump
669 *
670 * If \p level is visible, does a nice hexdump -C style dump of \p vbuf for
671 * \p len bytes. This can be extremely convenient while debugging.
672 */
673 LWS_VISIBLE LWS_EXTERN void
674 lwsl_hexdump_level(int level, const void *vbuf, size_t len);
675
676 LWS_VISIBLE LWS_EXTERN void
677 lwsl_hexdump_level_cx(lws_log_cx_t *cx, lws_log_prepend_cx_t prep, void *obj,
678 int hexdump_level, const void *vbuf, size_t len);
679
680 /**
681 * lwsl_hexdump() - helper to hexdump a buffer (DEBUG builds only)
682 *
683 * \param buf: buffer start to dump
684 * \param len: length of buffer to dump
685 *
686 * Calls through to lwsl_hexdump_level(LLL_DEBUG, ... for compatability.
687 * It's better to use lwsl_hexdump_level(level, ... directly so you can control
688 * the visibility.
689 */
690 LWS_VISIBLE LWS_EXTERN void
691 lwsl_hexdump(const void *buf, size_t len);
692
693 /**
694 * lws_is_be() - returns nonzero if the platform is Big Endian
695 */
lws_is_be(void)696 static LWS_INLINE int lws_is_be(void) {
697 const int probe = ~0xff;
698
699 return *(const char *)&probe;
700 }
701
702 /**
703 * lws_set_log_level() - Set the logging bitfield
704 * \param level: OR together the LLL_ debug contexts you want output from
705 * \param log_emit_function: NULL to leave it as it is, or a user-supplied
706 * function to perform log string emission instead of
707 * the default stderr one.
708 *
709 * log level defaults to "err", "warn" and "notice" contexts enabled and
710 * emission on stderr. If stderr is a tty (according to isatty()) then
711 * the output is coloured according to the log level using ANSI escapes.
712 *
713 * You can set the default security level for logging using the
714 * secrecy_and_log_level() macro to set the \p level parameter, eg
715 *
716 * lws_set_log_level(secrecy_and_log_level(LWS_SECRECY_PII, LLL_ERR | LLL_WARN),
717 * my_emit_function);
718 *
719 * Normally you can just leave it at the default.
720 */
721 LWS_VISIBLE LWS_EXTERN void
722 lws_set_log_level(int level, lws_log_emit_t log_emit_function);
723
724 /**
725 * lwsl_emit_syslog() - helper log emit function writes to system log
726 *
727 * \param level: one of LLL_ log level indexes
728 * \param line: log string
729 *
730 * You use this by passing the function pointer to lws_set_log_level(), to set
731 * it as the log emit function, it is not called directly.
732 */
733 LWS_VISIBLE LWS_EXTERN void
734 lwsl_emit_syslog(int level, const char *line);
735
736 /**
737 * lwsl_emit_stderr() - helper log emit function writes to stderr
738 *
739 * \param level: one of LLL_ log level indexes
740 * \param line: log string
741 *
742 * You use this by passing the function pointer to lws_set_log_level(), to set
743 * it as the log emit function, it is not called directly.
744 *
745 * It prepends a system timestamp like [2018/11/13 07:41:57:3989]
746 *
747 * If stderr is a tty, then ansi colour codes are added.
748 */
749 LWS_VISIBLE LWS_EXTERN void
750 lwsl_emit_stderr(int level, const char *line);
751
752 /**
753 * lwsl_emit_stderr_notimestamp() - helper log emit function writes to stderr
754 *
755 * \param level: one of LLL_ log level indexes
756 * \param line: log string
757 *
758 * You use this by passing the function pointer to lws_set_log_level(), to set
759 * it as the log emit function, it is not called directly.
760 *
761 * If stderr is a tty, then ansi colour codes are added.
762 */
763 LWS_VISIBLE LWS_EXTERN void
764 lwsl_emit_stderr_notimestamp(int level, const char *line);
765
766 /**
767 * lwsl_visible() - returns true if the log level should be printed
768 *
769 * \param level: one of LLL_ log level indexes
770 *
771 * This is useful if you have to do work to generate the log content, you
772 * can skip the work if the log level used to print it is not actually
773 * enabled at runtime.
774 */
775 LWS_VISIBLE LWS_EXTERN int
776 lwsl_visible(int level);
777
778 struct lws;
779
780 LWS_VISIBLE LWS_EXTERN const char *
781 lws_wsi_tag(struct lws *wsi);
782
783 LWS_VISIBLE LWS_EXTERN void
784 lwsl_refcount_cx(lws_log_cx_t *cx, int _new);
785
786 ///@}
787