M
Matt Garman
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
"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