It is currently Mon, 08 Aug 2022 19:02:59 GMT



 
Author Message
 Request code for log-file mechanism
Dear netters,

We are writing an application that must keep a log-file of the
actions that where taken. It is possible that more than one
process writes to a log file at the same time. It would therefore
be very convenient that the processes can not write to the
same log-file, or even better, that the logging mechanism is
intelligent enough to allow several processes to log things
in the same file, without causing file corruption. Due to the
nature of the application, the logging mechanism must be very
reliable. Furthermore, the application will be running on multiple
platforms, including Sun and VAX,, so a portable routine it
preferred. The application is written in C.

I am sure that such a logging mechanism has been written before
hundreds of times. So I do not want to invent the wheel all over
again. The problem is to find it.

Therefore, can anyone give us a lead where I can obtain a routine
or library in C that implements the above described logging function,
preferably in public domain, or commercial. Or if someone has such a
thing lying around, can you send it to me. It must however be royalty
free and include source code, so we can maintain it.
Also, if you have something that implements only part of our wishes,
we are interested.

Please reply by email.

M.D. Nijdam

-- Marco Nijdam, ma...@westc.nl  OR  mcsun!hp4nl!westc!marco
-- West Consulting bv, Phoenixstraat 49, 2611 AL  Delft, The Netherlands
--                     P.O. Box 3318,    2601 DH  Delft
-- Tel: +31-15-123190, Fax: +31-15-147889



 Sun, 04 Apr 1993 17:46:23 GMT   
 Request code for log-file mechanism

        Depending on how time-critical the logging function is, you might
want to either use a log server (like syslog(), which is already written
and comes with most bsd Unixes) - otherwise, you can roll your own by
just keeping the file open, attempting to lock it, seeking to the eof,
writing, and releasing the lock. Something like:

/*
don't take this as gospel! for the purpose of example, error checks are
omitted. besides, where do you log failures to write to your log ?
*/
#include        <stdio.h>
#include        <sys/file.h>

log(fd,txt)
int     fd;
char    *txt;
{
        int     rv;
        int     ln = strlen(txt);

        while(flock(fd,LOCK_EX))
                sleep(1);

        (void)lseek(fd,0L,SEEK_END);

        rv = (write(fd,txt,ln) == ln);

        (void)flock(fd,LOCK_UN);
        return(rv);



 Mon, 05 Apr 1993 05:31:40 GMT   
 Request code for log-file mechanism

Not only that, they wanted it *portable*.

You're in luck.  ANSI C comes pretty close to what you want.

    Open the log file:

        FILE *log = fopen(LogFileTitle, "a");

    Ensure that the buffer is big enough for the longest message:

        size_t MaxLogLineLength = {whatever};
        char *log_buffer = malloc(MaxLogLineLength+1);

    After having checked everything, set up the buffer:

        setvbuf(log, log_buffer, _IOLBF, MaxLogLineLength+1);

    Now, to write a record to the log file:

        fprintf(log, "{format}\n, {arguments});

What's going on here?  The "a" argument of fopen() says "the file position
indicator is positioned at the end of the file before EACH write".  Making
the buffer big enough for the longest line and selecting line buffering
ensures that each time you write a line using fprintf(), the whole line
will be buffered and then sent out.

You really need to check the fine print, but this *should* work reasonably
well.  That is, after all, why "a" is defined the way it is.  For anything
more reliable, you will have to rely on file locking.

--
Fear most of all to be in error.        -- Kierkegaard, quoting Socrates.



 Mon, 05 Apr 1993 13:45:03 GMT   
 
   [ 3 post ] 

Similar Threads

1. ANNOUNCE: logwrites 1.1 -- A transparent mechanism for logging installation of files

2. Help : how to execute code in object file on request

3. Apache: Logging file requests, rather than URL's

4. The request field in the log file of the Apache server

5. log file of http requests

6. Source code for simple node-locked licensing mechanism?

7. source code for the history mechanism that is provided by the csh command shell

8. mechanism of audit (log) on Unix ?

9. mechanism of audit (log) on Unix ?


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software