Subversion Repositories public iLand

Rev

Rev 994 | Rev 1081 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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