Re: message buffering for logs, sprintf, etc...
- From: Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx>
- Date: Tue, 31 May 2005 22:22:15 +0200
Matt Garman wrote:
I'm trying to develop (for my own personal use) a general "messaging facility" in C. The idea is that the facility will be used for both debugging/logging and user interfacing. I'd like it to be fairly portable, but as robust and reliable as possible.
My intent is to develop an API for such general messaging. I'll implement the parts that write to stdout, stderr and files. Later, I'll add support for writing to (for example) syslog, graphical windows, network sockets, etc.
So what I'd like to do is create a printf()-like function (i.e. one that takes variable arguments and is formatted according to the printf() conversion mechanism). This function would then take the user's message, plus some additional accounting information (time message was created, message "level", source file location, etc).
As for making this extendable, I figured I'd typedef something like this:
int message_function(int flags, const char* message);
So I can quickly create new functions that write to arbitrary locations.
Umh, why do you not want to use int message_function(int flags, const char* formatstring, ...); as wrappers for v([s|sn|f]?)printf()?
What this all boils down to is I'll have to use sprintf() and write to a buffer. It looks like there is no perfect way to do this, so I'm trying to get an idea of the pros and cons of the different strategies.
If you have a wrapper for vsnprintf(), you can break down your format string if necessary and use a fixed size buffer.
First question would be static memory versus dynamic memory. Keep in mind that this is a logger/debugger, so I could be writing messages like "out of memory"---which suggests dynamic memory probably isn't the way to go. In other words, I'd like this messaging log to continue working when all else has failed.
But with static memory, obviously, I'm limited to a fixed buffer size. What is too big and what is too little? The accounting information alone could easily occupy 80+ bytes. Plus, I don't like the idea of limiting the length of the user's message.
On the other hand, say I use dynamic memory. Should I just assume that if malloc() isn't available, the system is pretty unusable anyway?
As for dynamically allocating buffers for sprintf(), a bit of research showed that some people fopen() /dev/null (or NUL on Windows), fprintf() to that file pointer, and malloc() with the return value of fprintf().
If you have access to (v?)snprintf() (from the C99 standard library), you can use this instead: size = snprintf(NULL, 0, format, ....);
What about using tmpfile() as a dynamic memory store?
Same argument as with malloc() calls failing.
FWIW, I took a very cursory glance at the glib (www.gtk.org) source, and it looks like it uses malloc() for buffering messages (though I didn't check to see what it does when malloc() fails).
Anyway, any discussion is welcome!
HTH Michael -- E-Mail: Mine is an /at/ gmx /dot/ de address. .
- References:
- message buffering for logs, sprintf, etc...
- From: Matt Garman
- message buffering for logs, sprintf, etc...
- Prev by Date: Re: Formatted IO; %d or %i?
- Next by Date: Re: max size for printf() format conversion?
- Previous by thread: message buffering for logs, sprintf, etc...
- Next by thread: max size for printf() format conversion?
- Index(es):