QtHPConnect/errorhandler.cpp

114 lines
2.6 KiB
C++
Raw Normal View History

2019-02-10 14:32:15 +01:00
#include "errorhandler.h"
errorHandler::errorHandler()
{
2019-02-10 14:43:00 +01:00
}
errorHandler::errorHandler(QObject * parent)
{
pParent = parent;
}
QString errorHandler::getLogFileName()
{
return QString("hpinterface.log");
}
int errorHandler::writeLog(QString lineToBeLogged)
{
QMutexLocker locker(&m_lineLoggerMutex);
QFile fileLog(getLogFileName());
//doRollLogsIfNeeded(static_cast<qint64>(fileLog.size() + lineToBeLogged.length()));
// Do not open in append mode but seek() to avoid warning for unseekable
// devices, note that if open is made with WriteOnly without Append, the
// file gets truncated
if (!fileLog.open(QIODevice::ReadWrite | QIODevice::Text))
{
QTextStream out(stdout);
out << "CANNOT OPEN LOG FILE: " << getLogFileName();
return -1;
}
// seek() does nothing on sequential devices, this is in essence what QFile
// does when Append flag is set in open()
if (!fileLog.isSequential())
{
fileLog.seek(fileLog.size());
}
QDateTime time(QDateTime::currentDateTime());
QTextStream out(&fileLog);
out << time.toString("yyyy/MM/dd hh:mm:ss") << " "<< lineToBeLogged << endl;
return 0;
}
int errorHandler::writeStatus(QString msg)
{
if (MainWindow *pb = qobject_cast<MainWindow*> (pParent))
{
pb->writeStatus( msg);
}
return 0;
}
int errorHandler::writeChatter(QString msg)
{
if (MainWindow *pb = qobject_cast<MainWindow*> (pParent))
{
pb->writeChatter( msg);
}
return 0;
}
int errorHandler::error(ErrLevel el, int num, QString msg, QString Data=QString())
{
switch (el) {
case L0:
writeLog("Abort: "+msg);
writeStatus("Abort: "+msg);
2019-02-10 17:26:56 +01:00
exit(num);
2019-02-10 14:43:00 +01:00
break;
default:
writeLog(" "+msg+Data);
writeStatus(" "+msg);
if (Data=="")
writeChatter(" "+msg);
else
writeChatter(" "+msg+Data);
break;
}
return 0;
}
int errorHandler::dump(uint8_t * data, int size)
{
int i,j;
QString text = QString("Dump:\n");
QString texta = QString("ASCII:\n");
j=0;
for (i=0; i< size; i++)
{
text = text + QString(" %1 ").arg(QChar(data[i]).unicode(),2,16,QChar('0'));
j++;
if (j>16) {
j=0;
text=text+"\n";
}
texta = texta +" "+QChar(data[i]).unicode();
}
text = text + "\n";
texta = texta + "\n";
writeLog(text);
writeChatter(text);
writeLog(texta);
writeChatter(texta);
2019-02-10 14:32:15 +01:00
2019-02-10 17:26:56 +01:00
return 0;
2019-02-10 14:32:15 +01:00
}