Skip to content

Commit

Permalink
Multifilter export feature for dlt-commander (-multifilter) (#591)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Wenzel <[email protected]>
  • Loading branch information
alexmucde authored Nov 22, 2024
1 parent 02da750 commit 038e9f6
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 34 deletions.
20 changes: 16 additions & 4 deletions commander/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ int main(int argc, char *argv[])
{
qDebug() << "### Convert to DLT";
QDltExporter exporter(&dltFile,opt.getConvertDestFile(),0,QDltExporter::FormatDlt,QDltExporter::SelectionAll,0,1,0,0,opt.getDelimiter());
exporter.setFilterList(filterList);
if(opt.isMultifilter())
exporter.setMultifilterFilenames(opt.getFilterFiles());
else
exporter.setFilterList(filterList);
qDebug() << "Commandline DLT convert to " << opt.getConvertDestFile();
exporter.exportMessages();
qDebug() << "DLT export to DLT file format done";
Expand All @@ -136,7 +139,10 @@ int main(int argc, char *argv[])
{
qDebug() << "### Convert to ASCII";
QDltExporter exporter(&dltFile,opt.getConvertDestFile(),0,QDltExporter::FormatAscii,QDltExporter::SelectionAll,0,1,0,0,opt.getDelimiter());
exporter.setFilterList(filterList);
if(opt.isMultifilter())
exporter.setMultifilterFilenames(opt.getFilterFiles());
else
exporter.setFilterList(filterList);
qDebug() << "Commandline ASCII convert to " << opt.getConvertDestFile();
exporter.exportMessages();
qDebug() << "DLT export ASCII done";
Expand All @@ -145,7 +151,10 @@ int main(int argc, char *argv[])
{
qDebug() << "### Convert to CSV";
QDltExporter exporter(&dltFile,opt.getConvertDestFile(),0,QDltExporter::FormatCsv,QDltExporter::SelectionAll,0,1,0,0,opt.getDelimiter());
exporter.setFilterList(filterList);
if(opt.isMultifilter())
exporter.setMultifilterFilenames(opt.getFilterFiles());
else
exporter.setFilterList(filterList);
qDebug() << "Commandline CSV convert to " << opt.getConvertDestFile();
exporter.exportMessages();
qDebug() << "DLT export CSV done";
Expand All @@ -154,7 +163,10 @@ int main(int argc, char *argv[])
{
qDebug() << "### Convert to UTF8";
QDltExporter exporter(&dltFile,opt.getConvertDestFile(),0,QDltExporter::FormatUTF8,QDltExporter::SelectionAll,0,1,0,0,opt.getDelimiter());
exporter.setFilterList(filterList);
if(opt.isMultifilter())
exporter.setMultifilterFilenames(opt.getFilterFiles());
else
exporter.setFilterList(filterList);
qDebug() << "Commandline UTF8 convert to " << opt.getConvertDestFile();
exporter.exportMessages();
qDebug() << "DLT export UTF8 done";
Expand Down
10 changes: 10 additions & 0 deletions commander/optmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ OptManager::OptManager()
filter = false;
convertionmode = e_ASCI;
delimiter = ',';
multifilter = false;
}

/*OptManager* OptManager::getInstance()
Expand Down Expand Up @@ -92,6 +93,8 @@ void OptManager::printUsage()
qDebug()<<" -csv\tConversion will be done in CSV format";
qDebug()<<" -d\tConversion will NOT be done, save in dlt file format again instead";
qDebug()<<" -delimiter <character>\tThe used delimiter for CSV export (Default: ,).";
qDebug()<<" -multifilter\tMultifilter will generate a separate export file with the name of the filter.";
qDebug()<<" \t-c will define the folder name, not the filename.";
qDebug()<<"\nExamples:\n";
qDebug().noquote() << executable << "-c .\\trace.txt c:\\trace\\trace.dlt";
qDebug().noquote() << executable << "-c -u .\\trace.txt c:\\trace\\trace.dlt";
Expand Down Expand Up @@ -162,6 +165,12 @@ void OptManager::parse(QStringList *opt)

convertionmode = e_CSV;
}
else if(str.compare("-multifilter")==0)
{
qDebug() << "Multifilter export selected.";

multifilter = true;
}
else if(str.compare("-d")==0)
{
qDebug() << "Convert to DLT";
Expand Down Expand Up @@ -198,6 +207,7 @@ void OptManager::parse(QStringList *opt)
bool OptManager::isLogFile(){return log;}
bool OptManager::isFilterFile(){return filter;}
bool OptManager::isConvert(){return convert;}
bool OptManager::isMultifilter(){return multifilter;}
e_convertionmode OptManager::get_convertionmode(){return convertionmode;}
QStringList OptManager::getLogFiles(){return logFiles;}
QStringList OptManager::getFilterFiles(){return filterFiles;}
Expand Down
2 changes: 2 additions & 0 deletions commander/optmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class OptManager
bool isFilterFile();
bool isConvert();
bool isConvertUTF8();
bool isMultifilter();

e_convertionmode get_convertionmode();

Expand All @@ -64,6 +65,7 @@ class OptManager
bool log;
bool filter;
bool convert;
bool multifilter;
e_convertionmode convertionmode;

QStringList logFiles;
Expand Down
152 changes: 124 additions & 28 deletions qdlt/qdltexporter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <algorithm>
#include <QDebug>
#include <QFile>
#include <QFileInfo>

#include "qdltexporter.h"
#include "fieldnames.h"
Expand Down Expand Up @@ -58,10 +59,17 @@ bool QDltExporter::writeCSVHeader()
.arg(FieldNames::getName(FieldNames::Mode))
.arg(FieldNames::getName(FieldNames::ArgCount))
.arg(FieldNames::getName(FieldNames::Payload));
return to.write(header.toLatin1().constData()) < 0 ? false : true;
if(multifilterFilenames.isEmpty())
to.write(header.toLatin1().constData());
else
{
for(auto file: multifilterFilesList)
file->write(header.toLatin1().constData());
}
return true;
}

void QDltExporter::writeCSVLine(int index, QDltMsg msg)
void QDltExporter::writeCSVLine(int index, QDltMsg msg,QFile &to)
{
QString text("");

Expand Down Expand Up @@ -108,30 +116,83 @@ bool QDltExporter::startExport()
exportFormat == QDltExporter::FormatUTF8 ||
exportFormat == QDltExporter::FormatCsv)
{
if(!to.open(QIODevice::WriteOnly | QIODevice::Text))
if(multifilterFilenames.isEmpty())
{
if (QDltOptManager::getInstance()->issilentMode())
if(!to.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << QString("ERROR - cannot open the export file %1").arg(to.fileName());
if (QDltOptManager::getInstance()->issilentMode())
qDebug() << QString("ERROR - cannot open the export file %1").arg(to.fileName());
return false;
}
//else
//QMessageBox::critical(qobject_cast<QWidget *>(parent()), QString("DLT Viewer"),
// QString("Cannot open the export file %1").arg(to->fileName()));
return false;
}
else
{
for(auto filename : multifilterFilenames)
{
QFileInfo info(filename);
info.baseName();
QFile *file;
if(exportFormat == QDltExporter::FormatAscii)
file = new QFile(to.fileName()+"\\"+info.baseName()+".txt");
else if(exportFormat == QDltExporter::FormatUTF8)
file = new QFile(to.fileName()+"\\"+info.baseName()+".txt");
else if(exportFormat == QDltExporter::FormatCsv)
file = new QFile(to.fileName()+"\\"+info.baseName()+".csv");
QDltFilterList *filterList = new QDltFilterList();
if(!filterList->LoadFilter(filename,true))
qDebug() << "Export: Open filter file " << filename << " failed!";
if(!filterList->isEmpty() && file->open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "Multifilter export filename: " << file->fileName();
multifilterFilesList.append(file);
multifilterFilterList.append(filterList);
}
else
{
qDebug() << "Export: Export multifilter file " << file->fileName() << " failed!";
delete file;
delete filterList;
}
}

}
}
else if((exportFormat == QDltExporter::FormatDlt)||(exportFormat == QDltExporter::FormatDltDecoded))
{
if(!to.open(QIODevice::WriteOnly))
if(multifilterFilenames.isEmpty())
{
if (QDltOptManager::getInstance()->issilentMode() )
if(!to.open(QIODevice::WriteOnly))
{
qDebug() << QString("ERROR - cannot open the export file %1").arg(to.fileName());
if (QDltOptManager::getInstance()->issilentMode())
qDebug() << QString("ERROR - cannot open the export file %1").arg(to.fileName());
return false;
}
//else
//QMessageBox::critical(qobject_cast<QWidget *>(parent()), QString("DLT Viewer"),
// QString("Cannot open the export file %1").arg(to->fileName()));
return false;
}
else
{
for(auto filename : multifilterFilenames)
{
QFileInfo info(filename);
info.baseName();
QFile *file;
file = new QFile(to.fileName()+"\\"+info.baseName()+".dlt");
QDltFilterList *filterList = new QDltFilterList();
if(!filterList->LoadFilter(filename,true))
qDebug() << "Export: Open filter file " << filename << " failed!";
qDebug() << "Multifilter export filename: " << file->fileName();
if(!filterList->isEmpty() && file->open(QIODevice::WriteOnly))
{
multifilterFilesList.append(file);
multifilterFilterList.append(filterList);
}
else
{
delete file;
delete filterList;
qDebug() << "Export: Export multifilter file " << filename << " failed!";
}
}

}
}

Expand Down Expand Up @@ -185,8 +246,22 @@ bool QDltExporter::finish()
exportFormat == QDltExporter::FormatDlt ||
exportFormat == QDltExporter::FormatDltDecoded)
{
/* close output file */
to.close();
if(multifilterFilenames.isEmpty())
to.close();
else
{
for(auto file : multifilterFilesList)
{
file->close();
delete file;
}
for(auto filterList : multifilterFilterList)
{
delete filterList;
}
multifilterFilterList.clear();
multifilterFilesList.clear();
}
}
else if (exportFormat == QDltExporter::FormatClipboard ||
exportFormat == QDltExporter::FormatClipboardPayloadOnly ||
Expand Down Expand Up @@ -252,7 +327,7 @@ bool QDltExporter::getMsg(unsigned long int num,QDltMsg &msg,QByteArray &buf)
return result;
}

bool QDltExporter::exportMsg(unsigned long int num, QDltMsg &msg, QByteArray &buf)
bool QDltExporter::exportMsg(unsigned long int num, QDltMsg &msg, QByteArray &buf,QFile &to)
{
if((exportFormat == QDltExporter::FormatDlt)||(exportFormat == QDltExporter::FormatDltDecoded))
{
Expand Down Expand Up @@ -315,11 +390,11 @@ bool QDltExporter::exportMsg(unsigned long int num, QDltMsg &msg, QByteArray &bu
else if(exportFormat == QDltExporter::FormatCsv)
{
if(exportSelection == QDltExporter::SelectionAll)
writeCSVLine(num, msg);
writeCSVLine(num, msg,to);
else if(exportSelection == QDltExporter::SelectionFiltered)
writeCSVLine(from->getMsgFilterPos(num), msg);
writeCSVLine(from->getMsgFilterPos(num), msg,to);
else if(exportSelection == QDltExporter::SelectionSelected)
writeCSVLine(from->getMsgFilterPos(selectedRows[num]), msg);
writeCSVLine(from->getMsgFilterPos(selectedRows[num]), msg,to);
else
return false;
}
Expand Down Expand Up @@ -366,6 +441,11 @@ void QDltExporter::setFilterList(QDltFilterList &filterList)
this->filterList = filterList;
}

void QDltExporter::setMultifilterFilenames(QStringList multifilterFilenames)
{
this->multifilterFilenames=multifilterFilenames;
}

void QDltExporter::exportMessages()
{
QDltMsg msg;
Expand Down Expand Up @@ -457,15 +537,31 @@ void QDltExporter::exportMessages()
if(filterList.isEmpty() || filterList.checkFilter(msg))
{
// export message
if(!exportMsg(starting,msg,buf))
if(multifilterFilenames.isEmpty())
{
// finish();
//qDebug() << "DLT Export exportMsg() failed";
exportErrors++;
continue;
if(!exportMsg(starting,msg,buf,to))
{
// finish();
//qDebug() << "DLT Export exportMsg() failed";
exportErrors++;
continue;
}
else
exportCounter++;
}
else
exportCounter++;
{
for(int num=0;num<multifilterFilterList.size();num++)
{
if(multifilterFilterList[num]->checkFilter(msg))
{
if(!exportMsg(starting,msg,buf,*multifilterFilesList[num]))
exportErrors++;
else
exportCounter++;
}
}
}
}

} // for loop
Expand Down
12 changes: 10 additions & 2 deletions qdlt/qdltexporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ class QDLT_EXPORT QDltExporter : public QThread
* \param to File to write to
* \param msg msg to get the data from
*/
void writeCSVLine(int index, QDltMsg msg);
void writeCSVLine(int index, QDltMsg msg,QFile &to);

bool startExport();
bool finish();
bool getMsg(unsigned long int num, QDltMsg &msg, QByteArray &buf);
bool exportMsg(unsigned long int num, QDltMsg &msg,QByteArray &buf);
bool exportMsg(unsigned long int num, QDltMsg &msg,QByteArray &buf,QFile &to);

public:

Expand Down Expand Up @@ -77,6 +77,11 @@ class QDLT_EXPORT QDltExporter : public QThread
*/
void setFilterList(QDltFilterList &filterList);

/* Multifilter will be used if set to export to separate file for each Filter
* \param filterListmultifilterNames All filenames of the filters
*/
void setMultifilterFilenames(QStringList multifilterFilenames);

signals:

void clipboard(QString text);
Expand All @@ -102,6 +107,9 @@ public slots:
int dst; // project and local setting
char delimiter;
QDltFilterList filterList;
QStringList multifilterFilenames;
QList<QFile*> multifilterFilesList;
QList<QDltFilterList*> multifilterFilterList;
};

#endif // QDLTEXPORTER_H

0 comments on commit 038e9f6

Please sign in to comment.