Subversion Repositories public iLand

Rev

Rev 947 | Rev 989 | 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"
947 werner 29
#include "../iland/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
    // get the number of years to run...
67
    bool ok;
68
    int years = QCoreApplication::arguments().at(2).toInt(&ok);
69
    if (years<0 || !ok) {
70
        qDebug() << QCoreApplication::arguments().at(2) << "is an invalid number of years to run!";
71
        return;
72
    }
73
 
837 werner 74
    if (!QFile::exists(xml_name)) {
75
        qDebug() << "invalid XML project file: " << xml_name;
76
        return;
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);
837 werner 83
        if (iland_model.hasError()) {
84
            qWarning() << "!!!! ERROR !!!!";
85
            qWarning() << iland_model.lastError();
86
            qWarning() << "!!!! ERROR !!!!";
87
            return;
88
        }
678 werner 89
 
90
        setupLogging();
837 werner 91
        mParams.clear();
92
        if (QCoreApplication::arguments().count()>3) {
93
            qWarning() << "set command line values:";
94
            for (int i=3;i<QCoreApplication::arguments().count();++i) {
95
                QString line = QCoreApplication::arguments().at(i);
96
                mParams.append(line);
97
                QString key = line.left(line.indexOf('='));
98
                QString value = line.mid(line.indexOf('=')+1);
99
                qWarning() << "set" << key << "to value:" << value;
100
                const_cast<XmlHelper&>(GlobalSettings::instance()->settings()).setNodeValue(key, value);
101
            }
102
        }
678 werner 103
        qDebug() << "**************************************************";
104
        qDebug() << "***********     iLand console session     ********";
105
        qDebug() << "**************************************************";
760 werner 106
        qDebug() << "started at: " << QDateTime::currentDateTime().toString(Qt::ISODate);
107
        qDebug() << "iLand " << currentVersion() << " (" << svnRevision() << ")";
678 werner 108
        qDebug() << "**************************************************";
109
 
110
        qWarning() << "*** creating model...";
111
        qWarning() << "**************************************************";
112
 
113
        iland_model.create();
837 werner 114
        if (iland_model.hasError()) {
115
            qWarning() << "!!!! ERROR !!!!";
116
            qWarning() << iland_model.lastError();
117
            qWarning() << "!!!! ERROR !!!!";
118
            return;
119
        }
120
        runJavascript("onCreate");
678 werner 121
        qWarning() << "**************************************************";
679 werner 122
        qWarning() << "*** running model for" << years << "years";
678 werner 123
        qWarning() << "**************************************************";
124
 
679 werner 125
        iland_model.run(years + 1);
837 werner 126
        if (iland_model.hasError()) {
127
            qWarning() << "!!!! ERROR !!!!";
128
            qWarning() << iland_model.lastError();
129
            qWarning() << "!!!! ERROR !!!!";
130
            return;
131
        }
132
        runJavascript("onFinish");
679 werner 133
 
678 werner 134
        qWarning() << "**************************************************";
135
        qWarning() << "*** model run finished.";
136
        qWarning() << "*** " << QDateTime::currentDateTime();
137
        qWarning() << "**************************************************";
138
 
139
    } catch (const IException &e) {
140
        qWarning() << "*** An exception occured ***";
141
        qWarning() << e.message();
142
    }
143
    catch (const std::exception &e) {
837 werner 144
        qWarning() << "*** An (std)exception occured ***";
678 werner 145
        qWarning() << e.what();
146
    }
147
    QCoreApplication::quit();
148
 
149
 
150
}
151
 
152
void ConsoleShell::runYear(int year)
153
{
679 werner 154
    printf("simulating year %d ...\n", year-1);
678 werner 155
}
156
 
704 werner 157
QMutex qdebug_mutex;
780 werner 158
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
678 werner 159
 {
780 werner 160
    Q_UNUSED(context);
161
    QMutexLocker m(&qdebug_mutex);
678 werner 162
 
163
    switch (type) {
164
     case QtDebugMsg:
165
        *ConsoleShell::logStream() << msg << endl;
166
         break;
167
     case QtWarningMsg:
168
        *ConsoleShell::logStream() << msg << endl;
984 werner 169
        printf("Warning: %s\n", msg.toLocal8Bit().data());
678 werner 170
 
171
         break;
172
     case QtCriticalMsg:
173
        *ConsoleShell::logStream() << msg << endl;
984 werner 174
        printf("Critical: %s\n", msg.toLocal8Bit().data());
678 werner 175
         break;
176
     case QtFatalMsg:
177
        *ConsoleShell::logStream() << msg << endl;
984 werner 178
        printf("Fatal: %s\n", msg.toLocal8Bit().data());
678 werner 179
     }
180
 }
181
 
182
 
183
void ConsoleShell::setupLogging()
184
{
185
    if (mLogStream) {
186
        if (mLogStream->device())
187
            delete mLogStream->device();
188
        delete mLogStream;
189
        mLogStream = NULL;
190
    }
191
 
192
    QString fname = GlobalSettings::instance()->settings().value("system.logging.logFile", "logfile.txt");
193
    QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss");
194
    fname.replace("$date$", timestamp);
195
    fname = GlobalSettings::instance()->path(fname, "log");
196
    QFile *file = new QFile(fname);
197
 
198
    if (!file->open(QIODevice::WriteOnly)) {
199
        qDebug() << "cannot open logfile" << fname;
200
    } else {
201
        qDebug() << "Log output is redirected to logfile" << fname;
202
        mLogStream = new QTextStream(file);
203
    }
780 werner 204
    qInstallMessageHandler(myMessageOutput);
678 werner 205
 
206
 
207
}
208
 
837 werner 209
void ConsoleShell::runJavascript(const QString key)
210
{
211
    for (int i=0;i<mParams.count(); ++i) {
212
        QString line=mParams[i];
213
        QString pkey = line.left(line.indexOf('='));
214
        if (pkey == key) {
215
            QString command = line.mid(line.indexOf('=')+1);
216
            // execute the function
217
            qWarning() << "executing trigger" << key;
218
            qWarning() << GlobalSettings::instance()->executeJavascript(command);
219
        }
220
    }
221
 
222
 
223
}
224