00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "stdafx.h"
00037 #include "ErrorHandler.h"
00038
00039 #include "ErrorHandlerMacros.h"
00040
00041 #include <assert.h>
00042 #include <time.h>
00043
00044 #ifdef _MEMORY_DEBUG
00045 #define new DEBUG_NEW
00046 #define malloc DEBUG_MALLOC
00047 static char THIS_FILE[] = __FILE__;
00048 #endif
00049
00050 KOMODIA_NAMESPACE_START
00051
00052
00053
00054 CErrorHandler::CErrorLog::CErrorLog() : m_bAutoFlush(TRUE)
00055 {
00056 }
00057
00058 CErrorHandler::CErrorLog::CErrorLog(const CErrorLog& rLog) : m_bAutoFlush(rLog.m_bAutoFlush)
00059 {
00060 }
00061
00062 CErrorHandler::CErrorLog::~CErrorLog()
00063 {
00064 }
00065
00066 CErrorHandler::CErrorLog& CErrorHandler::CErrorLog::operator=(const CErrorLog& rLog)
00067 {
00068
00069 m_bAutoFlush=rLog.m_bAutoFlush;
00070
00071
00072 return *this;
00073 }
00074
00075 void CErrorHandler::CErrorLog::SetAutoFlush(BOOL bFlush)
00076 {
00077 m_bAutoFlush=bFlush;
00078 }
00079
00080 BOOL CErrorHandler::CErrorLog::GetAutoFlush()const
00081 {
00082 return m_bAutoFlush;
00083 }
00084
00085
00086
00087
00088
00089 CErrorHandler::CErrorsRepository::ErrorMap& CErrorHandler::CErrorsRepository::GetErrorsMap()
00090 {
00091 return m_aErrorMap;
00092 }
00093
00094 CErrorHandler::CErrorsRepository& CErrorHandler::CErrorsRepository::GetInstance()
00095 {
00096
00097 static CErrorsRepository sInstance;
00098
00099
00100 return sInstance;
00101 }
00102
00103
00104
00105
00106 CErrorHandler::CErrorLog* CErrorHandler::m_pLog=NULL;
00107
00108 #define CErrorHandler_Class "CErrorHandler"
00109
00110 CErrorHandler::CErrorHandler() : m_pLocalLog(NULL)
00111 {
00112 try
00113 {
00114
00115 SetName(CErrorHandler_Class);
00116 }
00117 ERROR_HANDLER("CErrorHandler")
00118 }
00119
00120 CErrorHandler::CErrorHandler(const CErrorHandler& rHandler) : m_pLocalLog(NULL)
00121 {
00122 try
00123 {
00124
00125 SetName(CErrorHandler_Class);
00126 }
00127 ERROR_HANDLER("CErrorHandler")
00128 }
00129
00130 CErrorHandler::~CErrorHandler()
00131 {
00132 }
00133
00134 void CErrorHandler::SetName(const std::string& rName)const
00135 {
00136
00137 m_sClassName=rName;
00138 }
00139
00140 std::string CErrorHandler::FormatData(const std::string& rClass,
00141 const std::string& rMethod,
00142 const std::string& rMsgOrError,
00143 bool bError)
00144 {
00145
00146 std::string sMsg;
00147
00148
00149 if (bError)
00150 sMsg="*** Error *** ";
00151 else
00152 sMsg="### Msg ### ";
00153
00154
00155 sMsg+="in "+rClass;
00156 sMsg+=":"+rMethod;
00157 sMsg+=" - " + rMsgOrError;
00158
00159
00160 return sMsg;
00161 }
00162
00163 void CErrorHandler::WriteError(const std::string& rClass,
00164 const std::string& rMethod,
00165 const std::string& rError)const
00166 {
00167
00168 if (!GetLog())
00169 return;
00170
00171
00172 std::string sError;
00173 sError=FormatData(rClass,
00174 rMethod,
00175 rError);
00176
00177
00178 GetLog()->WriteError(sError);
00179 }
00180
00181 void CErrorHandler::WriteStaticError(const std::string& rClass,
00182 const std::string& rMethod,
00183 const std::string& rError)
00184 {
00185
00186 if (!m_pLog)
00187 return;
00188
00189
00190 std::string sError;
00191 sError=FormatData(rClass,
00192 rMethod,
00193 rError);
00194
00195
00196 m_pLog->WriteError(sError);
00197 }
00198
00199 void CErrorHandler::WriteMessage(const std::string& rClass,
00200 const std::string& rMethod,
00201 const std::string& rMessage)const
00202 {
00203
00204 if (!GetLog())
00205 return;
00206
00207
00208 std::string sMsg;
00209 sMsg=FormatData(rClass,
00210 rMethod,
00211 rMessage,
00212 true);
00213
00214
00215 GetLog()->WriteMessage(sMsg);
00216 }
00217
00218 std::string CErrorHandler::ErrorCodeToString(DWORD dwErrorCode)
00219 {
00220 try
00221 {
00222
00223 if (!CErrorsRepository::GetInstance().GetErrorsMap().empty())
00224 {
00225
00226 CErrorsRepository::ErrorMap::const_iterator aIterator;
00227 aIterator=CErrorsRepository::GetInstance().GetErrorsMap().find(dwErrorCode);
00228
00229
00230 if (aIterator!=CErrorsRepository::GetInstance().GetErrorsMap().end())
00231 return aIterator->second;
00232 }
00233
00234
00235 LPVOID lpMsgBuf;
00236 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00237 FORMAT_MESSAGE_FROM_SYSTEM |
00238 FORMAT_MESSAGE_IGNORE_INSERTS,
00239 NULL,
00240 dwErrorCode,
00241 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00242 (LPTSTR) &lpMsgBuf,
00243 0,
00244 NULL);
00245
00246
00247 std::string sMessage;
00248
00249
00250 if (lpMsgBuf)
00251 {
00252
00253 sMessage=(char*)lpMsgBuf;
00254
00255
00256 LocalFree(lpMsgBuf);
00257 }
00258 else
00259
00260 sMessage="No error description found!";
00261
00262
00263 return sMessage;
00264 }
00265 catch (...)
00266 {
00267 return "Unknown";
00268 }
00269 }
00270
00271 void CErrorHandler::ReportError(const std::string& rMethod,
00272 const std::string& rMessage,
00273 int iErrorCode)const
00274 {
00275 if (!GetLog())
00276 return;
00277
00278 try
00279 {
00280
00281 char aTmp[11];
00282 sprintf(aTmp,"%d",iErrorCode);
00283
00284
00285 std::string sError;
00286 sError=rMessage;
00287 sError+=", and Socket error: ";
00288 sError+=aTmp;
00289 sError+=", ";
00290 sError+=ErrorCodeToString(iErrorCode);
00291
00292
00293 WriteError(m_sClassName,
00294 rMethod,
00295 sError);
00296 }
00297 ERROR_UNKNOWN("ReportError")
00298 }
00299
00300 void CErrorHandler::ReportError(const std::string& rMethod,
00301 int iErrorCode)const
00302 {
00303 if (!GetLog())
00304 return;
00305
00306 try
00307 {
00308
00309 char aTmp[11];
00310 sprintf(aTmp,"%d",iErrorCode);
00311
00312
00313 std::string sError;
00314 sError="Socket error: ";
00315 sError+=aTmp;
00316 sError+=", ";
00317 sError+=ErrorCodeToString(iErrorCode);
00318
00319
00320 WriteError(m_sClassName,
00321 rMethod,
00322 sError);
00323 }
00324 ERROR_UNKNOWN("ReportError")
00325 }
00326
00327 void CErrorHandler::ReportError(const std::string& rMethod,
00328 const std::string& rMessage)const
00329 {
00330 if (!GetLog())
00331 return;
00332
00333 try
00334 {
00335
00336 WriteError(m_sClassName,
00337 rMethod,
00338 rMessage);
00339 }
00340 ERROR_UNKNOWN("ReportError")
00341 }
00342
00343 void CErrorHandler::ReportStaticErrorOS(const std::string& rClass,
00344 const std::string& rMethod,
00345 const std::string& rMessage)
00346 {
00347 if (!m_pLog)
00348 return;
00349
00350 try
00351 {
00352
00353 DWORD dwLastError;
00354 dwLastError=GetLastError();
00355
00356
00357 std::string sMessage;
00358 sMessage=rMessage;
00359 sMessage+=", with error code: ";
00360
00361
00362 char aTmp[11];
00363 itoa(dwLastError,aTmp,10);
00364
00365
00366 sMessage+=aTmp;
00367 sMessage+=" ";
00368 sMessage+=ErrorCodeToString(dwLastError);
00369
00370
00371 WriteStaticError(rClass,
00372 rMethod,
00373 sMessage);
00374 }
00375 ERROR_UNKNOWN("ReportStaticError")
00376 }
00377
00378 void CErrorHandler::ReportErrorOS(const std::string& rMethod,
00379 const std::string& rMessage)const
00380 {
00381 if (!GetLog())
00382 return;
00383
00384 try
00385 {
00386
00387 DWORD dwLastError;
00388 dwLastError=GetLastError();
00389
00390
00391 std::string sMessage;
00392 sMessage=rMessage;
00393 sMessage+=", with error code: ";
00394
00395
00396 char aTmp[11];
00397 itoa(dwLastError,aTmp,10);
00398
00399
00400 sMessage+=aTmp;
00401 sMessage+=" ";
00402 sMessage+=ErrorCodeToString(dwLastError);
00403
00404
00405 CErrorLog* pLog;
00406 pLog=GetLog();
00407
00408
00409 WriteError(m_sClassName,
00410 rMethod,
00411 sMessage);
00412 }
00413 ERROR_UNKNOWN("ReportErrorOS")
00414 }
00415
00416 void CErrorHandler::ReportError(const std::string& rMethod,
00417 const std::string& rMessage,
00418 const std::string& rAdditionalData)const
00419 {
00420 if (!GetLog())
00421 return;
00422
00423 try
00424 {
00425
00426 std::string sNewMessage(rMessage);
00427 sNewMessage+=", Additional data: ";
00428 sNewMessage+=rAdditionalData;
00429
00430
00431 WriteError(m_sClassName,
00432 rMethod,
00433 sNewMessage);
00434 }
00435 ERROR_UNKNOWN("ReportError")
00436 }
00437
00438 void CErrorHandler::ReportError(const std::string& rMethod,
00439 const std::string& rMessage,
00440 DWORD dwAdditionalData)const
00441 {
00442 if (!GetLog())
00443 return;
00444
00445 try
00446 {
00447
00448 char aTmp[11];
00449 ltoa(dwAdditionalData,aTmp,10);
00450
00451
00452 std::string sNewMessage(rMessage);
00453 sNewMessage+=", Additional data: ";
00454 sNewMessage+=aTmp;
00455
00456
00457 WriteError(m_sClassName,
00458 rMethod,
00459 sNewMessage);
00460 }
00461 ERROR_UNKNOWN("ReportError")
00462 }
00463
00464 void CErrorHandler::SetLog(CErrorLog *pLog)
00465 {
00466
00467 m_pLog=pLog;
00468 }
00469
00470 void CErrorHandler::ReportError(const std::string& rMethod)const
00471 {
00472 if (!GetLog())
00473 return;
00474
00475 try
00476 {
00477
00478 DWORD dwLastError;
00479 dwLastError=GetLastError();
00480
00481
00482 WriteError(m_sClassName,
00483 rMethod,
00484 ErrorCodeToString(dwLastError));
00485 }
00486 ERROR_UNKNOWN("ReportError")
00487 }
00488
00489 CErrorHandler::CErrorLog* CErrorHandler::GetLog()const
00490 {
00491
00492 if (m_pLocalLog)
00493 return m_pLocalLog;
00494 else
00495 return m_pLog;
00496 }
00497
00498 void CErrorHandler::SetLocalLog(CErrorLog *pLog)
00499 {
00500 m_pLocalLog=pLog;
00501 }
00502
00503 void CErrorHandler::ReportStaticError(const std::string& rClass,
00504 const std::string& rMethod)
00505 {
00506 if (!m_pLog)
00507 return;
00508
00509 try
00510 {
00511
00512 DWORD dwLastError;
00513 dwLastError=GetLastError();
00514
00515
00516 WriteStaticError(rClass,
00517 rMethod,
00518 ErrorCodeToString(dwLastError));
00519 }
00520 ERROR_UNKNOWN("ReportStaticError")
00521 }
00522
00523 void CErrorHandler::ReportStaticError(const std::string& rClass,
00524 const std::string& rMethod,
00525 const std::string& rMessage)
00526 {
00527 if (!m_pLog)
00528 return;
00529
00530 try
00531 {
00532
00533 WriteStaticError(rClass,
00534 rMethod,
00535 rMessage);
00536 }
00537 ERROR_UNKNOWN("ReportStaticError")
00538 }
00539
00540 void CErrorHandler::ReportStaticError(const std::string& rClass,
00541 const std::string& rMethod,
00542 const std::string& rMessage,
00543 DWORD dwAdditionalData)
00544 {
00545 if (!m_pLog)
00546 return;
00547
00548 try
00549 {
00550
00551 char aTmp[11];
00552 ltoa(dwAdditionalData,aTmp,10);
00553
00554
00555 std::string sNewMessage(rMessage);
00556 sNewMessage+=", Additional data: ";
00557 sNewMessage+=aTmp;
00558
00559
00560 WriteStaticError(rClass,
00561 rMethod,
00562 sNewMessage);
00563 }
00564 ERROR_UNKNOWN("ReportStaticError")
00565 }
00566
00567 const std::string& CErrorHandler::GetClassName()const
00568 {
00569 return m_sClassName;
00570 }
00571
00572 std::string CErrorHandler::GetCurrentDateTime()
00573 {
00574 try
00575 {
00576
00577 std::string sDate;
00578
00579
00580 char cTmp[128];
00581
00582
00583 _strdate(cTmp);
00584 sDate=cTmp;
00585 sDate+=' ';
00586
00587
00588 _strtime(cTmp);
00589 sDate+=cTmp;
00590
00591
00592 return sDate;
00593 }
00594 ERROR_HANDLER_STATIC_RETURN(CErrorHandler_Class,"GetCurrentDateTime","")
00595 }
00596
00597 void CErrorHandler::RegisterError(DWORD dwErrorCode,
00598 const std::string& rDescription)
00599 {
00600 try
00601 {
00602
00603 CErrorsRepository::GetInstance().GetErrorsMap().insert(CErrorsRepository::ErrorMap::value_type(dwErrorCode,rDescription));
00604 }
00605 ERROR_HANDLER_STATIC(CErrorHandler_Class,"RegisterError")
00606 }
00607
00608 KOMODIA_NAMESPACE_END