Subversion Repositories public iLand

Rev

Rev 1104 | Rev 1182 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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