From 789f759f4cea382053851669bb7076d9a990a2cd Mon Sep 17 00:00:00 2001 From: james <> Date: Tue, 27 Jul 2010 14:49:35 +0000 Subject: add support for byte logging --- src/log.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 6 deletions(-) (limited to 'src/log.c') diff --git a/src/log.c b/src/log.c index 19e7c81..c311ddf 100644 --- a/src/log.c +++ b/src/log.c @@ -10,6 +10,9 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.16 2010/07/27 14:49:35 james + * add support for byte logging + * * Revision 1.15 2010/07/16 11:04:10 james * ignore tedious return values * @@ -65,6 +68,7 @@ typedef struct { int rotate; FILE *fp; char *filename; + int needs_newline; } File_Log; @@ -116,6 +120,18 @@ log_remove (Log * l) *ptr = l->next; } +static void flog_newline(Log *_l,int force) +{ + File_Log *l = (File_Log *) _l; + + if (force || !l->needs_newline) return; + + l->needs_newline=0; + + fputc ('\n', l->fp); + fflush (l->fp); +} + static void flog_sighup (Log * _l) @@ -131,9 +147,8 @@ flog_sighup (Log * _l) log_f (_l, ""); } - -static void -flog_log (Log * _l, char *buf) +static void +flog_emit_stamp(Log *_l) { File_Log *l = (File_Log *) _l; struct timeval tv = { 0 }; @@ -150,16 +165,20 @@ flog_log (Log * _l, char *buf) if (!l->fp) return; + flog_newline(_l,0); + gettimeofday (&tv, NULL); t = tv.tv_sec; tm = localtime (&t); fprintf (l->fp, "%s %2d %02d:%02d:%02d ", months[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); +} - fputs (buf, l->fp); - fputc ('\n', l->fp); - fflush (l->fp); + +static void flog_check_rotate(Log *_l) +{ + File_Log *l = (File_Log *) _l; if (l->rotate && rotate_check (l->filename)) { fclose (l->fp); @@ -169,6 +188,46 @@ flog_log (Log * _l, char *buf) } +static void +flog_log_bytes (Log * _l, void *_buf,int len) +{ + File_Log *l = (File_Log *) _l; + uint8_t *buf=(uint8_t *) _buf; + + if (!l->fp) + return; + + while (len--) { + if (*buf=='\n') { + flog_newline(_l,1); + flog_check_rotate(_l); + flog_emit_stamp(_l); + } else { + l->needs_newline++; + fputc (*buf, l->fp); + } + buf++; + } +} + +static void +flog_log (Log * _l, char *buf) +{ + File_Log *l = (File_Log *) _l; + + if (!l->fp) + return; + + flog_emit_stamp(_l); + + fputs (buf, l->fp); + fputc ('\n', l->fp); + fflush (l->fp); + + flog_check_rotate(_l); +} + + static void @@ -204,11 +263,14 @@ file_log_new (char *fn, int rotate) l->log = flog_log; + l->log_bytes = flog_log_bytes; l->close = flog_close; l->do_close = dc; l->rotate = rotate; l->filename = strdup (fn); + l->needs_newline=0; + fput_cp (l->fp, 0xffef); log_add ((Log *) l); -- cgit v1.2.3