[Ovmsdev] New can logging
mark at webb-johnson.net
Mon Jul 29 16:23:53 HKT 2019
We don’t really have a good solution for this, and to me the c++ classes are very lacking. It seems simple to want to sprintf to a std::string in an efficient manner, but there is no such functionality. The output string buffer we use works ok (although imho more cumbersome than sprintf), but is a huge overhead.
For can logging, I was trying to keep things as lightweight and efficient as possible. We would like to be able to process thousands of messages a second, so every bit of optimisation helps here. The current crtd implementation was just a simple port of what we had before, and definitely needs improvements. For example, using sprintf for those data byte output in hex is simple but slow. It would much faster to have a lookup table of 0->15 mapping to 0-9,a-f, and index that with number / 16, number % 16 (or number >> 4, and number & 0x0f).
I have made a change to the canformat_crtd to use snprintf (rather than sprintf) in most places - just the data byte sprintf left in two places. But the issue is bigger - 995 results in 141 files when searching the project for ’sprintf’ (most not in our code, but in third party libraries). If someone has the time to review and improve where appropriate, I would be happy to accept. But we need to be very careful when this is changed to ensure the change doesn’t break something. No regression testing, so any impact might be hard to find.
> On 29 Jul 2019, at 12:54 AM, Craig Leres <leres at xse.com> wrote:
> On 2019-07-28 04:10, Mark Webb-Johnson wrote:
>> I tried some more. It seems that the problem is with logging errors in crtd format. I didn’t see it because my bus had no errors.
>> Seems to be just a simple buffer overflow. I’ve committed a fix.
>> Regards, Mark.
>> P.S. I have a relatively big update to can logging going through testing. Some standardisation of functionality for incoming data (such as whether to transmit or simulate it), and a can log playback system.
> Would it be ok to stop using sprintf() completely? Personally I try and use snprintf() and strlcpy() instead of sprintf() and strcpy().
More information about the OvmsDev