Xref: utzoo comp.lang.c:32945 comp.unix.programmer:267 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!uakari.primate.wisc.edu!aplcen!haven!decuac!hussar.dco.dec.com!mjr From: mjr@hussar.dco.dec.com (Marcus J. Ranum) Newsgroups: comp.lang.c,comp.unix.programmer Subject: Re: Request code for log-file mechanism Keywords: c log-file source Message-ID: <1990Oct17.213140.19516@decuac.dec.com> Date: 17 Oct 90 21:31:40 GMT References: <1990Oct17.094623.2381@westc.uucp> Sender: news@decuac.dec.com (Network News) Reply-To: mjr@hussar.dco.dec.com (Marcus J. Ranum) Organization: Digital Equipment Corp., Washington Ultrix Resource Center Lines: 35 In article <1990Oct17.094623.2381@westc.uucp> marco@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 #include 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); }