Rev 1087 | Rev 1157 | 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> |
||
989 | werner | 24 | //#include <QKeyEvent> |
678 | werner | 25 | |
26 | #include "global.h" |
||
27 | #include "model.h" |
||
28 | #include "modelcontroller.h" |
||
947 | werner | 29 | #include "../iland/version.h" |
678 | werner | 30 | |
31 | QTextStream *ConsoleShell::mLogStream = 0; |
||
1087 | werner | 32 | bool ConsoleShell::mFlushLog = false; |
678 | werner | 33 | |
679 | werner | 34 | // a try to really get keyboard strokes in console mode... |
35 | // did not work. |
||
36 | class KeyboardTaker : public QThread |
||
37 | { |
||
38 | public: |
||
39 | void run(); |
||
40 | bool stop; |
||
41 | }; |
||
42 | |||
43 | void KeyboardTaker::run() |
||
44 | { |
||
45 | stop = false; |
||
46 | QTextStream qin(stdin, QFile::ReadOnly); |
||
47 | while (!stop) { |
||
48 | QString input = qin.read(1); |
||
49 | if (!input.isNull()) { |
||
50 | // .. process input |
||
51 | qWarning() << "input:" << input; |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | |||
678 | werner | 56 | ConsoleShell::ConsoleShell() |
57 | { |
||
58 | } |
||
59 | |||
679 | werner | 60 | /* |
61 | */ |
||
62 | |||
678 | werner | 63 | void ConsoleShell::run() |
64 | { |
||
679 | werner | 65 | |
66 | QString xml_name = QCoreApplication::arguments().at(1); |
||
67 | // get the number of years to run... |
||
68 | bool ok; |
||
69 | int years = QCoreApplication::arguments().at(2).toInt(&ok); |
||
70 | if (years<0 || !ok) { |
||
71 | qDebug() << QCoreApplication::arguments().at(2) << "is an invalid number of years to run!"; |
||
994 | werner | 72 | QCoreApplication::quit(); |
679 | werner | 73 | return; |
74 | } |
||
75 | |||
837 | werner | 76 | if (!QFile::exists(xml_name)) { |
77 | qDebug() << "invalid XML project file: " << xml_name; |
||
994 | werner | 78 | QCoreApplication::quit(); |
837 | werner | 79 | return; |
80 | } |
||
678 | werner | 81 | try { |
82 | |||
83 | ModelController iland_model; |
||
1081 | werner | 84 | GlobalSettings::instance()->setModelController( &iland_model ); |
678 | werner | 85 | QObject::connect(&iland_model, SIGNAL(year(int)),SLOT(runYear(int))); |
86 | iland_model.setFileName(xml_name); |
||
837 | werner | 87 | if (iland_model.hasError()) { |
88 | qWarning() << "!!!! ERROR !!!!"; |
||
89 | qWarning() << iland_model.lastError(); |
||
90 | qWarning() << "!!!! ERROR !!!!"; |
||
994 | werner | 91 | QCoreApplication::quit(); |
837 | werner | 92 | return; |
93 | } |
||
678 | werner | 94 | |
837 | werner | 95 | mParams.clear(); |
96 | if (QCoreApplication::arguments().count()>3) { |
||
97 | qWarning() << "set command line values:"; |
||
98 | for (int i=3;i<QCoreApplication::arguments().count();++i) { |
||
99 | QString line = QCoreApplication::arguments().at(i); |
||
100 | mParams.append(line); |
||
101 | QString key = line.left(line.indexOf('=')); |
||
102 | QString value = line.mid(line.indexOf('=')+1); |
||
103 | const_cast<XmlHelper&>(GlobalSettings::instance()->settings()).setNodeValue(key, value); |
||
994 | werner | 104 | qWarning() << "set" << key << "to value:" << value << "(set:" << GlobalSettings::instance()->settings().value(key) << ")."; |
837 | werner | 105 | } |
106 | } |
||
1075 | werner | 107 | setupLogging(); |
108 | |||
678 | werner | 109 | qDebug() << "**************************************************"; |
110 | qDebug() << "*********** iLand console session ********"; |
||
111 | qDebug() << "**************************************************"; |
||
760 | werner | 112 | qDebug() << "started at: " << QDateTime::currentDateTime().toString(Qt::ISODate); |
113 | qDebug() << "iLand " << currentVersion() << " (" << svnRevision() << ")"; |
||
678 | werner | 114 | qDebug() << "**************************************************"; |
115 | |||
116 | qWarning() << "*** creating model..."; |
||
117 | qWarning() << "**************************************************"; |
||
118 | |||
119 | iland_model.create(); |
||
837 | werner | 120 | if (iland_model.hasError()) { |
121 | qWarning() << "!!!! ERROR !!!!"; |
||
122 | qWarning() << iland_model.lastError(); |
||
123 | qWarning() << "!!!! ERROR !!!!"; |
||
994 | werner | 124 | QCoreApplication::quit(); |
837 | werner | 125 | return; |
126 | } |
||
127 | runJavascript("onCreate"); |
||
678 | werner | 128 | qWarning() << "**************************************************"; |
679 | werner | 129 | qWarning() << "*** running model for" << years << "years"; |
678 | werner | 130 | qWarning() << "**************************************************"; |
131 | |||
679 | werner | 132 | iland_model.run(years + 1); |
837 | werner | 133 | if (iland_model.hasError()) { |
134 | qWarning() << "!!!! ERROR !!!!"; |
||
135 | qWarning() << iland_model.lastError(); |
||
136 | qWarning() << "!!!! ERROR !!!!"; |
||
994 | werner | 137 | QCoreApplication::quit(); |
837 | werner | 138 | return; |
139 | } |
||
140 | runJavascript("onFinish"); |
||
679 | werner | 141 | |
678 | werner | 142 | qWarning() << "**************************************************"; |
143 | qWarning() << "*** model run finished."; |
||
144 | qWarning() << "*** " << QDateTime::currentDateTime(); |
||
145 | qWarning() << "**************************************************"; |
||
146 | |||
147 | } catch (const IException &e) { |
||
148 | qWarning() << "*** An exception occured ***"; |
||
149 | qWarning() << e.message(); |
||
150 | } |
||
151 | catch (const std::exception &e) { |
||
837 | werner | 152 | qWarning() << "*** An (std)exception occured ***"; |
678 | werner | 153 | qWarning() << e.what(); |
154 | } |
||
155 | QCoreApplication::quit(); |
||
156 | |||
157 | |||
158 | } |
||
159 | |||
160 | void ConsoleShell::runYear(int year) |
||
161 | { |
||
679 | werner | 162 | printf("simulating year %d ...\n", year-1); |
678 | werner | 163 | } |
164 | |||
704 | werner | 165 | QMutex qdebug_mutex; |
780 | werner | 166 | void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) |
678 | werner | 167 | { |
780 | werner | 168 | Q_UNUSED(context); |
169 | QMutexLocker m(&qdebug_mutex); |
||
678 | werner | 170 | |
171 | switch (type) { |
||
172 | case QtDebugMsg: |
||
173 | *ConsoleShell::logStream() << msg << endl; |
||
1087 | werner | 174 | if (ConsoleShell::flush()) |
175 | ConsoleShell::logStream()->flush();; |
||
678 | werner | 176 | break; |
177 | case QtWarningMsg: |
||
178 | *ConsoleShell::logStream() << msg << endl; |
||
1087 | werner | 179 | if (ConsoleShell::flush()) |
180 | ConsoleShell::logStream()->flush();; |
||
984 | werner | 181 | printf("Warning: %s\n", msg.toLocal8Bit().data()); |
678 | werner | 182 | |
183 | break; |
||
184 | case QtCriticalMsg: |
||
185 | *ConsoleShell::logStream() << msg << endl; |
||
1087 | werner | 186 | if (ConsoleShell::flush()) |
187 | ConsoleShell::logStream()->flush();; |
||
984 | werner | 188 | printf("Critical: %s\n", msg.toLocal8Bit().data()); |
678 | werner | 189 | break; |
190 | case QtFatalMsg: |
||
191 | *ConsoleShell::logStream() << msg << endl; |
||
1087 | werner | 192 | if (ConsoleShell::flush()) |
193 | ConsoleShell::logStream()->flush();; |
||
984 | werner | 194 | printf("Fatal: %s\n", msg.toLocal8Bit().data()); |
678 | werner | 195 | } |
196 | } |
||
197 | |||
198 | |||
199 | void ConsoleShell::setupLogging() |
||
200 | { |
||
201 | if (mLogStream) { |
||
202 | if (mLogStream->device()) |
||
203 | delete mLogStream->device(); |
||
204 | delete mLogStream; |
||
205 | mLogStream = NULL; |
||
206 | } |
||
207 | |||
208 | QString fname = GlobalSettings::instance()->settings().value("system.logging.logFile", "logfile.txt"); |
||
1087 | werner | 209 | mFlushLog = GlobalSettings::instance()->settings().valueBool("system.logging.flush"); |
678 | werner | 210 | QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss"); |
211 | fname.replace("$date$", timestamp); |
||
212 | fname = GlobalSettings::instance()->path(fname, "log"); |
||
213 | QFile *file = new QFile(fname); |
||
214 | |||
215 | if (!file->open(QIODevice::WriteOnly)) { |
||
216 | qDebug() << "cannot open logfile" << fname; |
||
217 | } else { |
||
218 | qDebug() << "Log output is redirected to logfile" << fname; |
||
219 | mLogStream = new QTextStream(file); |
||
220 | } |
||
780 | werner | 221 | qInstallMessageHandler(myMessageOutput); |
678 | werner | 222 | |
223 | |||
224 | } |
||
225 | |||
837 | werner | 226 | void ConsoleShell::runJavascript(const QString key) |
227 | { |
||
228 | for (int i=0;i<mParams.count(); ++i) { |
||
229 | QString line=mParams[i]; |
||
230 | QString pkey = line.left(line.indexOf('=')); |
||
231 | if (pkey == key) { |
||
232 | QString command = line.mid(line.indexOf('=')+1); |
||
233 | // execute the function |
||
234 | qWarning() << "executing trigger" << key; |
||
235 | qWarning() << GlobalSettings::instance()->executeJavascript(command); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | |||
240 | } |
||
241 |