#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>

#include "log.h"

FILE * logfile = NULL;
int logfd;

void doLogMessage(char * s, ...) {
   va_list args;

   if (!logfile) return;

   va_start(args, s);
   fprintf(logfile, "* ");
   vfprintf(logfile, s, args);
   fprintf(logfile, "\n");
   va_end(args);

   fflush(logfile);
}

void openLog(void) {
   if (!testing) {
       logfile = fopen("/dev/tty3", "w");
       if (logfile)
           logfd = open("/dev/tty3", O_WRONLY);
       else {
           logfile = fopen("/tmp/install.log", "a");
           logfd = open("/tmp/install.log", O_WRONLY| O_APPEND);
       }
   } else {
       logfile = fopen("debug.log", "w");
       logfd = open("debug.log", O_WRONLY);
   }
}

void closeLog(void) {
   if (logfile) {
       fclose(logfile);
       close(logfd);
   }
}