Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update print log fmt #153

Closed
wants to merge 8 commits into from

Conversation

chinglee-iot
Copy link
Member

Description

Log format is different for different compiler.
For example, the following code can be compiled with GCC 32 bits and 64 bits.

uint32_t sentLen = 0;
LogDebug( ( "PktioSendData sent %u bytes", sentLen ) );

However, there will be format warning with GNU arm compiler.

FreeRTOS-Cellular-Interface/source/cellular_pktio.c:1430:17: error: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'uint32_t' {aka 'long unsigned int'} [-Werror=format=]
 1430 |     LogDebug( ( "PktioSendData sent %u bytes", sentLen ) );
      |                 ^~~~~~~~~~~~~~~~~~~~~~         ~~~~~~~
      |                                                |
      |                                                uint32_t {aka long unsigned int}

Although C99 defines inttypes.h for this problem, this library doesn't reply on inttypes.h.

In this PR,

  • The following log format configs are defined for different compiler
    • CELLULAR_LOG_FMT_UINT32_HEX
    • CELLULAR_LOG_FMT_UINT32
    • CELLULAR_LOG_FMT_UINT16
    • CELLULAR_LOG_FMT_UINT8
    • CELLULAR_LOG_FMT_INT32
    • CELLULAR_LOG_FMT_INT16
    • CELLULAR_LOG_FMT_INT8
    • CELLULAR_LOG_FMT_PTR
    • CELLULAR_LOG_FMT_STR
    • CELLULAR_LOG_FMT_SIZE
    • CELLULAR_LOG_FMT_CHAR

User with GNU ARM compiler can define the following in celllular_config.h

#define CELLULAR_LOG_FMT_SIZE "u"
#define CELLULAR_LOG_FMT_UINT32 "lu"
#define CELLULAR_LOG_FMT_INT32 "ld"

Or using PRIx macros if inttypes.h is preferred

#include <inttypes.h>

#define CELLULAR_LOG_FMT_SIZE PRIu16
#define CELLULAR_LOG_FMT_UINT32 PRIu32
#define CELLULAR_LOG_FMT_INT32 PRIi32

Test Steps

Reference this branch https://github.com/chinglee-iot/FreeRTOS-Cellular-Interface/tree/add-compiler-warning-check
GCC
Build with the following command.

cmake -S test -B build -DCMAKE_C_COMPILER_WORKS=1
cd build
make compilation_test

GNU arm
Add the following config in test/unit-test/configs/compilation-test/cellular_config.h

#define CELLULAR_LOG_FMT_SIZE "u"
#define CELLULAR_LOG_FMT_UINT32 "lu"
#define CELLULAR_LOG_FMT_INT32 "ld"

Build with the following command.

CC=arm-none-eabi-gcc cmake -B build -S test -DCMAKE_C_COMPILER_WORKS=1
cd build
make compilation_test

Checklist:

  • I have tested my changes. No regression in existing tests.
  • I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue

#151
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@chinglee-iot chinglee-iot mentioned this pull request Oct 12, 2023
2 tasks
@chinglee-iot
Copy link
Member Author

Create another PR #154 for this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant