/**
 *  TouchTFT plugin for the Video Disk Recorder 
 * 
 *  common.c - A plugin for the Video Disk Recorder
 *
 *  (c) 2004 Lars Tegeler, Sascha Volkenandt 
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 *
 * $Id: common.c,v 1.3 2004/05/25 08:41:15 tegeler Exp $
 *
 */

//includes
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <syslog.h>
#include "debug.h"
#include "setup.h"

using std::string;

// for debuging

void d(int level, const char *fmt, ...)
{
  int set_level = 0; // TouchTFTSetup.Level
  int set_logger = STDOUT;

  auto char    t[BUFSIZ];
  auto va_list ap;
  auto time_t  now;
  FILE* lf = NULL;

  va_start(ap, fmt);
  
  if( set_level >= level )
    {
      switch( set_logger)
	{
	case STDOUT : 
	  {
	    vsnprintf(t + 21, sizeof t - 21, fmt, ap);
	    time(&now);
	    strftime(t, sizeof t, "[TouchTFT: %H:%M:%S]", localtime(&now));
	    t[20] = ' ';
	    printf("%s\033[K", t);
	  } break;
	  
	case SYSLOG :
	  {
	    vsnprintf(t + 10, sizeof t - 10, fmt, ap);
	    memcpy(t, "[TouchTFT] ", 10);
	    syslog(LOG_DEBUG, "%s", t);
	  } break;
	  
	case ASFILE : 
	  {
	    if (lf == (FILE *)NULL)
	      lf = fopen("/tmp/TouchTFT.log", "a");
	    if (lf != (FILE *)NULL) 
	      {
		vsnprintf(t + 20, sizeof t - 20, fmt, ap);
		time(&now);
		strftime(t, sizeof t, "%d.%m.%Y %H:%M:%S", localtime(&now));
		t[19] = ' ';
		fprintf(lf, "%s\n", t);
	      }
	  } break;
	}
    }

  va_end(ap);
}


