/***************************************************************************
                         log.c     -  description
                             -------------------

    begin                : Thu May  14 MET 2002

    copyright            : (C) 1999-2002 by Th. Marquardt, Steffen Hildebrandt

    email                : th.marquardt@t-online.de
                           Steffen.Hildebrandt@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include <ctype.h>

#include "gesko.h"


/* Variablen */
extern int logging;



/*****************************************************************************/
/* Funktion zum Start und Anzeigen des Loggings über serielle Schnittstelle  */
/* ----------------------------------------------------------------------    */
/* In:	                                                                     */
/* ---------------------------------------------------                       */
/* Out:	                                                                     */
/* ----------------------------------------------------                      */
/* Autor: Steffen Hildebrandt                                                */
/* letzte Aenderung: Steffen Hildebrandt 14.05.2002                          */
/*****************************************************************************/
int SerialLogging( int mode )
{
	unsigned char *temp_string = malloc(512);
  unsigned char *text = malloc(512);
  char *puffer = (char*)calloc(512, 1);
  char *puffer2 = (char*)calloc(512, 1);
  char *puffer2_old = (char*)calloc(512, 1);
  cmdtype Cmd;
	FILE *log_fd = NULL;
	time_t zeit;
	const char* format = "%d.%m.%y %H:%M:%S";
	struct tm timestamp;
	char *timestamp_string = (char*)calloc(64, 1);


	switch(mode)
	{
		case LOG_CONSOLE :
			printf("\nLogging auf Konsole...\n");
		 break;

		case LOG_FILE :
			printf("\nLogging in File < geskoctrl.log >...\n");
		 break;

		case LOG_CONSOLE_FILE :
			printf("\nLogging auf Konsole und in File < geskoctrl.log >...\n");
		 break;
	}

	/* falls in File geloggt werden soll... */
	if(mode == LOG_FILE || mode == LOG_CONSOLE_FILE)
	{
		log_fd = fopen("./geskoctrl.log", "w+");
		if(log_fd == NULL)
		{
			printf("FEHLER : Logfile < geskoctrl.log > konnte nicht geoeffnet werden!\n");
			return ERROR;
		}
	}

	printf("HINWEIS : Logging mit Crtl-C beenden!\n");

  Cmd.param_anz = 0;
  sprintf ( Cmd.befehl, "%s", "GTZ1" );
  GTPbxPutAndGet  ( Cmd, text, 1 );

  /* Start Logging... */
  printf("Start Logging...\n");

	logging = TRUE;

	/* Logging-Schleife - kann erst mit Ctrl-C beendet werden! */
	do
	{
		SerialIn(puffer, (char)13);	

		/* falls vorhanden, dann <LF> aus dem String entfernen... */
		if(puffer[0] == (char)10)
			memcpy(puffer2, &puffer[1], strlen(puffer) - 1);
		 else
			memcpy(puffer2, puffer, strlen(puffer));

		/* Daten fuer Timestemp erzeugen... */
		time(&zeit);
		timestamp = *localtime(&zeit);
		strftime(timestamp_string, 18, format, &timestamp);

		if(strlen(puffer2) > 0 && memcmp(puffer2, puffer2_old, strlen(puffer2)) != 0)
		{
			switch(mode)
			{
				case LOG_CONSOLE :
						printf("\n%s : %s", timestamp_string, puffer2);
			 	 break;

				case LOG_FILE :
					sprintf(temp_string, "\n%s : %s", timestamp_string, puffer2);
					fputs(temp_string, log_fd);
					fflush(log_fd);
			 	 break;

				case LOG_CONSOLE_FILE :
					printf("\n%s : %s", timestamp_string, puffer2);
					sprintf(temp_string, "\n%s : %s", timestamp_string, puffer2);
					fputs(temp_string, log_fd);
					fflush(log_fd);
			 	 break;
			}	
		}
		memcpy(puffer2_old, puffer2, strlen(puffer2));
	}
	while(1);
	
  return NO_ERROR;
}

