This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

snprintf int64_t not working

Hello,

In normal C program, if I want to print int64_t, I can use the following code.

#include <stdio.h>
#include <inttypes.h>

int main()
{
    int64_t t = 1634617662563;
    printf("ts:%"PRIi64" ,\n", t);
    printf("Hello World");

    return 0;
}

/* 
Results
----------------------------------------
ts:1634617662563 ,
Hello World

...Program finished with exit code 0
Press ENTER to exit console.

*/

But it does not work on nRF Connect SDK v1.7.0.

1. Why it prints %li on the terminal

2. The value after %li is not correct.

3. What does missinglog_strdup() means?

Any advice is appreciated.

The code I used to test

#include <zephyr.h>
#include <stdio.h>
#include <inttypes.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(main);

void main(void)
{
    char buf[100];
    
    // Verify k_uptime has value
    LOG_ERR("Uptime: %" PRIi64, k_uptime_get());
    
    // print int64_t using PRIi64
    snprintf(buf, sizeof(buf),
        "{"
            "\"ts\":%" PRIi64 ","
            "\"values\":"
            "{"
                "%d"
            "}},",
        k_uptime_get(), 123);    
    LOG_ERR("data:%s\n",buf);
    

    // print int64_t using %lld
    snprintf(buf, sizeof(buf),
        "{"
            "\"ts\":%lld,"
            "\"values\":"
            "{"
                "%d"
            "}},",
        (long long)k_uptime_get(), 123);    
    LOG_ERR("data:%s\n",buf);
}

Here is the log

2021-10-19T05:29:51.409Z DEBUG modem << õÝuU/}÷Uß}w}·#Õý}õ×µý}}ýõåõUUÕÕUßÕïUí×wW]Õ·OÕý}õ×µýýÿ]õçýu_Õ÷÷ÕUUýõWUWWÕÕõU÷ÕUÕõw÷ÕUÕõw÷wÕUÕõ÷÷×]ÕÕõUÕ_]ÕUýW÷ÕUõõw÷_UÕõõ×_]ÕÕÕW}w]ÕÕõUÕ_UUÕõwuWUÕÕõU}ÕUÕõwuWUÕÕõUÕUÕÕÕW}ÕUÕõwußÕÕÕõWwUÕÕÕW_UÕÕ×W}wÕUÕõWÕWUÕÕÕW}WÕUÕõwu×UÕõõU}_wÕÕÕW}_ÕUÕõW××ÕÕÕõUÕwÝUõõW}}×ý}õõÝý]}ÝõõõýuuußU×}*** Booting Zephyr OS build v2.6.99-ncs1  ***
2021-10-19T05:29:52.421Z DEBUG modem << [00:00:00.213,012] [1;31m<err> main: Uptime: 68719476949[0m
2021-10-19T05:29:52.482Z DEBUG modem << [00:00:00.213,073] [1;31m<err> main: data:{"ts":li,"values":{213}},
2021-10-19T05:29:52.485Z DEBUG modem << [0m
2021-10-19T05:29:52.487Z DEBUG modem << [00:00:00.213,134] [1;31m<err> main: data:{"ts":li,"values":{213}},
2021-10-19T05:29:52.491Z DEBUG modem << [0m
2021-10-19T05:29:52.494Z DEBUG modem << [00:00:01.218,475] [1;31m<err> log: argument 0 in source main log message "data:%s" missinglog_strdup().[0m
2021-10-19T05:29:52.496Z DEBUG modem << [00:00:01.224,975] [1;31m<err> log: argument 0 in source main log message "data:%s" missinglog_strdup().[0m

Here is the prj.conf

# General Config
CONFIG_SERIAL=y
CONFIG_ASSERT=n


# Logging
CONFIG_LOG=y
CONFIG_LOG_DEFAULT_LEVEL=4
CONFIG_STDOUT_CONSOLE=y


# NewLib C
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y

Parents Reply Children
Related