Rev 704 | Rev 779 | 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> |
||
679 | werner | 24 | #include <QKeyEvent> |
678 | werner | 25 | |
26 | #include "global.h" |
||
27 | #include "model.h" |
||
28 | #include "modelcontroller.h" |
||
760 | werner | 29 | #include "version.h" |
678 | werner | 30 | |
31 | QTextStream *ConsoleShell::mLogStream = 0; |
||
32 | |||
679 | werner | 33 | // a try to really get keyboard strokes in console mode... |
34 | // did not work. |
||
35 | class KeyboardTaker : public QThread |
||
36 | { |
||
37 | public: |
||
38 | void run(); |
||
39 | bool stop; |
||
40 | }; |
||
41 | |||
42 | void KeyboardTaker::run() |
||
43 | { |
||
44 | stop = false; |
||
45 | QTextStream qin(stdin, QFile::ReadOnly); |
||
46 | while (!stop) { |
||
47 | QString input = qin.read(1); |
||
48 | if (!input.isNull()) { |
||
49 | // .. process input |
||
50 | qWarning() << "input:" << input; |
||
51 | } |
||
52 | } |
||
53 | } |
||
54 | |||
678 | werner | 55 | ConsoleShell::ConsoleShell() |
56 | { |
||
57 | } |
||
58 | |||
679 | werner | 59 | /* |
60 | */ |
||
61 | |||
678 | werner | 62 | void ConsoleShell::run() |
63 | { |
||
679 | werner | 64 | |
65 | QString xml_name = QCoreApplication::arguments().at(1); |
||
66 | if (!QFile::exists(xml_name)) { |
||
67 | qDebug() << "invalid XML project file: " << xml_name; |
||
68 | return; |
||
69 | } |
||
70 | // get the number of years to run... |
||
71 | bool ok; |
||
72 | int years = QCoreApplication::arguments().at(2).toInt(&ok); |
||
73 | if (years<0 || !ok) { |
||
74 | qDebug() << QCoreApplication::arguments().at(2) << "is an invalid number of years to run!"; |
||
75 | return; |
||
76 | } |
||
77 | |||
678 | werner | 78 | try { |
79 | |||
80 | ModelController iland_model; |
||
81 | QObject::connect(&iland_model, SIGNAL(year(int)),SLOT(runYear(int))); |
||
82 | iland_model.setFileName(xml_name); |
||
83 | |||
84 | setupLogging(); |
||
85 | |||
86 | qDebug() << "**************************************************"; |
||
87 | qDebug() << "*********** iLand console session ********"; |
||
88 | qDebug() << "**************************************************"; |
||
760 | werner | 89 | qDebug() << "started at: " << QDateTime::currentDateTime().toString(Qt::ISODate); |
90 | qDebug() << "iLand " << currentVersion() << " (" << svnRevision() << ")"; |
||
678 | werner | 91 | qDebug() << "**************************************************"; |
92 | |||
93 | qWarning() << "*** creating model..."; |
||
94 | qWarning() << "**************************************************"; |
||
95 | |||
96 | iland_model.create(); |
||
97 | qWarning() << "**************************************************"; |
||
679 | werner | 98 | qWarning() << "*** running model for" << years << "years"; |
678 | werner | 99 | qWarning() << "**************************************************"; |
100 | |||
679 | werner | 101 | iland_model.run(years + 1); |
102 | |||
678 | werner | 103 | qWarning() << "**************************************************"; |
104 | qWarning() << "*** model run finished."; |
||
105 | qWarning() << "*** " << QDateTime::currentDateTime(); |
||
106 | qWarning() << "**************************************************"; |
||
107 | |||
108 | } catch (const IException &e) { |
||
109 | qWarning() << "*** An exception occured ***"; |
||
110 | qWarning() << e.message(); |
||
111 | } |
||
112 | catch (const std::exception &e) { |
||
113 | qWarning() << "*** An exception occured ***"; |
||
114 | qWarning() << e.what(); |
||
115 | } |
||
116 | QCoreApplication::quit(); |
||
117 | |||
118 | |||
119 | } |
||
120 | |||
121 | void ConsoleShell::runYear(int year) |
||
122 | { |
||
679 | werner | 123 | printf("simulating year %d ...\n", year-1); |
678 | werner | 124 | } |
125 | |||
704 | werner | 126 | QMutex qdebug_mutex; |
678 | werner | 127 | void myMessageOutput(QtMsgType type, const char *msg) |
128 | { |
||
129 | |||
704 | werner | 130 | QMutexLocker m(&qdebug_mutex); |
678 | werner | 131 | switch (type) { |
132 | case QtDebugMsg: |
||
133 | *ConsoleShell::logStream() << msg << endl; |
||
134 | break; |
||
135 | case QtWarningMsg: |
||
136 | *ConsoleShell::logStream() << msg << endl; |
||
137 | printf("Warning: %s\n", msg); |
||
138 | |||
139 | break; |
||
140 | case QtCriticalMsg: |
||
141 | *ConsoleShell::logStream() << msg << endl; |
||
142 | printf("Critical: %s\n", msg); |
||
143 | break; |
||
144 | case QtFatalMsg: |
||
145 | *ConsoleShell::logStream() << msg << endl; |
||
146 | printf("Fatal: %s\n", msg); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | |||
151 | void ConsoleShell::setupLogging() |
||
152 | { |
||
153 | if (mLogStream) { |
||
154 | if (mLogStream->device()) |
||
155 | delete mLogStream->device(); |
||
156 | delete mLogStream; |
||
157 | mLogStream = NULL; |
||
158 | } |
||
159 | |||
160 | QString fname = GlobalSettings::instance()->settings().value("system.logging.logFile", "logfile.txt"); |
||
161 | QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss"); |
||
162 | fname.replace("$date$", timestamp); |
||
163 | fname = GlobalSettings::instance()->path(fname, "log"); |
||
164 | QFile *file = new QFile(fname); |
||
165 | |||
166 | if (!file->open(QIODevice::WriteOnly)) { |
||
167 | qDebug() << "cannot open logfile" << fname; |
||
168 | } else { |
||
169 | qDebug() << "Log output is redirected to logfile" << fname; |
||
170 | mLogStream = new QTextStream(file); |
||
171 | } |
||
172 | qInstallMsgHandler(myMessageOutput); |
||
173 | |||
174 | |||
175 | } |
||
176 |