Hi Olivier, > On 10/04/2016 02:28 PM, Montorsi, Francesco wrote: > > Yes, but to be honest, that seems a troublesome solution for something > > as easy as logging a string; e.g. by using fopencookie() approach, you > > don't have the concept of "log message", you just provide a function > > that must write a block of bytes somewhere. > > Typically instead, you need to know where a log message starts and > > ends, to e.g., add prefixes/postfixes to it. > > I'm not sure that true if you call setbuf(log_stream, NULL). > > In that case, it looks easy to prefix / postfix messages with a fopencookie > callback like: > > /* example on stdout */ > ssize_t > simple_write(void *c, const char *buf, size_t size) { > ssize_t ret1, ret2, ret3; > > ret1 = fwrite("<", 1, 1, stdout); > if (ret1 == 0) > return 0; > ret2 = fwrite(buf, size, 1, stdout); > if (ret2 == 0) > return 0; > ret3 = fwrite(">", 1, 1, stdout); > if (ret3 == 0) > return 0; > return ret1 + ret2 + ret3; > } > I didn't know about setbuf()... but are we sure that in this way the simple_write() function will always receive a full string? I mean: in the manpage for setbuf() it says:
"... When the first I/O operation occurs on a file, malloc(3) is called, and a buffer is obtained. .... If the argument buf is NULL, only the mode is affected; a new buffer will be allocated on the next read or write operation." But: is it true that 1 write operation corresponds to 1 vfprintf() call? Maybe if you have a "long" a single vfprintf() call may translate to several simple_write() calls... I don't know honestly. > > Indeed, most of the C/C++ (open source) libraries have some simple > > hook that allows the user to have more control on logging... I think > > DPDK should be no exception... :) > > I understand that the current API is a bit more complex, but I don't feel > there > is any blocking issue to do what you want. What do you think? See above. Moreover, IMHO it would be much more user-friendly to have a simple function callback to implement vs having to dig into fopencookie()+setbuf()+etc etc . > Also, I know you've said your patch needs some rework, but as you've also > said you are using it, maybe it would be useful for you to know: > - it makes use of a global variable 'log_buffer', shared by all the pthreads, > which can lead to crashes That's a good point. I will turn it into a __thread variable. Thanks for pointing out this. > - it strips the log messages to 4095 chars Correct, but in my experience DPDK never creates such a long line of log message... Francesco