Log4cxx 宏不适用于包含空字符的字符串
我正在开发客户端服务器应用程序。我们有协议格式来从服务器发送和查询数据。 现在我需要在日志文件中记录来自或发送到服务器的数据。 我正在使用最新版本的 Log4cxx。 但这个二进制数据也包含空字符。
一条消息看起来像这样:
ab 40 01 00 00 00 ff f0 00 00 00 00 09 01 01 07 00 00 c0 a8 04 54 ae。
首先我尝试使用 char* pMsg 然后我做了这样的事情 使用 std string str(pMsg ) 两者都不起作用。
然后我做了类似的事情
char chMsg[MAX_PATH];
while(i<nBuffLen)
{
chMsg[i] = *(pMsgBuff+i);
i++;
}
所有方法都失败了,结果是
- 它正确打印了前三个字节,之后就没有了。
- 有时调用 LOG4CXX_INFO 会使应用程序崩溃。
我正在使用 LOG4CXX_INFO 宏来记录信息。
看来这是由于第四个字段中的 NULL 字符造成的。 我发现这个链接声称可以解决问题,但我尝试使用 LOG4CXX_INFO 执行相同的代码,但它崩溃了。 https://issues.apache.org/jira/browse/LOGCXX-162
锄头要解决这个问题吗?
I am working on client server application.we have protocol format to send and query data from Server.
Now I need to log the data coming from or to server on the log file.
I am using latest version of Log4cxx.
But this binary data contains null character as well.
one message lokks like this:
ab 40 01 00 00 00 ff f0 00 00 00 00 09 01 01 07 00 00 c0 a8 04 54 ae.
first I tried with char* pMsg then I did somethig like this
using std string str(pMsg ) both are not working .
then i did something like this
char chMsg[MAX_PATH];
while(i<nBuffLen)
{
chMsg[i] = *(pMsgBuff+i);
i++;
}
All the approach fails to work and the result is
- it is printing first three bytes correctly and none after that.
- Some time call to LOG4CXX_INFO crashes the application.
I am using LOG4CXX_INFO macro to log the info.
It seems it is due to NULL character in the fourth field.
i have found this link which claim to be resolved the issue but I tried the same code with LOG4CXX_INFO it is crashing.
https://issues.apache.org/jira/browse/LOGCXX-162
Hoe to resolve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,您没有正确构建
std::string
。当您直接调用
std::string(chMsg)
时,会涉及strlen
调用来确定您传递的 C 字符串的长度。不幸的是,在您的情况下,根据 C 标准中的定义,C 字符串是一系列以空字符结尾的字符。不过,还有其他 std::string 构造函数:
std::string(InputIt, InputIt)
可以与界定缓冲区的两个指针一起使用。(请参阅cplusplus.com)
如果您使用其中任何一个,那么
std::string
将包含空字符而不是在第一个字符处停止,因此应该正确打印。Note that your are not building the
std::string
appropriately.When you invoke
std::string(chMsg)
directly, astrlen
call is involved to determine the length of the C-String you passed. Unfortunately, in your case, a C-String is by definition in the C Standard a serie of characters ended by a null character.However there are other
std::string
constructors:std::string(char const*, size_t)
let you precise the length of the buffer.std::string(InputIt, InputIt)
can be used with two pointers delimiting the buffer.(see more at cplusplus.com)
If you use either of those, then the
std::string
will contain the null characters instead of stopping at the first of them, and therefore it should be printed correctly.