OutputHandler.cpp 5.01 KB
Newer Older
1
/**
Weichen's avatar
Weichen committed
2 3
 * \file        OutputHandler.cpp
 * \date        Nov 20, 2010
4
 * \version     v0.6
Weichen's avatar
Weichen committed
5
 * \copyright   <2009-2014> Forschungszentrum Jülich GmbH. All rights reserved.
6
 *
Weichen's avatar
Weichen committed
7
 * \section License
8 9 10
 * This file is part of JuPedSim.
 *
 * JuPedSim is free software: you can redistribute it and/or modify
Weichen's avatar
Weichen committed
11
 * it under the terms of the GNU Lesser General Public License as published by
12 13 14 15 16 17 18 19
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * JuPedSim is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
Weichen's avatar
Weichen committed
20
 * You should have received a copy of the GNU Lesser General Public License
21 22
 * along with JuPedSim. If not, see <http://www.gnu.org/licenses/>.
 *
Weichen's avatar
Weichen committed
23
 * \section Description
24 25
 *
 *
Weichen's avatar
Weichen committed
26 27
 **/

28

Ulrich Kemloh's avatar
Ulrich Kemloh committed
29 30
#include "OutputHandler.h"

31 32
#include <stdio.h>
#include <stdarg.h>
33
#include <cstdlib>
34
#include <cmath>
35 36 37

using namespace std;

Mohcine Chraibi's avatar
Mohcine Chraibi committed
38 39
void OutputHandler::incrementWarnings()
{
40
     _nWarnings += 1;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
41 42 43 44
}

int OutputHandler::GetWarnings()
{
45
     return _nWarnings;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
46 47 48 49
}

void OutputHandler::incrementErrors()
{
50
     _nErrors += 1;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
51 52 53 54
}

int OutputHandler::GetErrors()
{
55
     return _nErrors;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
56 57
}

58
void OutputHandler::Write(const string& str)
59
{
60
    cout << str << endl;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
61 62
}

Mohcine Chraibi's avatar
Mohcine Chraibi committed
63
void OutputHandler::ProgressBar(double TotalPeds, double NowPeds)
Mohcine Chraibi's avatar
Mohcine Chraibi committed
64 65 66 67 68 69 70
{
     // based on this answer:
     // https://stackoverflow.com/questions/1637587/c-libcurl-console-progress-bar
     // how wide you want the progress meter to be
     int totaldotz=40;
     double fraction = NowPeds / TotalPeds;
     // part of the progressmeter that's already "full"
71
     int dotz = static_cast<int>(round(fraction * totaldotz));
Mohcine Chraibi's avatar
Mohcine Chraibi committed
72 73 74 75 76 77 78 79

     // create the "meter"
     int ii=0;
     printf("Evacuation: %3.0f%% [",fraction*100);
     // part  that's full already
     for ( ; ii < dotz; ii++) {
          printf("=");
     }
Mohcine Chraibi's avatar
Mohcine Chraibi committed
80
     printf(">");
Mohcine Chraibi's avatar
Mohcine Chraibi committed
81 82 83 84 85 86 87 88 89
     // remaining part (spaces)
     for ( ; ii < totaldotz;ii++) {
          printf(" ");
     }
     // and back to line begin - do not forget the fflush to avoid output buffering problems!
     printf("]\r");
     fflush(stdout);
}

90
void OutputHandler::Write(const char* message,...)
91
 {
92
    char msg[CLENGTH]="";
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    va_list ap;
    va_start(ap, message);
    vsprintf(msg, message, ap);
    va_end(ap);

    string str(msg);

    if (str.find("ERROR") != string::npos)
    {
        cerr << msg << endl;
        cerr.flush();
        incrementErrors();
    }
    else if (str.find("WARNING") != string::npos)
    {
        cerr << msg << endl;
        cerr.flush();
        incrementWarnings();
    }
    else
    { // infos
        cout << msg << endl;
        cout.flush();
    }
117 118
}

119
void STDIOHandler::Write(const string& str)
120
{
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    if (str.find("ERROR") != string::npos)
       {
           cerr << str << endl;
           cerr.flush();
           incrementErrors();
       }
       else if (str.find("WARNING") != string::npos)
       {
           cerr << str << endl;
           cerr.flush();
           incrementWarnings();
       }
       else
       { // infos
           cout << str << endl;
           cout.flush();
       }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
138 139
}

140 141
FileHandler::FileHandler(const char *fn)
{
142
     _pfp.open(fn);
143 144
     if (!_pfp.is_open())
     {
145 146 147 148 149
          char tmp[CLENGTH];
          sprintf(tmp, "Error!!! File [%s] could not be opened!", fn);
          cerr << tmp << endl;
          exit(0);
     }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
150 151
}

152 153
FileHandler::~FileHandler()
{
154
     _pfp.close();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
155 156
}

157
void FileHandler::Write(const string& str)
158
{
159 160 161 162 163 164 165 166 167 168 169 170 171
    if (this != NULL) {
        _pfp << str << endl;
        _pfp.flush();
    }

    if (str.find("ERROR") != string::npos)
    {
        incrementErrors();
    }
    else if (str.find("WARNING") != string::npos)
    {
        incrementWarnings();
    }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
172 173
}

174
void FileHandler::Write(const char* str_msg,...)
175
{
176
     char msg[CLENGTH]="";
177
     va_list ap;
178 179
     va_start (ap, str_msg);
     vsprintf (msg,str_msg ,ap);
180
     va_end (ap);
181 182
     _pfp<<msg<<endl;
     _pfp.flush();
183 184 185 186 187 188 189 190 191 192

     string str(msg);
     if (str.find("ERROR") != string::npos)
     {
         incrementErrors();
     }
     else if (str.find("WARNING") != string::npos)
     {
         incrementWarnings();
     }
193 194
}

195 196
#ifdef _SIMULATOR

197
SocketHandler::SocketHandler(const string& host, int port)
198 199 200 201 202
{
     client = new TraVisToClient(host, port);
     brokentags.push_back("<trajectories>");
     brokentags.push_back("</trajectories>");
     brokentags.push_back("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
203 204
}

205
SocketHandler::~SocketHandler()
206 207
{
     delete client;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
208 209
}

210
void SocketHandler::Write(const string& stringRef)
211
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
212

213
     vector<string>::iterator str_it;
214
     string str=stringRef;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
215

216 217 218 219 220 221 222 223
     //There are a few broken tags which need to be checked for and removed.
     for (str_it = brokentags.begin(); str_it != brokentags.end(); ++str_it) {
          int tagstart = str.find(*str_it);
          if (tagstart != (int) string::npos) {
               str.erase(str.begin() + tagstart, str.begin() + tagstart + (*str_it).size());
          }
     }
     client->sendData(str.c_str());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
224
}
225 226

#endif