Rev 1104 | Rev 1182 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | |||
678 | werner | 2 | /******************************************************************************************** |
3 | ** iLand - an individual based forest landscape and disturbance model |
||
4 | ** http://iland.boku.ac.at |
||
5 | ** Copyright (C) 2009- Werner Rammer, Rupert Seidl |
||
6 | ** |
||
7 | ** This program is free software: you can redistribute it and/or modify |
||
8 | ** it under the terms of the GNU General Public License as published by |
||
9 | ** the Free Software Foundation, either version 3 of the License, or |
||
10 | ** (at your option) any later version. |
||
11 | ** |
||
12 | ** This program is distributed in the hope that it will be useful, |
||
13 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | ** GNU General Public License for more details. |
||
16 | ** |
||
17 | ** You should have received a copy of the GNU General Public License |
||
18 | ** along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
19 | ********************************************************************************************/ |
||
20 | |||
21 | #include "consoleshell.h" |
||
22 | |||
23 | #include <QtCore> |
||
1157 | werner | 24 | #include <QtDebug> |
989 | werner | 25 | //#include <QKeyEvent> |
678 | werner | 26 | |
27 | #include "global.h" |
||
28 | #include "model.h" |
||
29 | #include "modelcontroller.h" |
||
947 | werner | 30 | #include "../iland/version.h" |
678 | werner | 31 | |
32 | QTextStream *ConsoleShell::mLogStream = 0; |
||
1087 | werner | 33 | bool ConsoleShell::mFlushLog = false; |
678 | werner | 34 | |
679 | werner | 35 | // a try to really get keyboard strokes in console mode... |
36 | // did not work. |
||
37 | class KeyboardTaker : public QThread |
||
38 | { |
||
39 | public: |
||
40 | void run(); |
||
41 | bool stop; |
||
42 | }; |
||
43 | |||
44 | void KeyboardTaker::run() |
||
45 | { |
||
46 | stop = false; |
||
47 | QTextStream qin(stdin, QFile::ReadOnly); |
||
48 | while (!stop) { |
||
49 | QString input = qin.read(1); |
||
50 | if (!input.isNull()) { |
||
51 | // .. process input |
||
52 | qWarning() << "input:" << input; |
||
53 | } |
||
54 | } |
||
55 | } |
||
56 | |||
678 | werner | 57 | ConsoleShell::ConsoleShell() |
58 | { |
||
59 | } |
||
60 | |||
679 | werner | 61 | /* |
62 | */ |
||
63 | |||
678 | werner | 64 | void ConsoleShell::run() |
65 | { |
||
679 | werner | 66 | |
67 | QString xml_name = QCoreApplication::arguments().at(1); |
||
68 | // get the number of years to run... |
||
69 | bool ok; |
||
70 | int years = QCoreApplication::arguments().at(2).toInt(&ok); |
||
71 | if (years<0 || !ok) { |
||
72 | qDebug() << QCoreApplication::arguments().at(2) << "is an invalid number of years to run!"; |
||
994 | werner | 73 | QCoreApplication::quit(); |
679 | werner | 74 | return; |
75 | } |
||
76 | |||
837 | werner | 77 | if (!QFile::exists(xml_name)) { |
78 | qDebug() << "invalid XML project file: " << xml_name; |
||
994 | werner | 79 | QCoreApplication::quit(); |
837 | werner | 80 | return; |
81 | } |
||
678 | werner | 82 | try { |
83 | |||
84 | ModelController iland_model; |
||
1081 | werner | 85 | GlobalSettings::instance()->setModelController( &iland_model ); |
678 | werner | 86 | QObject::connect(&iland_model, SIGNAL(year(int)),SLOT(runYear(int))); |
87 | iland_model.setFileName(xml_name); |
||
837 | werner | 88 | if (iland_model.hasError()) { |
89 | qWarning() << "!!!! ERROR !!!!"; |
||
90 | qWarning() << iland_model.lastError(); |
||
91 | qWarning() << "!!!! ERROR !!!!"; |
||
994 | werner | 92 | QCoreApplication::quit(); |
837 | werner | 93 | return; |
94 | } |
||
678 | werner | 95 | |
837 | werner | 96 | mParams.clear(); |
97 | if (QCoreApplication::arguments().count()>3) { |
||
98 | qWarning() << "set command line values:"; |
||
99 | for (int i=3;i<QCoreApplication::arguments().count();++i) { |
||
100 | QString line = QCoreApplication::arguments().at(i); |
||
1157 | werner | 101 | line = line.remove(QChar('"')); // drop quotes |
837 | werner | 102 | mParams.append(line); |
1157 | werner | 103 | //qDebug() << qPrintable(line); |
837 | werner | 104 | QString key = line.left(line.indexOf('=')); |
105 | QString value = line.mid(line.indexOf('=')+1); |
||
106 | const_cast<XmlHelper&>(GlobalSettings::instance()->settings()).setNodeValue(key, value); |
||
1157 | werner | 107 | qWarning() << QString("set '%1' to value '%2'. result: '%3'").arg(key).arg(value).arg(GlobalSettings::instance()->settings().value(key)); |
837 | werner | 108 | } |
109 | } |
||
1075 | werner | 110 | setupLogging(); |
111 | |||
678 | werner | 112 | qDebug() << "**************************************************"; |
113 | qDebug() << "*********** iLand console session ********"; |
||
114 | qDebug() << "**************************************************"; |
||
1157 | werner | 115 | qDebug() << "started at: " << QDateTime::currentDateTime().toString("hh:mm:ss"); |
760 | werner | 116 | qDebug() << "iLand " << currentVersion() << " (" << svnRevision() << ")"; |
678 | werner | 117 | qDebug() << "**************************************************"; |
118 | |||
119 | qWarning() << "*** creating model..."; |
||
120 | qWarning() << "**************************************************"; |
||
121 | |||
122 | iland_model.create(); |
||
837 | werner | 123 | if (iland_model.hasError()) { |
124 | qWarning() << "!!!! ERROR !!!!"; |
||
125 | qWarning() << iland_model.lastError(); |
||
126 | qWarning() << "!!!! ERROR !!!!"; |
||
994 | werner | 127 | QCoreApplication::quit(); |
837 | werner | 128 | return; |
129 | } |
||
130 | runJavascript("onCreate"); |
||
678 | werner | 131 | qWarning() << "**************************************************"; |
679 | werner | 132 | qWarning() << "*** running model for" << years << "years"; |
678 | werner | 133 | qWarning() << "**************************************************"; |
134 | |||
679 | werner | 135 | iland_model.run(years + 1); |
837 | werner | 136 | if (iland_model.hasError()) { |
137 | qWarning() << "!!!! ERROR !!!!"; |
||
138 | qWarning() << iland_model.lastError(); |
||
139 | qWarning() << "!!!! ERROR !!!!"; |
||
994 | werner | 140 | QCoreApplication::quit(); |
837 | werner | 141 | return; |
142 | } |
||
143 | runJavascript("onFinish"); |
||
679 | werner | 144 | |
678 | werner | 145 | qWarning() << "**************************************************"; |
146 | qWarning() << "*** model run finished."; |
||
147 | qWarning() << "**************************************************"; |
||
148 | |||
149 | } catch (const IException &e) { |
||
150 | qWarning() << "*** An exception occured ***"; |
||
151 | qWarning() << e.message(); |
||
152 | } |
||
153 | catch (const std::exception &e) { |
||
837 | werner | 154 | qWarning() << "*** An (std)exception occured ***"; |
678 | werner | 155 | qWarning() << e.what(); |
156 | } |
||
157 | QCoreApplication::quit(); |
||
158 | |||
159 | |||
160 | } |
||
161 | |||
162 | void ConsoleShell::runYear(int year) |
||
163 | { |
||
1157 | werner | 164 | printf("%s: simulating year %d ...\n", QDateTime::currentDateTime().toString("hh:mm:ss").toLocal8Bit().data(), year-1); |
678 | werner | 165 | } |
166 | |||
1157 | werner | 167 | static QMutex qdebug_mutex; |
780 | werner | 168 | void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) |
678 | werner | 169 | { |
780 | werner | 170 | Q_UNUSED(context); |
171 | QMutexLocker m(&qdebug_mutex); |
||
678 | werner | 172 | |
173 | switch (type) { |
||
1157 | werner | 174 | case QtDebugMsg: |
678 | werner | 175 | *ConsoleShell::logStream() << msg << endl; |
1087 | werner | 176 | if (ConsoleShell::flush()) |
1157 | werner | 177 | ConsoleShell::logStream()->flush(); |
178 | break; |
||
179 | case QtWarningMsg: |
||
678 | werner | 180 | *ConsoleShell::logStream() << msg << endl; |
1087 | werner | 181 | if (ConsoleShell::flush()) |
1157 | werner | 182 | ConsoleShell::logStream()->flush(); |
183 | printf("%s: %s\n", QDateTime::currentDateTime().toString("hh:mm:ss").toLocal8Bit().data(), msg.toLocal8Bit().data()); |
||
184 | break; |
||
185 | // available from qt5.5 |
||
186 | // case QtInfoMsg: |
||
187 | // *ConsoleShell::logStream() << msg << endl; |
||
188 | // if (ConsoleShell::flush()) |
||
189 | // ConsoleShell::logStream()->flush(); |
||
190 | // printf("%s: %s\n", QDateTime::currentDateTime().toString("hh:mm:ss").toLocal8Bit().data(), msg.toLocal8Bit().data()); |
||
678 | werner | 191 | |
1157 | werner | 192 | // break; |
193 | case QtCriticalMsg: |
||
678 | werner | 194 | *ConsoleShell::logStream() << msg << endl; |
1087 | werner | 195 | if (ConsoleShell::flush()) |
1157 | werner | 196 | ConsoleShell::logStream()->flush(); |
984 | werner | 197 | printf("Critical: %s\n", msg.toLocal8Bit().data()); |
1157 | werner | 198 | break; |
199 | case QtFatalMsg: |
||
678 | werner | 200 | *ConsoleShell::logStream() << msg << endl; |
1087 | werner | 201 | if (ConsoleShell::flush()) |
1157 | werner | 202 | ConsoleShell::logStream()->flush(); |
984 | werner | 203 | printf("Fatal: %s\n", msg.toLocal8Bit().data()); |
1157 | werner | 204 | } |
678 | werner | 205 | } |
206 | |||
207 | |||
208 | void ConsoleShell::setupLogging() |
||
209 | { |
||
210 | if (mLogStream) { |
||
211 | if (mLogStream->device()) |
||
212 | delete mLogStream->device(); |
||
213 | delete mLogStream; |
||
214 | mLogStream = NULL; |
||
215 | } |
||
216 | |||
217 | QString fname = GlobalSettings::instance()->settings().value("system.logging.logFile", "logfile.txt"); |
||
1087 | werner | 218 | mFlushLog = GlobalSettings::instance()->settings().valueBool("system.logging.flush"); |
678 | werner | 219 | QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss"); |
220 | fname.replace("$date$", timestamp); |
||
221 | fname = GlobalSettings::instance()->path(fname, "log"); |
||
222 | QFile *file = new QFile(fname); |
||
223 | |||
224 | if (!file->open(QIODevice::WriteOnly)) { |
||
225 | qDebug() << "cannot open logfile" << fname; |
||
226 | } else { |
||
227 | qDebug() << "Log output is redirected to logfile" << fname; |
||
228 | mLogStream = new QTextStream(file); |
||
229 | } |
||
780 | werner | 230 | qInstallMessageHandler(myMessageOutput); |
678 | werner | 231 | |
232 | |||
233 | } |
||
234 | |||
837 | werner | 235 | void ConsoleShell::runJavascript(const QString key) |
236 | { |
||
237 | for (int i=0;i<mParams.count(); ++i) { |
||
238 | QString line=mParams[i]; |
||
239 | QString pkey = line.left(line.indexOf('=')); |
||
240 | if (pkey == key) { |
||
241 | QString command = line.mid(line.indexOf('=')+1); |
||
242 | // execute the function |
||
243 | qWarning() << "executing trigger" << key; |
||
244 | qWarning() << GlobalSettings::instance()->executeJavascript(command); |
||
245 | } |
||
246 | } |
||
247 | |||
248 | |||
249 | } |
||
250 |