+static void print_timestamp(FILE *f)
+{
+ struct timeval tv;
+ struct tm *tmp;
+ char ts[32];
+
+ gettimeofday(&tv, NULL);
+ tmp = localtime(&tv.tv_sec);
+ memset(ts, 0, sizeof(ts));
+ strftime(ts, sizeof(ts)-1, "%Y-%m-%d %H:%M:%S", tmp);
+ fprintf(f, "%s.%06ld: ", ts, tv.tv_usec);
+}
+
+static void write_log(char *buf, int len, char *fmt, ...)
+{
+ va_list ap;
+ int i;
+
+ if ((!logfile) && (!verbose))
+ return;
+
+ if (logfile)
+ print_timestamp(logfile);
+ if (verbose)
+ print_timestamp(stdout);
+
+ if (fmt) {
+ if (logfile) {
+ va_start(ap, fmt);
+ vfprintf(logfile, fmt, ap);
+ va_end(ap);
+ }
+ if (verbose) {
+ va_start(ap, fmt);
+ vprintf(fmt, ap);
+ va_end(ap);
+ }
+ }
+
+ if (buf && len) {
+ for (i = 0; i < len; i++) {
+ if (logfile)
+ fprintf(logfile, "%c", buf[i]);
+ if (verbose)
+ printf("%c", buf[i]);
+ }
+ if (logfile)
+ fprintf(logfile, "\n");
+ if (verbose)
+ printf("\n");
+ }
+ if (logfile)
+ fflush(logfile);
+}
+