Request code for log-file mechanism
Marcus J. Ranum
mjr at hussar.dco.dec.com
Thu Oct 18 07:31:40 AEST 1990
In article <1990Oct17.094623.2381 at westc.uucp> marco at westc.uucp (Marco Nijdam) writes:
>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.
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);
}
More information about the Comp.unix.programmer
mailing list