calisto-20000323/
calisto-20000323/lib/
calisto-20000323/lib/etc/
calisto-20000323/lib/players/
calisto-20000323/lib/text/
calisto-20000323/log/
/*
 Calisto (c) 1998-2000 Peter Howkins, Matthew Howkins, Simon Howkins

 $Id: log.c,v 1.5 2000/03/02 21:20:19 peter Exp $

 $Log: log.c,v $
 Revision 1.5  2000/03/02 21:20:19  peter
 Now uses msnprintf()

 Revision 1.4  2000/01/11 20:23:50  peter
 Added use of STRNCOPY and STRNAPPEND
 Added #include "config.h" for autoconf

 Revision 1.3  1999/12/16 18:02:12  peter
 Added #include of <string.h>

 Revision 1.2  1999/12/15 23:27:15  peter
 Moved LOGF variables into log.c

 Revision 1.1  1999/11/29 22:03:52  peter
 Initial revision


 */
static char rcsid[] = "$Id: log.c,v 1.5 2000/03/02 21:20:19 peter Exp $";

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "config.h"
#include "log.h"
#include "msnprintf.h"
#include "strplus.h"

LOGF *usage, *debug, *bug, *idea, *typo;

void log(LOGF *log, const char *format, ...)
{
  va_list ptr;
  time_t now;
  struct tm now2;
  char buffer[256];
  FILE *f;

  /* Prepare formatted time string */
  time(&now);
  now2 = *localtime(&now);
  strftime(buffer, 256, log->dateformat, &now2);
  /* Open file and output log info */
  f = fopen(log->filename, "a");
  if (!f) {
    perror("Could not open logfile");
  } else {
    fprintf(f, "%s ", buffer);
    va_start(ptr, format);
    vfprintf(f, format, ptr);
    va_end(ptr);
    fprintf(f, "\n");
    fclose(f);
  }
  /* Output to stdout also */
/*  printf("%s ", buffer);
  va_start(ptr, format);
  vprintf(format, ptr);
  va_end(ptr);
  printf("\n");*/
}

LOGF *log_init(const char *filename, const char *dateformat)
{
  LOGF *log;

  log = malloc(sizeof(LOGF));
  if (log) {
    STRNCOPY(log->filename, filename, sizeof(log->filename));
    msnprintf(log->dateformat, sizeof(log->dateformat), "[%s]", dateformat);
  }
  return log;
}