1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-09 00:00:04 +01:00

logs: go back to ansi only on tty and add lwsl_emit_stderr_notimestamp

This commit is contained in:
Andy Green 2018-11-13 17:03:33 +08:00
parent 66b73c680c
commit 7d9ff98263
3 changed files with 57 additions and 3 deletions

View file

@ -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
*

View file

@ -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)

View file

@ -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 <andy@warmcat.com>\n");