/********************************************************************* v.1 fifo_logs.cz improved log function for First In, First Out (instead of just truncating the entire file when it reaches a max length) Requirments: ewtoo derivitive talker Installing: replace your old log () function (located in glue.c) with this one.... Notes: if you have MAX_LOG_SIZE set very high... there is a small possiblity that could over write the stack length when processing... tho i doubt this could realistically happen... its not impossible. increaseing STACK_SIZE dramatically is one method to make sure all bases are covered... the second open() call doesnt use O_SYNC... cause it takes about 10 times longer when done syncrously im not sure of all the side effects of this from operating system to operating system but there shouldnt be anything noticable Bug Reports: send to phypor@benland.muc.edu Licences: freely distributable and modifiable, as long as credit given in appropritate places for any distrubitions (be it this alone or within another package) Warranty: non, the author is in no way responsible for use, misuse, abuse, or any other actions misactions, reactions of this code, its provided as is. Author: phypor "if it breaks you get to keep both pieces" ~old linux proverb *********************************************************************/ void log ( char * filename, char * string ) { int fd; int length; char *oldstack = stack; char path[160]; memset (path, 0, 160); sprintf (path, "logs/%s.log", filename); fd = open (path, (O_RDWR | O_CREAT | O_SYNC), (S_IRUSR | S_IWUSR)); length = lseek (fd, 0, SEEK_END); if ((length + strlen (string)) > MAX_LOG_SIZE) { lseek (fd, 0, SEEK_SET); /* put file ptr back to beginning of file */ read (fd, stack, length); /* read it into stack */ close (fd); /* close it */ fd = open (path, (O_WRONLY | O_CREAT | O_TRUNC), (S_IRUSR | S_IWUSR)); /* re-open, truncating it */ stack[length] = '\0'; /* make sure stack is NULL terminated */ stack += strlen (string); /* advance stack ptr length of string */ while (*stack && *stack != '\n') stack++; /* advance stack to next newline */ if (*stack) { stack++; /* advance past newline */ write (fd, stack, strlen (stack)); } stack = oldstack; } sprintf (stack, "%s - %s\n", sys_time (), string); if (!(sys_flags & NO_PRINT_LOG)) printf (stack); write (fd, stack, strlen (stack)); close (fd); }