00001
00002
00003
00004
00005
00006
00007 #include "ColorFilterMode.h"
00008 #include <iostream>
00009 #include "Logger.h"
00010 #include "MainWindow.h"
00011 #include <QApplication>
00012 #include <QCoreApplication>
00013 #include <QDebug>
00014 #include <QDir>
00015 #include <QFileInfo>
00016 #include <QObject>
00017 #include <QProcessEnvironment>
00018 #include "TranslatorContainer.h"
00019
00020 using namespace std;
00021
00022 const QString CMD_DEBUG ("debug");
00023 const QString CMD_ERROR_REPORT ("errorreport");
00024 const QString CMD_FILE_CMD_SCRIPT ("filecmdscript");
00025 const QString CMD_GNUPLOT ("gnuplot");
00026 const QString CMD_HELP ("help");
00027 const QString CMD_REGRESSION ("regression");
00028 const QString DASH ("-");
00029 const QString DASH_DEBUG ("-" + CMD_DEBUG);
00030 const QString DASH_ERROR_REPORT ("-" + CMD_ERROR_REPORT);
00031 const QString DASH_FILE_CMD_SCRIPT ("-" + CMD_FILE_CMD_SCRIPT);
00032 const QString DASH_GNUPLOT ("-" + CMD_GNUPLOT);
00033 const QString DASH_HELP ("-" + CMD_HELP);
00034 const QString DASH_REGRESSION ("-" + CMD_REGRESSION);
00035 const QString ENGAUGE_LOG_FILE ("engauge.log");
00036
00037
00038 bool checkFileExists (const QString &file);
00039 QString engaugeLogFilename ();
00040 bool engaugeLogFilenameAttempt (const QString &path,
00041 QString &pathAndFile);
00042 void parseCmdLine (int argc,
00043 char **argv,
00044 bool &isDebug,
00045 QString &errorReportFile,
00046 QString &fileCmdScriptFile,
00047 bool &isRegressionTest,
00048 bool &isGnuplot,
00049 QStringList &loadStartupFiles);
00050
00051
00052 bool checkFileExists (const QString &file)
00053 {
00054 QFileInfo check (file);
00055 return check.exists() && check.isFile();
00056 }
00057
00058 QString engaugeLogFilename()
00059 {
00060 QString pathAndFile;
00061
00062 #ifndef OSX
00063 QProcessEnvironment env;
00064
00065
00066 if (!engaugeLogFilenameAttempt (QCoreApplication::applicationDirPath(), pathAndFile)) {
00067 if (!engaugeLogFilenameAttempt (env.value ("HOME"), pathAndFile)) {
00068 if (!engaugeLogFilenameAttempt (env.value ("TEMP"), pathAndFile)) {
00069 pathAndFile = ENGAUGE_LOG_FILE;
00070 }
00071 }
00072 }
00073 #endif
00074
00075 return pathAndFile;
00076 }
00077
00078 bool engaugeLogFilenameAttempt (const QString &path,
00079 QString &pathAndFile)
00080 {
00081 bool success = false;
00082
00083
00084 pathAndFile = QString ("%1%2%3")
00085 .arg (path)
00086 .arg (QDir::separator())
00087 .arg (ENGAUGE_LOG_FILE);
00088 QFile file (pathAndFile);
00089 if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
00090
00091 file.close();
00092 success = true;
00093 }
00094
00095 return success;
00096 }
00097
00098 int main(int argc, char *argv[])
00099 {
00100 qRegisterMetaType<ColorFilterMode> ("ColorFilterMode");
00101
00102 QApplication app(argc, argv);
00103
00104
00105 TranslatorContainer translatorContainer (app);
00106
00107
00108 bool isDebug, isGnuplot, isRegressionTest;
00109 QString errorReportFile, fileCmdScriptFile;
00110 QStringList loadStartupFiles;
00111 parseCmdLine (argc,
00112 argv,
00113 isDebug,
00114 errorReportFile,
00115 fileCmdScriptFile,
00116 isRegressionTest,
00117 isGnuplot,
00118 loadStartupFiles);
00119
00120
00121 initializeLogging ("engauge",
00122 engaugeLogFilename(),
00123 isDebug);
00124 LOG4CPP_INFO_S ((*mainCat)) << "main args=" << QApplication::arguments().join (" ").toLatin1().data();
00125
00126
00127 MainWindow w (errorReportFile,
00128 fileCmdScriptFile,
00129 isRegressionTest,
00130 isGnuplot,
00131 loadStartupFiles);
00132 w.show();
00133
00134
00135 return app.exec();
00136 }
00137
00138 void parseCmdLine (int argc,
00139 char **argv,
00140 bool &isDebug,
00141 QString &errorReportFile,
00142 QString &fileCmdScriptFile,
00143 bool &isRegressionTest,
00144 bool &isGnuplot,
00145 QStringList &loadStartupFiles)
00146 {
00147 const int COLUMN_WIDTH = 20;
00148 bool showUsage = false;
00149
00150
00151 bool nextIsErrorReportFile = false;
00152 bool nextIsFileCmdScript = false;
00153
00154
00155 isDebug = false;
00156 errorReportFile = "";
00157 fileCmdScriptFile = "";
00158 isRegressionTest = false;
00159 isGnuplot = false;
00160
00161 for (int i = 1; i < argc; i++) {
00162
00163 if (nextIsErrorReportFile) {
00164 errorReportFile = argv [i];
00165 showUsage |= !checkFileExists (errorReportFile);
00166 nextIsErrorReportFile = false;
00167 } else if (nextIsFileCmdScript) {
00168 fileCmdScriptFile = argv [i];
00169 showUsage |= !checkFileExists (fileCmdScriptFile);
00170 nextIsFileCmdScript = false;
00171 } else if (strcmp (argv [i], DASH_DEBUG.toLatin1().data()) == 0) {
00172 isDebug = true;
00173 } else if (strcmp (argv [i], DASH_ERROR_REPORT.toLatin1().data()) == 0) {
00174 nextIsErrorReportFile = true;
00175 } else if (strcmp (argv [i], DASH_FILE_CMD_SCRIPT.toLatin1().data()) == 0) {
00176 nextIsFileCmdScript = true;
00177 } else if (strcmp (argv [i], DASH_GNUPLOT.toLatin1().data()) == 0) {
00178 isGnuplot = true;
00179 } else if (strcmp (argv [i], DASH_HELP.toLatin1().data()) == 0) {
00180 showUsage = true;
00181 } else if (strcmp (argv [i], DASH_REGRESSION.toLatin1().data()) == 0) {
00182 isRegressionTest = true;
00183 } else if (strncmp (argv [i], DASH.toLatin1().data(), 1) == 0) {
00184 showUsage = true;
00185 } else {
00186
00187
00188 QString fileName = argv [i];
00189 QFileInfo fInfo (fileName);
00190 if (fInfo.isRelative()) {
00191 fileName = fInfo.absoluteFilePath();
00192 }
00193 loadStartupFiles << fileName;
00194 }
00195 }
00196
00197 if (showUsage || nextIsErrorReportFile) {
00198
00199 cerr << "Usage: engauge "
00200 << "[" << DASH_DEBUG.toLatin1().data() << "] "
00201 << "[" << DASH_ERROR_REPORT.toLatin1().data() << " <file>] "
00202 << "[" << DASH_FILE_CMD_SCRIPT.toLatin1().data() << " <file> "
00203 << "[" << DASH_GNUPLOT.toLatin1().data() << "] "
00204 << "[" << DASH_HELP.toLatin1().data() << "] "
00205 << "[" << DASH_REGRESSION.toLatin1().data() << "] "
00206 << "[<load_file1>] [<load_file2>] ..." << endl
00207 << " " << DASH_DEBUG.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00208 << QObject::tr ("Enables extra debug information. Used for debugging").toLatin1().data() << endl
00209 << " " << DASH_ERROR_REPORT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00210 << QObject::tr ("Specifies an error report file as input. Used for debugging and testing").toLatin1().data() << endl
00211 << " " << DASH_FILE_CMD_SCRIPT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00212 << QObject::tr ("Specifies a file command script file as input. Used for debugging and testing").toLatin1().data() << endl
00213 << " " << DASH_GNUPLOT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00214 << QObject::tr ("Output diagnostic gnuplot input files. Used for debugging").toLatin1().data() << endl
00215 << " " << DASH_HELP.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00216 << QObject::tr ("Show this help information").toLatin1().data() << endl
00217 << " " << DASH_REGRESSION.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00218 << QObject::tr ("Executes the error report file or file command script. Used for regression testing").toLatin1().data() << endl
00219 << " " << QString ("<load file> ").leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00220 << QObject::tr ("File(s) to be imported or opened at startup").toLatin1().data() << endl;
00221
00222 exit (0);
00223 }
00224 }