i am writing a reusable logging module for a firmware prototype. I have a logger api which has a wrapper function void LogWithNum(eLogSubSystem sys, eLogLevel level, char * msg, int * number)
我正在为固件原型编写可重用的日志记录模块。我有一个记录器api,它有一个包装函数void LogWithNum(eLogSubSystem sys,eLogLevel level,char * msg,int * number)
In this wrapper function, a system specific write function is called. Could be UART for example, or a display driver...
在此包装函数中,将调用特定于系统的写入函数。可以是UART,例如显示驱动程序......
All system write / transmit functions somehow require the length in bytes of a message though. And my wrapper function only gets a a char * to the message it should send. See here for example, where my wrapper calls the systems UART transmit function:
所有系统写入/发送功能都以某种方式需要消息的字节长度。而我的包装函数只获取它应该发送的消息的char *。例如,请参阅此处,我的包装器调用系统UART传输功能:
void LogWithNum(eLogSubSystem sys, eLogLevel level, char * msg, int * number)
{
UARTDRV_Transmit(handle, char * msg, int msglen);
}
How can i derive the length of any message so i can pass it properly to the underlying transmitting function?
我如何得出任何消息的长度,以便我可以将其正确传递给底层传输函数?
I dont want to use large librarys since i am on a uC and want to save space.
我不想使用大型图书馆,因为我在uC上并希望节省空间。
1 个解决方案
#1
5
Different ways to solve your problem:
解决问题的不同方法:
Using strlen to calculate the c-string length
使用strlen计算c-string长度
void LogWithNum(eLogSubSystem sys, eLogLevel level, char * msg, int * number)
{
UARTDRV_Transmit(handle, msg, strlen(msg));
}
Or looping through the c-string until null terminator
或者循环遍历c-string,直到null终止符
void LogWithNum(eLogSubSystem sys, eLogLevel level, char * msg, int * number)
{
while (*msg != '\0')
UARTDRV_Transmit(handle, msg++, 1);
}
Or calculate the msg
length without strlen
combining solutions above
或者计算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);
}
#1
5
Different ways to solve your problem:
解决问题的不同方法:
Using strlen to calculate the c-string length
使用strlen计算c-string长度
void LogWithNum(eLogSubSystem sys, eLogLevel level, char * msg, int * number)
{
UARTDRV_Transmit(handle, msg, strlen(msg));
}
Or looping through the c-string until null terminator
或者循环遍历c-string,直到null终止符
void LogWithNum(eLogSubSystem sys, eLogLevel level, char * msg, int * number)
{
while (*msg != '\0')
UARTDRV_Transmit(handle, msg++, 1);
}
Or calculate the msg
length without strlen
combining solutions above
或者计算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);
}