153 lines
5.3 KiB
C++
153 lines
5.3 KiB
C++
/*******************************************************************************
|
|
* *
|
|
* Société de Transports de Montréal. *
|
|
* 2012 *
|
|
* *
|
|
* Projet Zones Tests *
|
|
* *
|
|
* *
|
|
* *
|
|
*******************************************************************************/
|
|
/*
|
|
Description:
|
|
Cette classe permet de créer un fichier log très détaillé.
|
|
CEngLog ne devrait être utilisée pour des fins de débogage par l'ingénierie
|
|
Le log peut être activé en ajoutant la ligne ENGLOG=V dans le fichier de
|
|
configuration de la Zone Tests (ZT.cfg) où V (verbosité) est une valeur entre
|
|
1 et 3 qui représente le niveau de détail désiré.
|
|
*/
|
|
|
|
/* ************************************************************************** */
|
|
/* Revision:
|
|
### 20121219 JFM
|
|
Verision d'origine.
|
|
|
|
### YYYYMMDD Description du besoin ou du bug
|
|
Description du changement.
|
|
*/
|
|
|
|
/* ************************************************************************** */
|
|
|
|
#include "EngLog.h"
|
|
#include <QDateTime>
|
|
#include <QString>
|
|
#include "RamMonitor.h"
|
|
#include <QFileInfo>
|
|
|
|
//#include <QTextCodec>
|
|
|
|
//singleton instanciation
|
|
CEngLog CEngLog::mSingleton;
|
|
|
|
CEngLog::CEngLog():
|
|
mEngLogFile(NULL),
|
|
mVerbosity(3)
|
|
{
|
|
//QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
|
|
}
|
|
|
|
CEngLog::~CEngLog()
|
|
{
|
|
if(mEngLogFile)
|
|
delete mEngLogFile;
|
|
}
|
|
|
|
void CEngLog::Init(unsigned int Verbosity)
|
|
{
|
|
mVerbosity = Verbosity;
|
|
QString FileName;
|
|
#ifndef USE_SINGLE_ENGINEERING_FILE
|
|
FileName.sprintf("./ING/IngLog_%s_%s",QDateTime::currentDateTime().date().toString("yyyyMMdd").toAscii().data(),QDateTime::currentDateTime().time().toString("hh-mm-ss").toAscii().data());
|
|
FileName.append(".txt");
|
|
#else
|
|
FileName.sprintf("./ING/IngLog.txt");
|
|
#endif
|
|
mEngLogFile = new QFile(FileName);
|
|
if(mEngLogFile)
|
|
{
|
|
#ifndef USE_SINGLE_ENGINEERING_FILE
|
|
mEngLogFile->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Unbuffered);
|
|
#else
|
|
QFileInfo EngLogFileInfo(*mEngLogFile);
|
|
if(EngLogFileInfo.size() > MAX_ENGINEERING_LOG_FILESIZE) //Make shure the file doesn't get too big
|
|
{
|
|
mEngLogFile->open(QIODevice::WriteOnly |QIODevice::Truncate | QIODevice::Text | QIODevice::Unbuffered);
|
|
}
|
|
else
|
|
{
|
|
mEngLogFile->open(QIODevice::WriteOnly |QIODevice::Append | QIODevice::Text | QIODevice::Unbuffered);
|
|
}
|
|
#endif
|
|
|
|
QString temp;
|
|
mEngLogFile->write("\n\n");
|
|
AddLogString(temp.sprintf("********************************************************************"),0);
|
|
AddLogString(temp.sprintf("Zone Tests. Fichier Log d'ingénierie"),0);
|
|
AddLogString(temp.sprintf("Créé le %s à %s",QDateTime::currentDateTime().date().toString("yyyy-MM-dd").toAscii().data(),QDateTime::currentDateTime().time().toString("hh:mm:ss").toAscii().data()),0);
|
|
AddLogString(temp.sprintf("Verbosité niveau %d",mVerbosity),0);
|
|
AddLogString(temp.sprintf("********************************************************************"),0);
|
|
}
|
|
}
|
|
|
|
void CEngLog::AddLogString(QString string, unsigned int Verbosity)
|
|
{
|
|
if(string.isEmpty())
|
|
return;
|
|
|
|
qDebug("%s",string.toUtf8().data());
|
|
|
|
if(mEngLogFile == NULL)
|
|
return;
|
|
|
|
if(mEngLogFile->isOpen() == false)
|
|
return;
|
|
|
|
if(Verbosity <= mVerbosity)
|
|
{
|
|
if(string.at(string.length()-1) != '\n')
|
|
string.append('\n');
|
|
|
|
if(string.length() > 1)
|
|
string.prepend(QDateTime::currentDateTime().toString("yyyy/MM/dd - hh:mm:ss.zzz : ").toAscii().data());
|
|
|
|
mEngLogFile->write(string.toUtf8());
|
|
|
|
#ifdef LOG_RAM_USAGE
|
|
long ram = CRamMonitor::instance()->GetRamUsage(false);
|
|
mEngLogFile->write(QString().sprintf("Ram: %ld \n",ram).toUtf8());
|
|
#endif
|
|
|
|
}
|
|
}
|
|
|
|
//When a buffer is already formatted, just write it to the file.
|
|
void CEngLog::WriteFormattedString(QString string)
|
|
{
|
|
mEngLogFile->write(string.toUtf8());
|
|
qDebug("%s",string.toUtf8().data());
|
|
}
|
|
|
|
unsigned int CEngLog::DeleteEngLogFile()
|
|
{
|
|
mEngLogFile->remove();
|
|
delete mEngLogFile;
|
|
|
|
mEngLogFile = new QFile("./ING/IngLog.txt");
|
|
if(mEngLogFile)
|
|
{
|
|
mEngLogFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text | QIODevice::Unbuffered);
|
|
|
|
QString temp;
|
|
mEngLogFile->write("\n\n");
|
|
AddLogString(temp.sprintf("********************************************************************"));
|
|
AddLogString(temp.sprintf("Zone Tests. Fichier Log d'ingénierie (créé suite à une destruction)"),0);
|
|
AddLogString(temp.sprintf("Créé le %s à %s",QDateTime::currentDateTime().date().toString("yyyy-MM-dd").toAscii().data(),QDateTime::currentDateTime().time().toString("hh:mm:ss").toAscii().data()),0);
|
|
AddLogString(temp.sprintf("Verbosité niveau %d",mVerbosity),0);
|
|
AddLogString(temp.sprintf("********************************************************************"));
|
|
}
|
|
mEngLogFile->flush();
|
|
|
|
|
|
return RET_OK;
|
|
}
|