1

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

  1. it is printing first three bytes correctly and none after that.
  2. 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?

Vikram Ranabhatt
  • 7,268
  • 15
  • 70
  • 133
  • 1
    What type is the data stored in? `std::string`? `const char*`? You left out the most important detail. – ildjarn May 26 '11 at 07:19

1 Answers1

0

Note that your are not building the std::string appropriately.

When you invoke std::string(chMsg) directly, a strlen 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.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • the problem is how LOG4CXX_INFO is wrting on the file.I think when LOG4CXX_INFO encounter 0('\0') in the buffer which may be string or whatever it stops writing.I tried your approch as well it is only printing first two byes and not after that. – Vikram Ranabhatt May 26 '11 at 08:31
  • @Chris_vr: whether it writes to a file or not should not be relevant, if the `string` is well built it should be fully printed. Have you tried displaying it on the console (`std::cerr`) to check the string itself ? – Matthieu M. May 26 '11 at 08:41