Subversion Repositories public iLand

Rev

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

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