Mod debuger to 7 days logs in .qet dir

the user can now read the log files
and/or send them to us for further investigation
This commit is contained in:
Simon De Backer
2020-08-14 18:31:38 +02:00
parent d556206998
commit 274108d509

View File

@@ -34,11 +34,12 @@ void myMessageOutput(QtMsgType type,
const QString &msg) const QString &msg)
{ {
QByteArray dbs = QString txt=QTime::currentTime().toString("hh:mm:ss.zzz");
QTime::currentTime().toString("hh:mm:ss.zzz").toLocal8Bit(); QByteArray dbs =txt.toLocal8Bit();
QByteArray localMsg = msg.toLocal8Bit(); QByteArray localMsg = msg.toLocal8Bit();
const char *file = context.file ? context.file : ""; const char *file = context.file ? context.file : "";
const char *function = context.function ? context.function : ""; const char *function = context.function ? context.function : "";
switch (type) { switch (type) {
case QtDebugMsg: case QtDebugMsg:
fprintf(stderr, fprintf(stderr,
@@ -48,12 +49,14 @@ void myMessageOutput(QtMsgType type,
file, file,
context.line, context.line,
function); function);
txt+=" Debug: ";
break; break;
case QtInfoMsg: case QtInfoMsg:
fprintf(stderr, fprintf(stderr,
"%s Info: %s \n", "%s Info: %s \n",
dbs.constData(), dbs.constData(),
localMsg.constData()); localMsg.constData());
txt+=" Info: ";
break; break;
case QtWarningMsg: case QtWarningMsg:
fprintf(stderr, fprintf(stderr,
@@ -62,6 +65,7 @@ void myMessageOutput(QtMsgType type,
localMsg.constData(), localMsg.constData(),
file, context.line, file, context.line,
function); function);
txt+=" Warning: ";
break; break;
case QtCriticalMsg: case QtCriticalMsg:
fprintf(stderr, fprintf(stderr,
@@ -71,6 +75,7 @@ void myMessageOutput(QtMsgType type,
file, file,
context.line, context.line,
function); function);
txt+=" Critical: ";
break; break;
case QtFatalMsg: case QtFatalMsg:
fprintf(stderr, fprintf(stderr,
@@ -80,6 +85,7 @@ void myMessageOutput(QtMsgType type,
file, file,
context.line, context.line,
function); function);
txt+=" Fatal: ";
break; break;
default: default:
fprintf(stderr, fprintf(stderr,
@@ -89,6 +95,58 @@ void myMessageOutput(QtMsgType type,
file, file,
context.line, context.line,
function); function);
txt+=" Unknown: ";
}
txt+= msg;
if(type==QtInfoMsg){
txt+=" \n";
} else {
txt+= " (";
txt+= context.file ? context.file : "";
txt+= ":";
txt+= context.line ? context.line :0;
txt+= ", ";
txt+= context.function ? context.function : "";
txt+=")\n";
}
QFile outFile(QDir::homePath()
+"/.qet/"
+QDate::currentDate().toString("yyyyMMdd")
+".log");
if(outFile.open(QIODevice::WriteOnly | QIODevice::Append))
{
QTextStream ts(&outFile);
ts << txt;
}
outFile.close();
}
/**
@brief delete_old_log_files
delete old log files
@param days : max days old
*/
void delete_old_log_files(int days)
{
const QDate today = QDate::currentDate();
const QString path = QDir::homePath() + "/.qet/";
QString filter("%1%1%1%1%1%1%1%1.log"); // pattern
filter = filter.arg("[0123456789]"); // valid characters
Q_FOREACH (auto fileInfo,
QDir(path).entryInfoList(
QStringList(filter),
QDir::Files))
{
if (fileInfo.lastRead().date().daysTo(today) > days)
{
QString filepath = fileInfo.absoluteFilePath();
QDir deletefile;
deletefile.setPath(filepath);
deletefile.remove(filepath);
qDebug() << "File " + filepath + " is deleted!";
}
} }
} }
@@ -103,7 +161,11 @@ void myMessageOutput(QtMsgType type,
*/ */
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
// for debugging
qInstallMessageHandler(myMessageOutput); qInstallMessageHandler(myMessageOutput);
qInfo("Start-up");
// delete old log files of max 7 days old.
delete_old_log_files(7);
//Some setup, notably to use with QSetting. //Some setup, notably to use with QSetting.
QCoreApplication::setOrganizationName("QElectroTech"); QCoreApplication::setOrganizationName("QElectroTech");
QCoreApplication::setOrganizationDomain("qelectrotech.org"); QCoreApplication::setOrganizationDomain("qelectrotech.org");
@@ -112,7 +174,7 @@ int main(int argc, char **argv)
//HighDPI //HighDPI
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
SingleApplication app(argc, argv, true); SingleApplication app(argc, argv, true);
// for debugging
{ {
Machine_info *my_ma =new Machine_info(); Machine_info *my_ma =new Machine_info();
my_ma->send_info_to_debug(); my_ma->send_info_to_debug();