Search code examples
cdebuggingcmakelibwebsockets

How to enable debug logging with Libwebsockets?


I'm trying to implement a websocket client (with libwebsockets in C). I'm using cmake to control the software compilation process.

My question is simple : How to enable debug logging with Libwebsockets ?

First i compiled and installed libwebsockets like it said in the documentation Notes about building lws :

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

$ cmake .. -DCMAKE_BUILD_TYPE=DEBUG

From Libwebsockets 2.1 doc Notes about coding with 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.

Like in this (official) example, i put lws_set_log_level(10, NULL); at the beginning of my websocket main function.

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)

Then i run cmake :

cmake .. -DCMAKE_BUILD_TYPE=DEBUG
make

Everything works fine, my websocket client seems ok.

But i've no debug logs... What am i missing ?


Solution

  • Tks to the lws-team on 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)

    I misread 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 */
    };
    

    Like Andy said, the default is 0000 0111, so 7...