eclipse-wakaama / wakaama

Eclipse Wakaama is a C implementation of the Open Mobile Alliance's LightWeight M2M protocol (LWM2M).
BSD 3-Clause "New" or "Revised" License
498 stars 374 forks source link

Enable COAP logs from cmake #716

Open parmi93 opened 1 year ago

parmi93 commented 1 year ago

It is now possible to enable COAP logs via the cmake file, without having to edit the coap/er-coap-13/er-coap-13.c file. Also usually the printf(...) function is not available with embedded platforms, so it has been replaced with lwm2m_coap_printf(...) which must be defined in the platform.c file.

parmi93 commented 1 year ago
  • Add a format attribute to the function lwm2m_coap_printf(), so that passed arguments can be checked at compile time.

Do you mean add the format like this (in the liblwm2m.h file)?

__attribute__ ((format (printf, 1, 2))) void lwm2m_coap_printf(const char * format, ...);

When I added this attribute the compiler gave me several warnings in the er-coap-13.c file about the format specifies type (which I fixed). Some of the warnings that I fixed:

./lwm2m/wakaama/coap/er-coap-13/er-coap-13.c:658:3: warning: format specifies type 'unsigned long' but the argument has type 'uint32_t' (aka 'unsigned int') [-Wformat]
  COAP_SERIALIZE_BLOCK_OPTION(  COAP_OPTION_BLOCK2,         block2, "Block2")
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./lwm2m/wakaama/coap/er-coap-13/er-coap-13.h:301:43: note: expanded from macro 'COAP_SERIALIZE_BLOCK_OPTION'
      PRINTF(text" [%lu%s (%u B/blk)]\n", coap_pkt->field##_num, coap_pkt->field##_more ? "+" : "", coap_pkt->field##_size); \
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./lwm2m/wakaama/coap/er-coap-13/er-coap-13.c:52:39: note: expanded from macro 'PRINTF'
#define PRINTF(...) lwm2m_coap_printf(__VA_ARGS__)
                                      ^~~~~~~~~~~
./lwm2m/wakaama/coap/er-coap-13/er-coap-13.c:659:3: warning: format specifies type 'unsigned long' but the argument has type 'uint32_t' (aka 'unsigned int') [-Wformat]
  COAP_SERIALIZE_BLOCK_OPTION(  COAP_OPTION_BLOCK1,         block1, "Block1")
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./lwm2m/wakaama/coap/er-coap-13/er-coap-13.h:301:43: note: expanded from macro 'COAP_SERIALIZE_BLOCK_OPTION'
      PRINTF(text" [%lu%s (%u B/blk)]\n", coap_pkt->field##_num, coap_pkt->field##_more ? "+" : "", coap_pkt->field##_size); \
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./lwm2m/wakaama/coap/er-coap-13/er-coap-13.c:52:39: note: expanded from macro 'PRINTF'
#define PRINTF(...) lwm2m_coap_printf(__VA_ARGS__)
parmi93 commented 1 year ago

Do you mean that I should append this in the wakaama/examples/client/CMakeLists.txt file?

# Client without DTLS support and CoAP logs enabled
add_executable(lwm2mclient ${SOURCES})
target_compile_definitions(lwm2mclient PRIVATE LWM2M_CLIENT_MODE LWM2M_BOOTSTRAP LWM2M_SUPPORT_SENML_JSON LWM2M_WITH_COAP_LOGS)
target_sources_wakaama(lwm2mclient)
target_sources_shared(lwm2mclient)
parmi93 commented 1 year ago
  • Add a format attribute to the function lwm2m_coap_printf(), so that passed arguments can be checked at compile time.

Do you mean add the format like this (in the liblwm2m.h file)?

__attribute__ ((format (printf, 1, 2))) void lwm2m_coap_printf(const char * format, ...);

When I added this attribute the compiler gave me several warnings in the er-coap-13.c file about the format specifies type (which I fixed). Some of the warnings that I fixed:

./lwm2m/wakaama/coap/er-coap-13/er-coap-13.c:658:3: warning: format specifies type 'unsigned long' but the argument has type 'uint32_t' (aka 'unsigned int') [-Wformat]
  COAP_SERIALIZE_BLOCK_OPTION(  COAP_OPTION_BLOCK2,         block2, "Block2")
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./lwm2m/wakaama/coap/er-coap-13/er-coap-13.h:301:43: note: expanded from macro 'COAP_SERIALIZE_BLOCK_OPTION'
      PRINTF(text" [%lu%s (%u B/blk)]\n", coap_pkt->field##_num, coap_pkt->field##_more ? "+" : "", coap_pkt->field##_size); \
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./lwm2m/wakaama/coap/er-coap-13/er-coap-13.c:52:39: note: expanded from macro 'PRINTF'
#define PRINTF(...) lwm2m_coap_printf(__VA_ARGS__)
                                      ^~~~~~~~~~~
./lwm2m/wakaama/coap/er-coap-13/er-coap-13.c:659:3: warning: format specifies type 'unsigned long' but the argument has type 'uint32_t' (aka 'unsigned int') [-Wformat]
  COAP_SERIALIZE_BLOCK_OPTION(  COAP_OPTION_BLOCK1,         block1, "Block1")
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./lwm2m/wakaama/coap/er-coap-13/er-coap-13.h:301:43: note: expanded from macro 'COAP_SERIALIZE_BLOCK_OPTION'
      PRINTF(text" [%lu%s (%u B/blk)]\n", coap_pkt->field##_num, coap_pkt->field##_more ? "+" : "", coap_pkt->field##_size); \
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./lwm2m/wakaama/coap/er-coap-13/er-coap-13.c:52:39: note: expanded from macro 'PRINTF'
#define PRINTF(...) lwm2m_coap_printf(__VA_ARGS__)

I would like to point out that after I fix the warnings in the er-coap-13.c file, my system doesn't crash anymore. Prior to this fix with CoAP Logs enabled, my system always crashed when a large amount of logs were generated.

I guess this was due to lines of code like this (in c the behaviour of the code is undefined in these cases): https://github.com/eclipse/wakaama/blob/069b405d101bc0b76dbb794d5e73a9d67e82fef0/coap/er-coap-13/er-coap-13.c#L851-L851

https://github.com/eclipse/wakaama/blob/069b405d101bc0b76dbb794d5e73a9d67e82fef0/coap/er-coap-13/er-coap-13.c#L869-L869

mlasch commented 8 months ago

We moved the default branch from master to main, can you rebase your PR to the latest main on eclipse/wakaama?

LukasWoodtli commented 5 months ago

I did some big refactoring and improvements for the logging infrastructure in Wakaama: #748. The logging in the CoAP part should eventually be incorporated into the rest of Wakaama logging.