libwebsockets
Lightweight C library for HTML5 websockets
lws-logs.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 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 
36 
37 #define LLL_ERR (1 << 0)
38 #define LLL_WARN (1 << 1)
39 #define LLL_NOTICE (1 << 2)
40 #define LLL_INFO (1 << 3)
41 #define LLL_DEBUG (1 << 4)
42 #define LLL_PARSER (1 << 5)
43 #define LLL_HEADER (1 << 6)
44 #define LLL_EXT (1 << 7)
45 #define LLL_CLIENT (1 << 8)
46 #define LLL_LATENCY (1 << 9)
47 #define LLL_USER (1 << 10)
48 #define LLL_THREAD (1 << 11)
49 
50 #define LLL_COUNT (12) /* set to count of valid flags */
51 
61 LWS_VISIBLE LWS_EXTERN int
62 lwsl_timestamp(int level, char *p, size_t len);
63 
64 #if defined(LWS_PLAT_OPTEE) && !defined(LWS_WITH_NETWORK)
65 #define _lws_log(aaa, ...) SMSG(__VA_ARGS__)
66 #else
67 LWS_VISIBLE LWS_EXTERN void _lws_log(int filter, const char *format, ...) LWS_FORMAT(2);
68 LWS_VISIBLE LWS_EXTERN void _lws_logv(int filter, const char *format, va_list vl);
69 #endif
70 
71 /*
72  * Figure out which logs to build in or not
73  */
74 
75 #if defined(_DEBUG)
76  /*
77  * In DEBUG build, select all logs unless NO_LOGS
78  */
79  #if defined(LWS_WITH_NO_LOGS)
80  #define _LWS_LINIT (LLL_ERR | LLL_USER)
81  #else
82  #define _LWS_LINIT ((1 << LLL_COUNT) - 1)
83  #endif
84 #else /* not _DEBUG */
85 #if defined(LWS_WITH_NO_LOGS)
86 #define _LWS_LINIT (LLL_ERR | LLL_USER)
87 #else
88  #define _LWS_LINIT (LLL_ERR | LLL_USER | LLL_WARN | LLL_NOTICE)
89 #endif
90 #endif /* _DEBUG */
91 
92 /*
93  * Create either empty overrides or the ones forced at build-time.
94  * These overrides have the final say... any bits set in
95  * LWS_LOGGING_BITFIELD_SET force the build of those logs, any bits
96  * set in LWS_LOGGING_BITFIELD_CLEAR disable the build of those logs.
97  *
98  * If not defined lws decides based on CMAKE_BUILD_TYPE=DEBUG or not
99  */
100 
101 #if defined(LWS_LOGGING_BITFIELD_SET)
102  #define _LWS_LBS (LWS_LOGGING_BITFIELD_SET)
103 #else
104  #define _LWS_LBS 0
105 #endif
106 
107 #if defined(LWS_LOGGING_BITFIELD_CLEAR)
108  #define _LWS_LBC (LWS_LOGGING_BITFIELD_CLEAR)
109 #else
110  #define _LWS_LBC 0
111 #endif
112 
113 /*
114  * Compute the final active logging bitfield for build
115  */
116 #define _LWS_ENABLED_LOGS (((_LWS_LINIT) | (_LWS_LBS)) & ~(_LWS_LBC))
117 
118 /*
119  * Individually enable or disable log levels for build
120  * depending on what was computed
121  */
122 
123 #if (_LWS_ENABLED_LOGS & LLL_ERR)
124 #define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
125 #else
126 #define lwsl_err(...) do {} while(0)
127 #endif
128 
129 #if (_LWS_ENABLED_LOGS & LLL_WARN)
130 #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
131 #else
132 #define lwsl_warn(...) do {} while(0)
133 #endif
134 
135 #if (_LWS_ENABLED_LOGS & LLL_NOTICE)
136 #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
137 #else
138 #define lwsl_notice(...) do {} while(0)
139 #endif
140 
141 #if (_LWS_ENABLED_LOGS & LLL_INFO)
142 #define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
143 #else
144 #define lwsl_info(...) do {} while(0)
145 #endif
146 
147 #if (_LWS_ENABLED_LOGS & LLL_DEBUG)
148 #define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
149 #else
150 #define lwsl_debug(...) do {} while(0)
151 #endif
152 
153 #if (_LWS_ENABLED_LOGS & LLL_PARSER)
154 #define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
155 #else
156 #define lwsl_parser(...) do {} while(0)
157 #endif
158 
159 #if (_LWS_ENABLED_LOGS & LLL_HEADER)
160 #define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__)
161 #else
162 #define lwsl_header(...) do {} while(0)
163 #endif
164 
165 #if (_LWS_ENABLED_LOGS & LLL_EXT)
166 #define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__)
167 #else
168 #define lwsl_ext(...) do {} while(0)
169 #endif
170 
171 #if (_LWS_ENABLED_LOGS & LLL_CLIENT)
172 #define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
173 #else
174 #define lwsl_client(...) do {} while(0)
175 #endif
176 
177 #if (_LWS_ENABLED_LOGS & LLL_LATENCY)
178 #define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__)
179 #else
180 #define lwsl_latency(...) do {} while(0)
181 #endif
182 
183 #if (_LWS_ENABLED_LOGS & LLL_THREAD)
184 #define lwsl_thread(...) _lws_log(LLL_THREAD, __VA_ARGS__)
185 #else
186 #define lwsl_thread(...) do {} while(0)
187 #endif
188 
189 #if (_LWS_ENABLED_LOGS & LLL_USER)
190 #define lwsl_user(...) _lws_log(LLL_USER, __VA_ARGS__)
191 #else
192 #define lwsl_user(...) do {} while(0)
193 #endif
194 
195 
196 #define lwsl_hexdump_err(...) lwsl_hexdump_level(LLL_ERR, __VA_ARGS__)
197 #define lwsl_hexdump_warn(...) lwsl_hexdump_level(LLL_WARN, __VA_ARGS__)
198 #define lwsl_hexdump_notice(...) lwsl_hexdump_level(LLL_NOTICE, __VA_ARGS__)
199 #define lwsl_hexdump_info(...) lwsl_hexdump_level(LLL_INFO, __VA_ARGS__)
200 #define lwsl_hexdump_debug(...) lwsl_hexdump_level(LLL_DEBUG, __VA_ARGS__)
201 
212 LWS_VISIBLE LWS_EXTERN void
213 lwsl_hexdump_level(int level, const void *vbuf, size_t len);
214 
225 LWS_VISIBLE LWS_EXTERN void
226 lwsl_hexdump(const void *buf, size_t len);
227 
231 static LWS_INLINE int lws_is_be(void) {
232  const int probe = ~0xff;
233 
234  return *(const char *)&probe;
235 }
236 
248 LWS_VISIBLE LWS_EXTERN void
250  void (*log_emit_function)(int level, const char *line));
251 
261 LWS_VISIBLE LWS_EXTERN void
262 lwsl_emit_syslog(int level, const char *line);
263 
277 LWS_VISIBLE LWS_EXTERN void
278 lwsl_emit_stderr(int level, const char *line);
279 
291 LWS_VISIBLE LWS_EXTERN void
292 lwsl_emit_stderr_notimestamp(int level, const char *line);
293 
303 LWS_VISIBLE LWS_EXTERN int
304 lwsl_visible(int level);
305 
306 struct lws;
307 
308 LWS_VISIBLE LWS_EXTERN const char *
309 lws_wsi_tag(struct lws *wsi);
310 
LWS_VISIBLE LWS_EXTERN void lwsl_hexdump_level(int level, const void *vbuf, size_t len)
LWS_VISIBLE LWS_EXTERN void lws_set_log_level(int level, void(*log_emit_function)(int level, const char *line))
LWS_VISIBLE LWS_EXTERN int lwsl_visible(int level)
LWS_VISIBLE LWS_EXTERN int lwsl_timestamp(int level, char *p, size_t len)
LWS_VISIBLE LWS_EXTERN void lwsl_emit_stderr_notimestamp(int level, const char *line)
LWS_VISIBLE LWS_EXTERN void lwsl_hexdump(const void *buf, size_t len)
LWS_VISIBLE LWS_EXTERN void lwsl_emit_stderr(int level, const char *line)
LWS_VISIBLE LWS_EXTERN void lwsl_emit_syslog(int level, const char *line)