gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:24:16 72 4
gpt4 key购买 nike

我正在尝试实现一个 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/

72 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com