/****************************************************************************/
/* Copyright (c) 2015 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Power Buoy Data Logger log file writer class                  */
/* Filename : ZSVWriter.cpp                                                 */
/* Author   : Henthorn                                                      */
/* Project  : Power Buoy                                                    */
/* Version  : 1.0                                                           */
/* Created  : 04/21/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
/*                                                                          */
/* This class is meant to be used with data source objects to write source  */
/* data to a log file in a particular format. Need a different format?      */
/* Derive a new class from ZSVWriter and provide the updated functions.     */
/*                                                                          */
/****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>

#include "ZSVWriter.hpp"


// ZSVWriter constructor opens a log file in the directory for writing.
//
ZSVWriter::ZSVWriter(const char *dirname, const char *filename,
   unsigned char new_file_interval)
  : CSVWriter(dirname, filename)
{
   return;

   if (dirname)  _dirname  = strdup(dirname);
   if (filename) _filename = strdup(filename);
   memset(&_xb, 0, sizeof(_xb));
   memset(&_pc, 0, sizeof(_pc));

   // Open a log file
   //
   if (0 != open_log())
   {
      perror("pblog: Could not create log file");
      printf("pblog: %s/%s\n", _dirname, _filename);
   }
}

// Open a log file for writing.
// Use the _dirname and _filename to construct
// the full path name of the file.
// If no filename was specified in the constructor,
// use the current time to construct the name as YYYY.DDD-HH.MM.SS
//
int ZSVWriter::open_log()
{
   char pathname[200];

   _file_open_time = time(&_file_open_time);
   if (!_filename || strlen(_filename) == 0)
   {
      struct tm now;
      localtime_r(&_file_open_time, &now);
      sprintf(pathname, "%s/%4d.%02d.%02dT%02d.%02d.%02d.csv.gz", _dirname,
         now.tm_year+1900, now.tm_mon+1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec);
   }
   else
   {
      sprintf(pathname, "%s/%s.csv.gz", _dirname, _filename);
   }

   printf("Attempting to open %s\n", pathname);
   _zfile = gzopen(pathname, "wb");
   if (!file_open())
   {
      perror("pblog: ");
      return -1;
   }

   write_header();
   return 0;
}

ZSVWriter::~ZSVWriter()
{
   if (_dirname)  delete _dirname;
   if (_filename) delete _filename;
   if (_zfile) gzclose(_zfile);
}

int ZSVWriter::write_to_log(const char* buf)
{
   return gzputs(_zfile, buf);
}

