MsgHandler.cpp
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029
00030 #include <string>
00031 #include <cassert>
00032 #include <vector>
00033 #include <algorithm>
00034 #include <iostream>
00035 #include "MsgHandler.h"
00036 #include <utils/options/OptionsCont.h>
00037 #include <utils/iodevices/OutputDevice.h>
00038 #include <utils/common/UtilExceptions.h>
00039 #include "AbstractMutex.h"
00040
00041 #ifdef CHECK_MEMORY_LEAKS
00042 #include <foreign/nvwa/debug_new.h>
00043 #endif // CHECK_MEMORY_LEAKS
00044
00045
00046
00047
00048
00049 bool gSuppressWarnings = false;
00050 bool gSuppressMessages = false;
00051
00052
00053
00054
00055
00056 MsgHandler *MsgHandler::myErrorInstance = 0;
00057 MsgHandler *MsgHandler::myWarningInstance = 0;
00058 MsgHandler *MsgHandler::myMessageInstance = 0;
00059 bool MsgHandler::myAmProcessingProcess = false;
00060 AbstractMutex *MsgHandler::myLock = 0;
00061
00062
00063
00064
00065
00066 MsgHandler *
00067 MsgHandler::getMessageInstance() {
00068 if (myMessageInstance==0) {
00069 myMessageInstance = new MsgHandler(MT_MESSAGE);
00070 }
00071 return myMessageInstance;
00072 }
00073
00074
00075 MsgHandler *
00076 MsgHandler::getWarningInstance() {
00077 if (myWarningInstance==0) {
00078 myWarningInstance = new MsgHandler(MT_WARNING);
00079 }
00080 return myWarningInstance;
00081 }
00082
00083
00084 MsgHandler *
00085 MsgHandler::getErrorInstance() {
00086 if (myErrorInstance==0) {
00087 myErrorInstance = new MsgHandler(MT_ERROR);
00088 }
00089 return myErrorInstance;
00090 }
00091
00092
00093 void
00094 MsgHandler::inform(std::string msg, bool addType) {
00095 if (myLock!=0) {
00096 myLock->lock();
00097 }
00098
00099 if (myAmProcessingProcess && (myReport2COUT || myReport2CERR)) {
00100 std::cout << std::endl;
00101 }
00102 msg = build(msg, addType);
00103
00104 if (myReport2COUT) {
00105 std::cout << msg << std::endl;
00106 }
00107
00108 if (myReport2CERR) {
00109 std::cerr << msg << std::endl;
00110 }
00111
00112 for (RetrieverVector::iterator i=myRetrievers.begin(); i!=myRetrievers.end(); i++) {
00113 (*i)->inform(msg);
00114 }
00115
00116 myWasInformed = true;
00117 myAmProcessingProcess = false;
00118 if (myLock!=0) {
00119 myLock->unlock();
00120 }
00121 }
00122
00123
00124 void
00125 MsgHandler::progressMsg(std::string msg, bool addType) {
00126 if (myLock!=0) {
00127 myLock->lock();
00128 }
00129
00130 if (myAmProcessingProcess && (myReport2COUT || myReport2CERR)) {
00131 std::cout << std::endl;
00132 }
00133 msg = build(msg, addType);
00134
00135 if (myReport2COUT) {
00136 std::cout << msg << '\r';
00137 }
00138
00139 if (myReport2CERR) {
00140 std::cerr << msg << '\r';
00141 }
00142
00143 for (RetrieverVector::iterator i=myRetrievers.begin(); i!=myRetrievers.end(); i++) {
00144 (*i)->inform(msg);
00145 }
00146
00147 myWasInformed = true;
00148 myAmProcessingProcess = false;
00149 if (myLock!=0) {
00150 myLock->unlock();
00151 }
00152 }
00153
00154
00155 void
00156 MsgHandler::beginProcessMsg(std::string msg, bool addType) {
00157 if (myLock!=0) {
00158 myLock->lock();
00159 }
00160 msg = build(msg, addType);
00161
00162 if (myReport2COUT) {
00163 std::cout << msg << ' ';
00164 std::cout.flush();
00165 myAmProcessingProcess = true;
00166 }
00167
00168 if (myReport2CERR) {
00169 std::cerr << msg << ' ';
00170 std::cerr.flush();
00171 myAmProcessingProcess = true;
00172 }
00173
00174 for (RetrieverVector::iterator i=myRetrievers.begin(); i!=myRetrievers.end(); i++) {
00175 (*i)->inform(msg + " ");
00176 myAmProcessingProcess = true;
00177 }
00178
00179 myWasInformed = true;
00180 if (myLock!=0) {
00181 myLock->unlock();
00182 }
00183 }
00184
00185
00186 void
00187 MsgHandler::endProcessMsg(std::string msg) {
00188 if (myLock!=0) {
00189 myLock->lock();
00190 }
00191
00192 if (myReport2COUT) {
00193 std::cout << msg << std::endl;
00194 }
00195
00196 if (myReport2CERR) {
00197 std::cerr << msg << std::endl;
00198 }
00199
00200 for (RetrieverVector::iterator i=myRetrievers.begin(); i!=myRetrievers.end(); i++) {
00201 (*i)->inform(msg);
00202 }
00203
00204 myWasInformed = true;
00205 myAmProcessingProcess = false;
00206 if (myLock!=0) {
00207 myLock->unlock();
00208 }
00209 }
00210
00211
00212 void
00213 MsgHandler::clear() {
00214 if (myLock!=0) {
00215 myLock->lock();
00216 }
00217 myWasInformed = false;
00218 if (myLock!=0) {
00219 myLock->unlock();
00220 }
00221 }
00222
00223
00224 void
00225 MsgHandler::addRetriever(OutputDevice *retriever) {
00226 if (myLock!=0) {
00227 myLock->lock();
00228 }
00229 RetrieverVector::iterator i =
00230 find(myRetrievers.begin(), myRetrievers.end(), retriever);
00231 if (i==myRetrievers.end()) {
00232 myRetrievers.push_back(retriever);
00233 }
00234
00235 if (myType==MT_WARNING) {
00236 gSuppressWarnings = OptionsCont::getOptions().exists("suppress-warnings")
00237 ? OptionsCont::getOptions().getBool("suppress-warnings")
00238 : false;
00239 } else if (myType==MT_MESSAGE) {
00240 gSuppressMessages = false;
00241 }
00242 if (myLock!=0) {
00243 myLock->unlock();
00244 }
00245 }
00246
00247
00248 void
00249 MsgHandler::removeRetriever(OutputDevice *retriever) {
00250 if (myLock!=0) {
00251 myLock->lock();
00252 }
00253 RetrieverVector::iterator i =
00254 find(myRetrievers.begin(), myRetrievers.end(), retriever);
00255 if (i!=myRetrievers.end()) {
00256 myRetrievers.erase(i);
00257 }
00258
00259 if (myType==MT_WARNING) {
00260 gSuppressWarnings = OptionsCont::getOptions().exists("suppress-warnings")
00261 ? OptionsCont::getOptions().getBool("suppress-warnings")
00262 : myRetrievers.size()==0;
00263 } else if (myType==MT_MESSAGE) {
00264 gSuppressMessages = !(myRetrievers.size()==0||myReport2COUT);
00265 }
00266 if (myLock!=0) {
00267 myLock->unlock();
00268 }
00269 }
00270
00271
00272 void
00273 MsgHandler::report2cout(bool value) {
00274 if (myLock!=0) {
00275 myLock->lock();
00276 }
00277 myReport2COUT = value;
00278 if (myType==MT_WARNING) {
00279 gSuppressWarnings = OptionsCont::getOptions().exists("suppress-warnings")
00280 ? OptionsCont::getOptions().getBool("suppress-warnings")
00281 : !myReport2COUT;
00282 } else if (myType==MT_MESSAGE) {
00283 gSuppressMessages = myRetrievers.size()==0&&!myReport2COUT;
00284 }
00285 std::cout.setf(std::ios::fixed, std::ios::floatfield);
00286 if (myLock!=0) {
00287 myLock->unlock();
00288 }
00289 }
00290
00291
00292 void
00293 MsgHandler::report2cerr(bool value) {
00294 if (myLock!=0) {
00295 myLock->lock();
00296 }
00297 myReport2CERR = value;
00298 if (myType==MT_WARNING) {
00299 gSuppressWarnings = OptionsCont::getOptions().exists("suppress-warnings")
00300 ? OptionsCont::getOptions().getBool("suppress-warnings")
00301 : !myReport2CERR;
00302 } else if (myType==MT_MESSAGE) {
00303 gSuppressMessages = myRetrievers.size()==0&&!myReport2COUT;
00304 }
00305 std::cerr.setf(std::ios::fixed, std::ios::floatfield);
00306 if (myLock!=0) {
00307 myLock->unlock();
00308 }
00309 }
00310
00311
00312 void
00313 MsgHandler::initOutputOptions(bool gui) {
00314 OptionsCont& oc = OptionsCont::getOptions();
00315 getMessageInstance()->report2cout(!gui && oc.getBool("verbose"));
00316 getWarningInstance()->report2cerr(!gui && !oc.getBool("suppress-warnings"));
00317
00318 if (oc.isSet("log-file")) {
00319 try {
00320 OutputDevice *logFile = &OutputDevice::getDevice(oc.getString("log-file"));
00321 getErrorInstance()->addRetriever(logFile);
00322 getWarningInstance()->addRetriever(logFile);
00323 getMessageInstance()->addRetriever(logFile);
00324 } catch (IOError &) {
00325 throw ProcessError("Could not build logging file '" + oc.getString("log-file") + "'");
00326 }
00327 }
00328 if (oc.isSet("message-log")) {
00329 try {
00330 OutputDevice *logFile = &OutputDevice::getDevice(oc.getString("message-log"));
00331 getMessageInstance()->addRetriever(logFile);
00332 } catch (IOError &) {
00333 throw ProcessError("Could not build logging file '" + oc.getString("message-log") + "'");
00334 }
00335 }
00336 if (oc.isSet("error-log")) {
00337 try {
00338 OutputDevice *logFile = &OutputDevice::getDevice(oc.getString("error-log"));
00339 getErrorInstance()->addRetriever(logFile);
00340 getWarningInstance()->addRetriever(logFile);
00341 } catch (IOError &) {
00342 throw ProcessError("Could not build logging file '" + oc.getString("error-log") + "'");
00343 }
00344 }
00345 }
00346
00347
00348 void
00349 MsgHandler::cleanupOnEnd() {
00350 if (myLock!=0) {
00351 myLock->lock();
00352 }
00353 delete myMessageInstance;
00354 myMessageInstance = 0;
00355 delete myWarningInstance;
00356 myWarningInstance = 0;
00357 delete myErrorInstance;
00358 myErrorInstance = 0;
00359 if (myLock!=0) {
00360 myLock->unlock();
00361 }
00362 }
00363
00364
00365 MsgHandler::MsgHandler(MsgType type)
00366 : myType(type), myWasInformed(false), myReport2COUT(type==MT_MESSAGE),
00367 myReport2CERR(type!=MT_MESSAGE) {}
00368
00369
00370 MsgHandler::~MsgHandler() {
00371 }
00372
00373
00374 bool
00375 MsgHandler::wasInformed() const {
00376 return myWasInformed;
00377 }
00378
00379
00380 void
00381 MsgHandler::assignLock(AbstractMutex *lock) {
00382 assert(myLock==0);
00383 myLock = lock ;
00384 }
00385
00386
00387
00388
00389