如何使用 Libwebsockets 启用调试日志记录?

How to enable debug logging with Libwebsockets?

我正在尝试实现一个 websocket 客户端(使用 C 中的 libwebsockets)。我正在使用 cmake 来控制软件编译过程。

我的问题很简单:如何使用 Libwebsockets 启用调试日志记录?

首先,我按照文档中的说明编译并安装了 libwebsockets 关于构建 lws 的注意事项

To build with debug info and _DEBUG for lower priority debug messages compiled in, use

$ cmake .. -DCMAKE_BUILD_TYPE=DEBUG

来自 Libwebsockets 2.1 文档 关于使用 lws 编码的注意事项 (https://libwebsockets.org/lws-api-doc-master/html/md_README.coding.html) :

Debug Logging

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.

A helper function lwsl_emit_syslog() is exported from the library to simplify logging to syslog. You still need to use setlogmask, openlog and closelog in your user code.

The logging apis are made available for user code.

lwsl_err(...) lwsl_warn(...) lwsl_notice(...) lwsl_info(...) lwsl_debug(...) The difference between notice and info is that notice will be logged by default whereas info is ignored by default.

If you are not building with _DEBUG defined, ie, without this

$ cmake .. -DCMAKE_BUILD_TYPE=DEBUG then log levels below notice do not actually get compiled in.

this (official) example 一样,我把 lws_set_log_level(10, NULL); 放在我的 websocket 主函数的开头。

CMakeLists.txt :

cmake_minimum_required(VERSION 3.6)
project(foo)
set(CMAKE_C_STANDARD 11)
set(CMAKE_BUILD_TYPE DEBUG) #For CLion IDE but i'm pretty sur it does nothing.

set(SOURCE_FILES
        websocket.c
        websocket.h
        main.c)

add_executable(foo ${SOURCE_FILES})
target_link_libraries(foo websockets)

然后我 运行 cmake :

cmake .. -DCMAKE_BUILD_TYPE=DEBUG
make

一切正常,我的 websocket 客户端似乎正常。

但是我没有调试日志...我错过了什么?

感谢 lws-team Github :

The build type of DEBUG stops more verbose logging types from being removed during build.

You still need to enable them at runtime using a bitmap in lws_set_log_level()... the default is 7 (err / warn / notice)

我看错了libwebsockets.h :

    enum lws_log_levels {
    LLL_ERR = 1 << 0,
    LLL_WARN = 1 << 1,
    LLL_NOTICE = 1 << 2,
    LLL_INFO = 1 << 3,
    LLL_DEBUG = 1 << 4,
    LLL_PARSER = 1 << 5,
    LLL_HEADER = 1 << 6,
    LLL_EXT = 1 << 7,
    LLL_CLIENT = 1 << 8,
    LLL_LATENCY = 1 << 9,
    LLL_USER = 1 << 10,

    LLL_COUNT = 11 /* set to count of valid flags */
};

像安迪说的,默认是0000 0111,所以7...