#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

#include "logutils.h"

static struct timeval gettimeofdaystruct();

static int _logLevel=LOG_OUTPUT;
static FILE *_logFile=stderr;

char *LOGLEVELS[]={"ERROR","WARN","OUTPUT","VERBOSE","VVERBOSE","DEBUG","VDEBUG"};

static timeval inittime = gettimeofdaystruct();

static struct timeval gettimeofdaystruct()
{
  struct timeval t;

  gettimeofday(&t, NULL);
  return t;
}

int logSetOutputFile(const char *path)
{
  _logFile=fopen(path,"a");
  if (_logFile==NULL)
    return 0;

  fprintf(_logFile, "-------------------------------------\n");
  time_t t=time(NULL);
  fprintf(_logFile, "Log reopened %s", ctime(&t));
  fprintf(_logFile, "-------------------------------------\n");
  fflush(_logFile);

  return 1;
}


void log(int level, const char *channel, const char *format, ...)
{
  va_list va;
  struct timeval t;
  long elapsedms;

  if (level>_logLevel)
    return;

  gettimeofday(&t, NULL);
  elapsedms=(t.tv_sec-inittime.tv_sec)*1000+(t.tv_usec-inittime.tv_usec)/1000;

  fprintf(_logFile, "%-10s %-10s %05i.%03i ",channel, LOGLEVELS[level],elapsedms/1000,elapsedms%1000);
  va_start(va, format);
  vfprintf(_logFile, format, va);
  fprintf(_logFile,"\n");
  va_end(va);
  fflush(_logFile);
}

void logerrno(int level, const char *channel,  const char *format, ...)
{
  va_list va;

  channel = channel; //silence compiler warning.

  if (level>_logLevel)
    return;

  va_start(va, format);
  vfprintf(_logFile, format, va);
  
  fprintf(_logFile," (errno: %s)\n",strerror(errno));
  va_end(va);
}

int logging(int level)
{
  return (level>_logLevel);
}

void logLevel(int level)
{
  _logLevel=level;
}

int logGetLevel()
{
  return _logLevel;
}

