logging extend level set api to allow setting emission function
Signed-off-by: Andy Green <andy.green@linaro.org>
This commit is contained in:
parent
7e9b427afc
commit
de8f27a80b
9 changed files with 42 additions and 16 deletions
|
@ -262,6 +262,10 @@ available are (OR together the numbers to select multiple)
|
|||
64 EXTENSION
|
||||
128 CLIENT
|
||||
|
||||
Also using lws_set_log_level api you may provide a custom callback to actually
|
||||
emit the log string. By default, this points to an internal emit function
|
||||
that sends to stderr. Setting it to NULL leaves it as it is instead.
|
||||
|
||||
|
||||
Websocket version supported
|
||||
---------------------------
|
||||
|
|
|
@ -44,6 +44,8 @@ int openssl_websocket_private_data_index;
|
|||
#endif
|
||||
|
||||
static int log_level = LLL_ERR | LLL_WARN;
|
||||
static void lwsl_emit_stderr(const char *line);
|
||||
static void (*lwsl_emit)(const char *line) = lwsl_emit_stderr;
|
||||
static const char *log_level_names[] = {
|
||||
"ERR",
|
||||
"WARN",
|
||||
|
@ -3273,35 +3275,50 @@ libwebsocket_ensure_user_space(struct libwebsocket *wsi)
|
|||
return wsi->user_space;
|
||||
}
|
||||
|
||||
|
||||
static void lwsl_emit_stderr(const char *line)
|
||||
{
|
||||
fprintf(stderr, "%s", line);
|
||||
}
|
||||
|
||||
void _lws_log(int filter, const char *format, ...)
|
||||
{
|
||||
char buf[256];
|
||||
va_list ap;
|
||||
int n;
|
||||
int pos = 0;
|
||||
|
||||
if (!(log_level & filter))
|
||||
return;
|
||||
|
||||
for (n = 0; n < LLL_COUNT; n++)
|
||||
if (filter == (1 << n)) {
|
||||
fprintf(stderr, "%s: ", log_level_names[n]);
|
||||
pos = sprintf(buf, "%s: ", log_level_names[n]);
|
||||
break;
|
||||
}
|
||||
|
||||
va_start(ap, format);
|
||||
vfprintf(stderr, format, ap);
|
||||
va_end(ap);
|
||||
vsnprintf(buf + pos, (sizeof buf) - pos, format, ap);
|
||||
buf[(sizeof buf) - 1] = '\0';
|
||||
va_end(ap);
|
||||
|
||||
lwsl_emit(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* lws_set_log_level() - Set the logging bitfield
|
||||
* @level: OR together the LLL_ debug contexts you want output from
|
||||
* @log_emit_function: NULL to leave it as it is, or a user-supplied
|
||||
* function to perform log string emission instead of
|
||||
* the default stderr one.
|
||||
*
|
||||
*
|
||||
* defaults to err and warn contexts enabled
|
||||
* log level defaults to "err" and "warn" contexts enabled only and
|
||||
* emission on stderr.
|
||||
*/
|
||||
|
||||
void lws_set_log_level(int level)
|
||||
void lws_set_log_level(int level, void (*log_emit_function)(const char *line))
|
||||
{
|
||||
log_level = level;
|
||||
if (log_emit_function)
|
||||
lwsl_emit = log_emit_function;
|
||||
}
|
||||
|
||||
|
|
|
@ -655,7 +655,7 @@ struct libwebsocket_extension {
|
|||
};
|
||||
|
||||
LWS_EXTERN
|
||||
void lws_set_log_level(int level);
|
||||
void lws_set_log_level(int level, void (*log_emit_function)(const char *line));
|
||||
|
||||
LWS_EXTERN struct libwebsocket_context *
|
||||
libwebsocket_create_context(int port, const char * interf,
|
||||
|
|
|
@ -353,16 +353,21 @@ having to take any care about data visibility between the processes, it'll
|
|||
<h2>lws_set_log_level - Set the logging bitfield</h2>
|
||||
<i>void</i>
|
||||
<b>lws_set_log_level</b>
|
||||
(<i>int</i> <b>level</b>)
|
||||
(<i>int</i> <b>level</b>,
|
||||
<i>void (*</i><b>log_emit_function</b>) <i>(const char *line)</i>)
|
||||
<h3>Arguments</h3>
|
||||
<dl>
|
||||
<dt><b>level</b>
|
||||
<dd>OR together the LLL_ debug contexts you want output from
|
||||
<dt><b>log_emit_function</b>
|
||||
<dd>NULL to leave it as it is, or a user-supplied
|
||||
function to perform log string emission instead of
|
||||
the default stderr one.
|
||||
</dl>
|
||||
<h3>Description</h3>
|
||||
<blockquote>
|
||||
<p>
|
||||
defaults to err and warn contexts enabled
|
||||
log level defaults to "err" and "warn" contexts enabled only and
|
||||
emission on stderr.
|
||||
</blockquote>
|
||||
<hr>
|
||||
<h2>libwebsocket_write - Apply protocol then write data to client</h2>
|
||||
|
|
|
@ -222,7 +222,7 @@ int main(int argc, char **argv)
|
|||
continue;
|
||||
switch (n) {
|
||||
case 'd':
|
||||
lws_set_log_level(atoi(optarg));
|
||||
lws_set_log_level(atoi(optarg), NULL);
|
||||
break;
|
||||
case 's':
|
||||
use_ssl = 2; /* 2 = allow selfsigned */
|
||||
|
|
|
@ -264,7 +264,7 @@ int main(int argc, char **argv)
|
|||
continue;
|
||||
switch (n) {
|
||||
case 'd':
|
||||
lws_set_log_level(atoi(optarg));
|
||||
lws_set_log_level(atoi(optarg), NULL);
|
||||
break;
|
||||
case 's':
|
||||
use_ssl = 1;
|
||||
|
|
|
@ -339,7 +339,7 @@ int main(int argc, char **argv)
|
|||
continue;
|
||||
switch (n) {
|
||||
case 'd':
|
||||
lws_set_log_level(atoi(optarg));
|
||||
lws_set_log_level(atoi(optarg), NULL);
|
||||
break;
|
||||
case 'm':
|
||||
use_mirror = 1;
|
||||
|
|
|
@ -460,7 +460,7 @@ int main(int argc, char **argv)
|
|||
continue;
|
||||
switch (n) {
|
||||
case 'd':
|
||||
lws_set_log_level(atoi(optarg));
|
||||
lws_set_log_level(atoi(optarg), NULL);
|
||||
break;
|
||||
case 's':
|
||||
use_ssl = 1;
|
||||
|
|
|
@ -417,7 +417,7 @@ int main(int argc, char **argv)
|
|||
continue;
|
||||
switch (n) {
|
||||
case 'd':
|
||||
lws_set_log_level(atoi(optarg));
|
||||
lws_set_log_level(atoi(optarg), NULL);
|
||||
break;
|
||||
case 's':
|
||||
use_ssl = 1;
|
||||
|
|
Loading…
Add table
Reference in a new issue