#include "Log.h"

ArquivoLog *AlocaLog(){
	ArquivoLog *Log;
	Log = (ArquivoLog*) malloc (sizeof(ArquivoLog));
	Log->Existe = (int) malloc (sizeof(int));
	Log->Existe = -1;
	Log->Path = (char*) malloc (500 * sizeof(char));
	strcpy(Log->Path, "LogFile.txt");

	return Log;
}

void BuscaArquivoLog(ArquivoLog *Log){

	Log->Arq = fopen(Log->Path, "a+");

	if(Log->Arq == NULL){
		Log->Existe = 0;
	}

	if(Log->Existe == 0){
		Log->Arq = fopen(Log->Path, "a+");
	}

	fclose(Log->Arq);
}

void LogInfo(ArquivoLog *Log, char *Mensagem){
	char *MensagemTempo;
	Log->Arq = fopen(Log->Path, "a+");

	MensagemTempo = timestamp();
	MensagemTempo[24] =	'\t';

	strcat(MensagemTempo, Mensagem);

	fputs(MensagemTempo, Log->Arq);
	fputs("\n", Log->Arq);

	fclose(Log->Arq);
}

char *timestamp(){
	char *TS = (char*) malloc (sizeof(char));
    time_t ltime; /* calendar time */
    ltime=time(NULL); /* get current cal time */

	strcpy(TS, asctime(localtime(&ltime)));
    //printf("%s",asctime( localtime(&ltime) ) );

	return TS;
}