#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "tsplog.h"
#include "Individual.h"
TspLog::TspLog(int iLogLevel0, int iScreenLogLevel0, char *sLogDir, char *sBaseLogName)
{
char sTemp[255];
bool bOk;
iStartLogTime = time(NULL);
iLogLevel = iLogLevel0;
iScreenLogLevel = iScreenLogLevel0;
iNiveau=0;
strcpy(sNiveau, "");
sprintf(sTemp, "%s/%s.log", sLogDir, sBaseLogName);
fGlobalLog = fopen(sTemp, "w");
bOk = (fGlobalLog!=NULL);
if (bOk)
{
sprintf(sTemp, "%s/%s.html", sLogDir, sBaseLogName);
fHtmlLog = fopen(sTemp, "w");
bOk = (fHtmlLog != NULL);
}
if (bOk)
{
fprintf(fHtmlLog, "<html>\n");
fprintf(fHtmlLog, "<head>\n");
fprintf(fHtmlLog, " <title>%s</title>\n", sBaseLogName);
fprintf(fHtmlLog, "</head>\n");
fprintf(fHtmlLog, "<body bgcolor=\"#ffffff\">\n");
sprintf(sTemp, "%s/%s.time", sLogDir, sBaseLogName);
fTimeLog = fopen(sTemp, "w");
bOk = (fTimeLog != NULL);
}
if (bOk)
{
CreateGnuPlotFile(sLogDir, sBaseLogName);
}
}
void TspLog::CreateGnuPlotFile(char *sLogDir, char *sBaseLogName)
{
FILE* fGnuPlot;
char sGnuPlotFileName[255];
char sPngFileName[255];
char sTimeLogName[255];
sprintf(sGnuPlotFileName, "%s/%s.plot", sLogDir, sBaseLogName);
sprintf(sPngFileName, "%s.png", sBaseLogName);
sprintf(sTimeLogName, "%s.time", sBaseLogName);
fGnuPlot = fopen(sGnuPlotFileName, "w");
fprintf(fGnuPlot, "set terminal png small color\n");
fprintf(fGnuPlot, "set xlabel \"Temps\"\n");
fprintf(fGnuPlot, "set ylabel \"-Distance totale du parcours\"\n");
fprintf(fGnuPlot, "set autoscale\n");
fprintf(fGnuPlot, "set out \"%s\"\n", sPngFileName);
fprintf(fGnuPlot, "plot '%s' title \"%s\" with lines\n", sTimeLogName,sTimeLogName);
fclose(fGnuPlot);
}
TspLog::~TspLog()
{
fclose(fGlobalLog);
fclose(fTimeLog);
fclose(fHtmlLog);
}
void TspLog::GlobalLog()
{
if (iCurrentLevel <= iLogLevel)
{
fprintf(fGlobalLog, "%s%s\n", sNiveau, sCurrentMessage);
fflush(fGlobalLog);
}
if (iCurrentLevel <= iScreenLogLevel)
{
fprintf(stdout, "%s%s\n", sNiveau, sCurrentMessage);
fflush(stdout);
}
}
void TspLog::DebugMsg(char *sMessage)
{
iCurrentLevel = LEVEL_DEBUG;
sprintf(sCurrentMessage, "<%s> : %s", sFunctionName, sMessage);
GlobalLog();
}
void TspLog::ScreenMsg(char *sMessage)
{
iCurrentLevel = LEVEL_DEBUG;
if (iCurrentLevel <= iScreenLogLevel)
{
fprintf(stdout, "%s%s\n", sNiveau, sMessage);
fflush(stdout);
}
}
void TspLog::InfoMsg(char *sMessage)
{
iCurrentLevel = LEVEL_INFO;
sprintf(sCurrentMessage, "<%s> : %s", sFunctionName, sMessage);
GlobalLog();
}
void TspLog::ErrorMsg(char *sMessage)
{
iCurrentLevel = LEVEL_CRITICAL;
sprintf(sCurrentMessage, "*** ERREUR : <%s> : %s", sFunctionName, sMessage);
GlobalLog();
}
void TspLog::ProjectInfoMsg(char *_project_info, char *_project_version, char *_project_author)
{
iCurrentLevel = LEVEL_INFO;
sprintf(sCurrentMessage, "Project info : %s\nProject version : %s\nProject author : %s",
_project_info, _project_version, _project_author);
GlobalLog();
}
void TspLog::CalculeChaineNiveau()
{
int i;
strcpy(sNiveau, "");
for (i=0; i<iNiveau; i++)
{
strcat(sNiveau, " ");
}
}
void TspLog::StartFunction(char *sFunctionName0, int iInstance0, int iCurrentLevel0, int iNbIteration0)
{
sprintf(sFunctionName, "%s[%d]", sFunctionName0, iInstance0);
iNbIteration = iNbIteration0;
iCurrentLevel=iCurrentLevel0;
CalculeChaineNiveau();
iNiveau++;
sprintf(sCurrentMessage, "===> Début <%s>", sFunctionName);
GlobalLog();
if (iNbIteration > 0)
{
iStartTime = time(NULL);
sprintf(sCurrentMessage, "<%s> : Nb itérations prévues : %d", sFunctionName, iNbIteration);
GlobalLog();
}
}
void TspLog::EndFunction(char *sFunctionName0, int iInstance0)
{
double lfNbIterParSeconde;
iNiveau--;
CalculeChaineNiveau();
sprintf(sFunctionName, "%s[%d]", sFunctionName0, iInstance0);
iCurrentLevel = LEVEL_DEBUG;
if (iNbIteration > 0)
{
iEndTime = time(NULL);
if ((iEndTime-iStartTime) != 0)
{
lfNbIterParSeconde = (float)(iNbIteration)/(float)(iEndTime-iStartTime);
}
else
{
lfNbIterParSeconde = (float)(iNbIteration);
}
sprintf(sCurrentMessage, "<%s> : Temps de calcul = %d secondes (%f itérations/secondes)", sFunctionName, iEndTime-iStartTime, lfNbIterParSeconde);
GlobalLog();
}
sprintf(sCurrentMessage, "<=== Fin <%s>", sFunctionName);
GlobalLog();
}
void TspLog::LogRecord(int iCurrentIteration,
char *sPath,
double lfFitness,
int iNbCities,
bool bDefaultProblem,
bool bTSPLIB,
char *sCitiesPosX,
char *sCitiesPosY)
{
int iTempsCalcul;
iTempsCalcul = TimeLog(lfFitness);
iCurrentLevel = LEVEL_INFO;
sprintf(sCurrentMessage, "<%s> (itération %d/%d) (%d s) :\tNouveau record : <<< %f >>>", sFunctionName, iCurrentIteration, iNbIteration, iTempsCalcul, -lfFitness);
GlobalLog();
if (fHtmlLog != NULL)
{
fprintf(fHtmlLog, "<applet code=\"DisplayTsp.class\" width=200 height=200>\n");
if (bDefaultProblem)
{
fprintf(fHtmlLog, "<param name=Problem value=\"default\">\n");
}
else
{
if (bTSPLIB)
{
fprintf(fHtmlLog, "<param name=ProblemType value=\"tsplib\">\n");
}
fprintf(fHtmlLog, "<param name=Problem value=\"custom\">\n");
fprintf(fHtmlLog, "<param name=NbCities value=\"%d\">\n", iNbCities);
fprintf(fHtmlLog, "<param name=CitiesPosX value=\"%s\">\n", sCitiesPosX);
fprintf(fHtmlLog, "<param name=CitiesPosY value=\"%s\">\n", sCitiesPosY);
}
fprintf(fHtmlLog, "<param name=Parcours value=\"%s\">\n", sPath);
fprintf(fHtmlLog, "<hr>\n</applet><br>\n");
fprintf(fHtmlLog, "<i>%s (%d s)</i><br><br>\n", sFunctionName, iTempsCalcul);
fflush(fHtmlLog);
}
}
int TspLog::TimeLog(double lfFitness)
{
int iTempsCalcul;
iTempsCalcul=(int)(time(NULL)-iStartLogTime);
fprintf(fTimeLog, "%d\t%f\t#%s\n", iTempsCalcul, lfFitness, sFunctionName);
fflush(fTimeLog);
return iTempsCalcul;
}