/**
@brief myMessageOutput
for debugging
@param type : the messages that can be sent to a message handler
@param context : were? wat?
@param msg : Message
*/
void myMessageOutput(QtMsgType type,
const QMessageLogContext &context,
const QString &msg)
{
QString txt=QTime::currentTime().toString("hh:mm:ss.zzz");
QByteArray dbs =txt.toLocal8Bit();
QByteArray localMsg = msg.toLocal8Bit();
const char *file = context.file ? context.file : "";
const char *function = context.function ? context.function : "";
switch (type) {
case QtDebugMsg:
fprintf(stderr,
"%s Debug: %s (%s:%u, %s)\n",
dbs.constData(),
localMsg.constData(),
file,
context.line,
function);
txt+=" Debug: ";
break;
case QtInfoMsg:
fprintf(stderr,
"%s Info: %s \n",
dbs.constData(),
localMsg.constData());
txt+=" Info: ";
break;
case QtWarningMsg:
fprintf(stderr,
"%s Warning: %s (%s:%u, %s)\n",
dbs.constData(),
localMsg.constData(),
file, context.line,
function);
txt+=" Warning: ";
break;
case QtCriticalMsg:
fprintf(stderr,
"%s Critical: %s (%s:%u, %s)\n",
dbs.constData(),
localMsg.constData(),
file,
context.line,
function);
txt+=" Critical: ";
break;
case QtFatalMsg:
fprintf(stderr,
"%s Fatal: %s (%s:%u, %s)\n",
dbs.constData(),
localMsg.constData(),
file,
context.line,
function);
txt+=" Fatal: ";
break;
default:
fprintf(stderr,
"%s Unknown: %s (%s:%u, %s)\n",
dbs.constData(),
localMsg.constData(),
file,
context.line,
function);
txt+=" Unknown: ";
}
txt+= msg;
if(type==QtInfoMsg){
txt+=" \n";
} else {
txt+= " (";
txt+= context.file ? context.file : "";
txt+= ":";
txt+=QString::number(context.line ? context.line :0);
txt+= ", ";
txt+= context.function ? context.function : "";
txt+=")\n";
}
QFile outFile(QETApp::configDir()
+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 = QETApp::configDir() + "/";
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!";
}
}
}
/**
@brief main
Main function of QElectroTech
@param argc : number of parameters
\~French number of paramètres
\~ @param argv : parameters
\~French paramètres
\~ @return exit code
*/
int main(int argc, char **argv)
{
//Some setup, notably to use with QSetting.
QCoreApplication::setOrganizationName("QElectroTech");
QCoreApplication::setOrganizationDomain("qelectrotech.org");
QCoreApplication::setApplicationName("QElectroTech");
//Creation and execution of the application
//HighDPI
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
SingleApplication app(argc, argv, true);
#ifdef Q_OS_MACOS
//Handle the opening of QET when user double click on a .qet .elmt .tbt file
//or drop these same files to the QET icon of the dock
MacOSXOpenEvent open_event;
app.installEventFilter(&open_event);
app.setStyle(QStyleFactory::create("Fusion"));
#endif
if (app.isSecondary())
{
QStringList arg_list = app.arguments();
//Remove the first argument, it's the binary file
arg_list.takeFirst();
QETArguments qetarg(arg_list);
QString message = "launched-with-args: " + QET::joinWithSpaces(
QStringList(qetarg.arguments()));
app.sendMessage(message.toUtf8());
return 0;
}
QETApp qetapp;
QETApp::instance()->installEventFilter(&qetapp);
QObject::connect(&app, &SingleApplication::receivedMessage,
&qetapp, &QETApp::receiveMessage);
// for debugging
qInstallMessageHandler(myMessageOutput);
qInfo("Start-up");
// delete old log files of max 7 days old.
delete_old_log_files(7);
{
Machine_info *my_ma =new Machine_info();
my_ma->send_info_to_debug();
delete my_ma;
}
#if 0 // So Annoying! (Perhaps make it once only, or once a day/week?
if(! QET::FileFormatStable){
QMessageBox::StandardButton btn = QMessageBox::critical(
nullptr,
QCoreApplication::translate(
"main",
"Unstable file format!"),
QCoreApplication::translate(
"main",
"ATTENTION: This application version is UNSTABLE!
"
"Everything you do with this application can break your "
"workspace, libraries or projects! Saved files will not be "
"readable with stable releases of QElectroTech. It's highly "
"recommended to create a backup before proceeding. If you are "
"unsure, please download an official stable release instead.
"
// "For details, please take a look at QElectroTech's "
// "versioning concept.
"
"Are you really sure to continue with the risk of breaking your "
"files?!
","this text is not finished yet, expect changes!")
.arg("doc_release_workflow"),
QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
if (!(btn == QMessageBox::Yes))
return 1;
}
#endif
return app.exec();
}