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

标签 c debugging cmake 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 客户端似乎正常。

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

最佳答案

感谢 Github 上的lws-team:

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...

关于c - 如何使用 Libwebsockets 启用调试日志记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42535181/

相关文章:

c++ - 为什么 C++ 标准指定在具有混合符号的二进制运算中将有符号整数转换为无符号?

c - 警告 : left-hand operand of comma expression has no effect

debugging - 为什么 int 3 生成 64 位的 SIGSEGV 而不是停止调试器?

c - 在集群上调试 MPI 程序时出现问题

c++ - CMake 解析错误函数缺少结尾 ")"。相反,发现未终止的字符串带有文本“)

c++ - CMake 使用 GLOB 结果写入头文件

c - 程序只能在 Debug模式下运行,但不能在正常的 exe 中运行(使用 c lion)?

python - 识别当前正在执行哪个函数调用

c++ - 编译 GLFW 源窗口

c - 头文件中的全局结构