/* * log.c: * * Copyright (c) 2008 James McKenzie , * All rights reserved. * */ static char rcsid[] = "$Id$"; /* * $Log$ * Revision 1.1 2008/02/14 12:14:50 james * *** empty log message *** * */ #include "project.h" typedef struct { LOG_SIGNATURE; FILE *fp; } File_Log; static void flog_log (Log * _l, char *buf) { File_Log *l = (File_Log *) _l; struct timeval tv = { 0 }; struct tm *tm; time_t t; static const char *days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; static const char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; if (!l->fp) return; gettimeofday (&tv, NULL); t = tv.tv_sec; tm = localtime (&t); fprintf (l->fp, "%s %2d %02d:%02d:%02d.%06d ", months[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec); fputs (buf, l->fp); fputc ('\n', l->fp); fflush (l->fp); } static void flog_close (Log * _l) { File_Log *l = (File_Log *) _l; if (l->fp) fclose (l->fp); free (l); } Log * file_log_new (char *fn) { File_Log *l; FILE *f; f = fopen (fn, "a+"); if (!f) return NULL; l = malloc (sizeof (File_Log)); l->log = flog_log; l->close = flog_close; l->fp = f; return (Log *) l; }