message buffering for logs, sprintf, etc...



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.

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.

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().

What about using tmpfile() as a dynamic memory store?

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!

Thank you,
Matt

--
Matt Garman
email at: http://raw-sewage.net/index.php?file=email
.



Relevant Pages

  • Re: Dynamic Memory allocation
    ... handle dynamic memory allocations any way they wish as long as they conform ... function such as malloc ... Buffer = malloc; ...
    (comp.lang.c)
  • Re: message buffering for logs, sprintf, etc...
    ... "messaging facility" in C. ... My intent is to develop an API for such general messaging. ... format string if necessary and use a fixed size buffer. ... Windows), fprintfto that file pointer, and malloc() with the ...
    (comp.lang.c)
  • Re: Static version of bignum library
    ... I wrote some RSA code a few years ago where the functions all ... take a parameter "TMP" meaning "here's a temporary buffer of size foo * ... various values of foo depending upon the function being used). ... > don't want to support dynamic memory allocation. ...
    (comp.programming)
  • Re: Static version of bignum library
    ... I wrote some RSA code a few years ago where the functions all ... take a parameter "TMP" meaning "here's a temporary buffer of size foo * ... various values of foo depending upon the function being used). ... > don't want to support dynamic memory allocation. ...
    (sci.crypt)
  • Re: Problem with malloc
    ... >> I have the following problem, I must to do my assignment, but I really do ... >> not know how to use the malloc. ... start out with that and modify it to use dynamic memory. ... Don't ask for homework solutions in usenet. ...
    (comp.lang.c)