diff --git a/include/libwebsockets/lws-logs.h b/include/libwebsockets/lws-logs.h index d74489205..a108cd751 100644 --- a/include/libwebsockets/lws-logs.h +++ b/include/libwebsockets/lws-logs.h @@ -179,6 +179,36 @@ lws_set_log_level(int level, LWS_VISIBLE LWS_EXTERN void lwsl_emit_syslog(int level, const char *line); +/** + * lwsl_emit_stderr() - helper log emit function writes to stderr + * + * \param level: one of LLL_ log level indexes + * \param line: log string + * + * You use this by passing the function pointer to lws_set_log_level(), to set + * it as the log emit function, it is not called directly. + * + * It prepends a system timestamp like [2018/11/13 07:41:57:3989] + * + * If stderr is a tty, then ansi colour codes are added. + */ +LWS_VISIBLE LWS_EXTERN void +lwsl_emit_stderr(int level, const char *line); + +/** + * lwsl_emit_stderr_notimestamp() - helper log emit function writes to stderr + * + * \param level: one of LLL_ log level indexes + * \param line: log string + * + * You use this by passing the function pointer to lws_set_log_level(), to set + * it as the log emit function, it is not called directly. + * + * If stderr is a tty, then ansi colour codes are added. + */ +LWS_VISIBLE LWS_EXTERN void +lwsl_emit_stderr_notimestamp(int level, const char *line); + /** * lwsl_visible() - returns true if the log level should be printed * diff --git a/lib/core/libwebsockets.c b/lib/core/libwebsockets.c index 71c1a0971..0b2a0b5ac 100644 --- a/lib/core/libwebsockets.c +++ b/lib/core/libwebsockets.c @@ -2066,10 +2066,12 @@ static const char * const colours[] = { "[31m", /* LLL_THREAD */ }; -LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line) +static char tty; + +LWS_VISIBLE void +lwsl_emit_stderr(int level, const char *line) { char buf[50]; - static char tty = 3; int n, m = LWS_ARRAY_SIZE(colours) - 1; if (!tty) @@ -2088,6 +2090,28 @@ LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line) } else fprintf(stderr, "%s%s", buf, line); } + +LWS_VISIBLE void +lwsl_emit_stderr_notimestamp(int level, const char *line) +{ + int n, m = LWS_ARRAY_SIZE(colours) - 1; + + if (!tty) + tty = isatty(2) | 2; + + if (tty == 3) { + n = 1 << (LWS_ARRAY_SIZE(colours) - 1); + while (n) { + if (level & n) + break; + m--; + n >>= 1; + } + fprintf(stderr, "%c%s%s%c[0m", 27, colours[m], line, 27); + } else + fprintf(stderr, "%s", line); +} + #endif LWS_VISIBLE void _lws_logv(int filter, const char *format, va_list vl) diff --git a/lwsws/main.c b/lwsws/main.c index 1f0c09bf3..f74fdf2b9 100644 --- a/lwsws/main.c +++ b/lwsws/main.c @@ -285,7 +285,7 @@ int main(int argc, char **argv) // openlog("lwsws", syslog_options, LOG_DAEMON); #endif - lws_set_log_level(debug_level, NULL); // lwsl_emit_syslog); + lws_set_log_level(debug_level, lwsl_emit_stderr_notimestamp); lwsl_notice("lwsws libwebsockets web server - license CC0 + LGPL2.1\n"); lwsl_notice("(C) Copyright 2010-2018 Andy Green \n");