1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

web: fix proper setting of libwebsocket log level

This commit is contained in:
Steffen Vogel 2021-10-01 07:52:01 -04:00
parent e52e92cdf6
commit 01b9ecec45
3 changed files with 34 additions and 39 deletions

View file

@ -26,6 +26,7 @@
#include <atomic>
#include <thread>
#include <libwebsockets.h>
#include <jansson.h>
#include <villas/log.hpp>
@ -61,7 +62,6 @@ protected:
Api *api;
void worker();
static void lwsLogger(int level, const char *msg);
public:
@ -74,6 +74,9 @@ public:
void start();
void stop();
static void lwsLogger(int level, const char *msg);
static int lwsLogLevel(Log::Level lvl);
/** Parse HTTPd and WebSocket related options */
int parse(json_t *json);

View file

@ -20,7 +20,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <libwebsockets.h>
#include <cstring>
#include <villas/node/config.h>
@ -147,7 +146,7 @@ void Web::lwsLogger(int lws_lvl, const char *msg) {
logger->warn("{}", msg);
break;
case LLL_NOTICE:
case LLL_NOTICE:
case LLL_INFO:
logger->info("{}", msg);
break;
@ -157,6 +156,27 @@ void Web::lwsLogger(int lws_lvl, const char *msg) {
}
}
int Web::lwsLogLevel(Log::Level lvl) {
int lwsLvl = 0;
switch (lvl) {
case Log::Level::trace:
lwsLvl |= LLL_THREAD | LLL_USER | LLL_LATENCY | LLL_CLIENT | LLL_EXT | LLL_HEADER | LLL_PARSER;
case Log::Level::debug:
lwsLvl |= LLL_DEBUG | LLL_NOTICE | LLL_INFO;
case Log::Level::info:
case Log::Level::warn:
lwsLvl |= LLL_WARN;
case Log::Level::err:
lwsLvl |= LLL_ERR;
case Log::Level::critical:
case Log::Level::off:
default: { }
}
return lwsLvl;
}
void Web::worker()
{
lws *wsi;
@ -185,8 +205,7 @@ Web::Web(Api *a) :
htdocs(WEB_PATH),
api(a)
{
/** @todo Port to C++: add LLL_DEBUG and others if trace log level is activated */
lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE, lwsLogger);
lws_set_log_level(lwsLogLevel(logging.getLevel()), lwsLogger);
}
int Web::parse(json_t *json)

View file

@ -37,12 +37,14 @@
#include <villas/tool.hpp>
#include <villas/log.hpp>
#include <villas/uuid.hpp>
#include <villas/web.hpp>
#include "villas-relay.hpp"
typedef char uuid_string_t[37];
using namespace villas;
using namespace villas::node;
namespace villas {
namespace node {
@ -244,7 +246,7 @@ Relay::Relay(int argc, char *argv[]) :
throw RuntimeError("Failed to initialize memory");
/* Initialize logging */
lws_set_log_level((1 << LLL_COUNT) - 1, loggerCallback);
lws_set_log_level(Web::lwsLogLevel(logging.getLevel()), Web::lwsLogger);
protocols = {
{
@ -269,37 +271,6 @@ Relay::Relay(int argc, char *argv[]) :
};
}
void Relay::loggerCallback(int level, const char *msg)
{
auto logger = logging.get("lws");
char *nl = (char *) strchr(msg, '\n');
if (nl)
*nl = 0;
/* Decrease severity for some errors. */
if (strstr(msg, "Unable to open") == msg)
level = LLL_WARN;
switch (level) {
case LLL_ERR:
logger->error("{}", msg);
break;
case LLL_WARN:
logger->warn( "{}", msg);
break;
case LLL_INFO:
logger->info( "{}", msg);
break;
default:
logger->debug("{}", msg);
break;
}
}
int Relay::httpProtocolCallback(lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
{
int ret;
@ -444,6 +415,7 @@ void Relay::parse()
switch (c) {
case 'd':
logging.setLevel(optarg);
lws_set_log_level(Web::lwsLogLevel(logging.getLevel()), Web::lwsLogger);
break;
case 'p':
@ -478,7 +450,8 @@ void Relay::parse()
continue;
check: if (optarg == endptr) {
check:
if (optarg == endptr) {
logger->error("Failed to parse parse option argument '-{} {}'", c, optarg);
exit(EXIT_FAILURE);
}
@ -558,7 +531,7 @@ const lws_http_mount Relay::mount = {
.cache_intermediaries = 0,
.origin_protocol = LWSMPRO_CALLBACK, /* dynamic */
.mountpoint_len = 7, /* char count */
.basic_auth_login_file =nullptr,
.basic_auth_login_file = nullptr,
};
} /* namespace tools */