/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : EventLogServer.cc                                             */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include <time.h>
#include <string.h>
#include "EventLogServer.h"
#include "Syslog.h"

EventLogServer::EventLogServer(const char *logFileName, Boolean append)
{
  _logFileName = strdup(logFileName);

  if (openFile(append) == -1) {
    Syslog::write("EventLogServer::EventLogServer() - openFile() failed\n");
    return;
  }
}


EventLogServer::~EventLogServer()
{
  closeFile();
  free((void *)_logFileName);
}



int EventLogServer::openFile(Boolean append)
{
  if (_fp) {
    closeFile();
  }

  const char *access;
  if (append)
    access = "a";
  else
    access = "w";

  if ((_fp = fopen(_logFileName, access)) == 0) {
    return -1;
  }

  return 0;
}


int EventLogServer::closeFile()
{
  if (!_fp)
    return 0;

  int status = fclose(_fp);
  _fp = 0;
  return status;
}


const char *EventLogServer::fileName()
{
  return _logFileName;
}


long EventLogServer::write(EventLogIF::Level level, 
			   EventLogIF::Message message)
{
  Boolean debug = False;

  dprintf("EventLogServer::write()...\n");

  if (!_fp) {
    Syslog::write("EventLogServer::write() - file not open!\n");
    return -1;
  }

  const time_t sec = time(0);
  char timeBuf[100];
  struct tm *timePtr = gmtime(&sec);

  strftime(timeBuf, sizeof(timeBuf), "%b %d %Y  %H:%M:%S UTC", timePtr);

  char buf[100];
  sprintf(buf, "%s,  %s ", timeBuf, clientName());
  char *ptr = buf + strlen(buf);

  switch (level) {

  case EventLogIF::Info:
    strcpy(ptr, "Info: ");
    break;

  case EventLogIF::Warning:
    strcpy(ptr, "Warning: ");
    break;

  case EventLogIF::Error:
    strcpy(ptr, "Error: ");
    break;

  default:
    strcpy(ptr, "???: ");
  }

  dprintf("EventLogServer::write() %s%s\n", buf, message);
  fprintf(_fp, "%s%s\n", buf, message);
  fflush(_fp);
  return 0;
}



int EventLogServer::spawnAuxTasks()
{
  return 0;
}

