如何确定generice记录器功能的char*的长度?

Iam*_*fox 0 c logging

我正在为固件原型编写可重用的日志记录模块.我有一个记录器api,它有一个包装函数void LogWithNum(eLogSubSystem sys, eLogLevel level, char * msg, int * number)

在此包装函数中,将调用特定于系统的写入函数.可以是UART,例如显示驱动程序......

所有系统写入/发送功能都需要以消息的字节长度为单位.而我的包装器函数只获取它应该发送的消息的char*.例如,请参阅此处,我的包装器调用系统UART传输功能:

void LogWithNum(eLogSubSystem sys, eLogLevel level, char * msg, int * number)
{

     UARTDRV_Transmit(handle, char * msg, int msglen);
}
Run Code Online (Sandbox Code Playgroud)

我如何得出任何消息的长度,以便我可以将其正确传递给底层传输函数?

我不想使用大型图书馆,因为我在uC上并希望节省空间.

LPs*_*LPs 5

解决问题的不同方法:

使用strlen计算长度

void LogWithNum(eLogSubSystem sys, eLogLevel level, char * msg, int * number)
{
     UARTDRV_Transmit(handle, msg, strlen(msg));
}
Run Code Online (Sandbox Code Playgroud)

或者循环遍历直到null终止符

void LogWithNum(eLogSubSystem sys, eLogLevel level, char * msg, int * number)
{
     while (*msg != '\0')
        UARTDRV_Transmit(handle, msg++, 1);
}
Run Code Online (Sandbox Code Playgroud)

或者计算msg长度而不strlen结合上述解决方案

void LogWithNum(eLogSubSystem sys, eLogLevel level, char * msg, int * number)
{
     int msg_length = 0;

     while (*msg++ != '\0')
        msg_length++;

     if (msg_length > 0)
        UARTDRV_Transmit(handle, msg, msg_length);
}
Run Code Online (Sandbox Code Playgroud)