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
00109 CErrorHandler* CErrorHandler::m_pSecondLevelLog=NULL;
00110 CErrorHandler* CErrorHandler::m_pThirdLevelLog=NULL;
00111
00112 #define CErrorHandler_Class "CErrorHandler"
00113
00114 CErrorHandler::CErrorHandler() : m_pLocalLog(NULL),
00115 m_bWriteToMain(false)
00116 {
00117 try
00118 {
00119
00120 SetName(CErrorHandler_Class);
00121 }
00122 ERROR_HANDLER("CErrorHandler")
00123 }
00124
00125 CErrorHandler::CErrorHandler(const CErrorHandler& rHandler) : m_pLocalLog(NULL)
00126 {
00127 try
00128 {
00129
00130 SetName(CErrorHandler_Class);
00131 }
00132 ERROR_HANDLER("CErrorHandler")
00133 }
00134
00135 CErrorHandler::~CErrorHandler()
00136 {
00137 }
00138
00139 void CErrorHandler::SetName(const std::string& rName)const
00140 {
00141
00142 m_sClassName=rName;
00143 }
00144
00145 std::string CErrorHandler::FormatData(const std::string& rClass,
00146 const std::string& rMethod,
00147 const std::string& rMsgOrError,
00148 bool bError)
00149 {
00150
00151 std::string sMsg;
00152
00153
00154 if (bError)
00155 sMsg="*** Error *** ";
00156 else
00157 sMsg="### Msg ### ";
00158
00159
00160 sMsg+="in "+rClass;
00161 sMsg+="::"+rMethod;
00162 sMsg+=" - " + rMsgOrError;
00163
00164
00165 return sMsg;
00166 }
00167
00168 void CErrorHandler::WriteError(const std::string& rClass,
00169 const std::string& rMethod,
00170 const std::string& rError)const
00171 {
00172
00173 if (!GetLog())
00174 return;
00175
00176
00177 std::string sError;
00178 sError=FormatData(rClass,
00179 rMethod,
00180 rError);
00181
00182
00183 GetLog()->WriteError(sError);
00184
00185
00186 if (m_bWriteToMain &&
00187 m_pLog)
00188
00189 m_pLog->WriteError(sError);
00190 }
00191
00192 void CErrorHandler::WriteStaticError(const std::string& rClass,
00193 const std::string& rMethod,
00194 const std::string& rError)
00195 {
00196
00197 if (!m_pLog)
00198 return;
00199
00200
00201 std::string sError;
00202 sError=FormatData(rClass,
00203 rMethod,
00204 rError);
00205
00206
00207 m_pLog->WriteError(sError);
00208 }
00209
00210 void CErrorHandler::WriteStaticMessage(const std::string& rClass,
00211 const std::string& rMethod,
00212 const std::string& rMessage)
00213 {
00214
00215 if (!m_pLog)
00216 return;
00217
00218
00219 std::string sMsg;
00220 sMsg=FormatData(rClass,
00221 rMethod,
00222 rMessage,
00223 false);
00224 }
00225
00226 void CErrorHandler::WriteMessage(const std::string& rClass,
00227 const std::string& rMethod,
00228 const std::string& rMessage)const
00229 {
00230
00231 if (!GetLog())
00232 return;
00233
00234
00235 std::string sMsg;
00236 sMsg=FormatData(rClass,
00237 rMethod,
00238 rMessage,
00239 false);
00240
00241
00242 GetLog()->WriteMessage(sMsg);
00243
00244
00245 if (m_bWriteToMain &&
00246 m_pLog)
00247
00248 m_pLog->WriteMessage(sMsg);
00249 }
00250
00251 std::string CErrorHandler::ErrorCodeToString(DWORD dwErrorCode)
00252 {
00253 try
00254 {
00255
00256 if (!CErrorsRepository::GetInstance().GetErrorsMap().empty())
00257 {
00258
00259 CErrorsRepository::ErrorMap::const_iterator aIterator;
00260 aIterator=CErrorsRepository::GetInstance().GetErrorsMap().find(dwErrorCode);
00261
00262
00263 if (aIterator!=CErrorsRepository::GetInstance().GetErrorsMap().end())
00264 return aIterator->second;
00265 }
00266
00267
00268 LPVOID lpMsgBuf;
00269 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00270 FORMAT_MESSAGE_FROM_SYSTEM |
00271 FORMAT_MESSAGE_IGNORE_INSERTS,
00272 NULL,
00273 dwErrorCode,
00274 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00275 (LPTSTR) &lpMsgBuf,
00276 0,
00277 NULL);
00278
00279
00280 std::string sMessage;
00281
00282
00283 if (lpMsgBuf)
00284 {
00285
00286 sMessage=(char*)lpMsgBuf;
00287
00288
00289 LocalFree(lpMsgBuf);
00290 }
00291 else
00292
00293 sMessage="No error description found!";
00294
00295
00296 return sMessage;
00297 }
00298 catch (...)
00299 {
00300 return "Unknown";
00301 }
00302 }
00303
00304 void CErrorHandler::ReportError(const std::string& rMethod,
00305 const std::string& rMessage,
00306 int iErrorCode)const
00307 {
00308 if (!GetLog())
00309 return;
00310
00311 try
00312 {
00313
00314 char aTmp[11];
00315 sprintf(aTmp,"%d",iErrorCode);
00316
00317
00318 std::string sError;
00319 sError=rMessage;
00320 sError+=", and Socket error: ";
00321 sError+=aTmp;
00322 sError+=", ";
00323 sError+=ErrorCodeToString(iErrorCode);
00324
00325
00326 WriteError(m_sClassName,
00327 rMethod,
00328 sError);
00329 }
00330 ERROR_UNKNOWN("ReportError")
00331 }
00332
00333 void CErrorHandler::ReportError(const std::string& rMethod,
00334 int iErrorCode)const
00335 {
00336 if (!GetLog())
00337 return;
00338
00339 try
00340 {
00341
00342 char aTmp[11];
00343 sprintf(aTmp,"%d",iErrorCode);
00344
00345
00346 std::string sError;
00347 sError="Socket error: ";
00348 sError+=aTmp;
00349 sError+=", ";
00350 sError+=ErrorCodeToString(iErrorCode);
00351
00352
00353 WriteError(m_sClassName,
00354 rMethod,
00355 sError);
00356 }
00357 ERROR_UNKNOWN("ReportError")
00358 }
00359
00360 void CErrorHandler::WriteMessage(const std::string& rMethod,
00361 const std::string& rMessage)const
00362 {
00363 if (!GetLog())
00364 return;
00365
00366 try
00367 {
00368
00369 WriteMessage(m_sClassName,
00370 rMethod,
00371 rMessage);
00372 }
00373 ERROR_UNKNOWN("WriteMessage")
00374 }
00375
00376 void CErrorHandler::ReportError(const std::string& rMethod,
00377 const std::string& rMessage)const
00378 {
00379 if (!GetLog())
00380 return;
00381
00382 try
00383 {
00384
00385 WriteError(m_sClassName,
00386 rMethod,
00387 rMessage);
00388 }
00389 ERROR_UNKNOWN("ReportError")
00390 }
00391
00392 void CErrorHandler::ReportStaticErrorOS(const std::string& rClass,
00393 const std::string& rMethod,
00394 const std::string& rMessage)
00395 {
00396 if (!m_pLog)
00397 return;
00398
00399 try
00400 {
00401
00402 DWORD dwLastError;
00403 dwLastError=GetLastError();
00404
00405
00406 std::string sMessage;
00407 sMessage=rMessage;
00408 sMessage+=", with error code: ";
00409
00410
00411 char aTmp[11];
00412 itoa(dwLastError,aTmp,10);
00413
00414
00415 sMessage+=aTmp;
00416 sMessage+=" ";
00417 sMessage+=ErrorCodeToString(dwLastError);
00418
00419
00420 WriteStaticError(rClass,
00421 rMethod,
00422 sMessage);
00423 }
00424 ERROR_UNKNOWN("ReportStaticError")
00425 }
00426
00427 void CErrorHandler::ReportErrorOS(const std::string& rMethod,
00428 const std::string& rMessage)const
00429 {
00430 if (!GetLog())
00431 return;
00432
00433 try
00434 {
00435
00436 DWORD dwLastError;
00437 dwLastError=GetLastError();
00438
00439
00440 std::string sMessage;
00441 sMessage=rMessage;
00442 sMessage+=", with error code: ";
00443
00444
00445 char aTmp[11];
00446 itoa(dwLastError,aTmp,10);
00447
00448
00449 sMessage+=aTmp;
00450 sMessage+=" ";
00451 sMessage+=ErrorCodeToString(dwLastError);
00452
00453
00454 WriteError(m_sClassName,
00455 rMethod,
00456 sMessage);
00457 }
00458 ERROR_UNKNOWN("ReportErrorOS")
00459 }
00460
00461 void CErrorHandler::ReportError(const std::string& rMethod,
00462 const std::string& rMessage,
00463 const std::string& rAdditionalData)const
00464 {
00465 if (!GetLog())
00466 return;
00467
00468 try
00469 {
00470
00471 std::string sNewMessage(rMessage);
00472 sNewMessage+=", Additional data: ";
00473 sNewMessage+=rAdditionalData;
00474
00475
00476 WriteError(m_sClassName,
00477 rMethod,
00478 sNewMessage);
00479 }
00480 ERROR_UNKNOWN("ReportError")
00481 }
00482
00483 void CErrorHandler::ReportError(const std::string& rMethod,
00484 const std::string& rMessage,
00485 DWORD dwAdditionalData)const
00486 {
00487 if (!GetLog())
00488 return;
00489
00490 try
00491 {
00492
00493 char aTmp[11];
00494 ltoa(dwAdditionalData,aTmp,10);
00495
00496
00497 std::string sNewMessage(rMessage);
00498 sNewMessage+=", Additional data: ";
00499 sNewMessage+=aTmp;
00500
00501
00502 WriteError(m_sClassName,
00503 rMethod,
00504 sNewMessage);
00505 }
00506 ERROR_UNKNOWN("ReportError")
00507 }
00508
00509 void CErrorHandler::SetLog(CErrorLog *pLog)
00510 {
00511
00512 m_pLog=pLog;
00513 }
00514
00515 void CErrorHandler::ReportError(const std::string& rMethod)const
00516 {
00517 if (!GetLog())
00518 return;
00519
00520 try
00521 {
00522
00523 DWORD dwLastError;
00524 dwLastError=GetLastError();
00525
00526
00527 WriteError(m_sClassName,
00528 rMethod,
00529 ErrorCodeToString(dwLastError));
00530 }
00531 ERROR_UNKNOWN("ReportError")
00532 }
00533
00534 CErrorHandler::CErrorLog* CErrorHandler::GetLog()const
00535 {
00536
00537 if (m_pLocalLog)
00538 return m_pLocalLog;
00539 else
00540 return m_pLog;
00541 }
00542
00543 void CErrorHandler::SetLocalLog(CErrorLog *pLog,
00544 bool bWriteToMain)
00545 {
00546
00547 m_pLocalLog=pLog;
00548
00549
00550 m_bWriteToMain=bWriteToMain;
00551 }
00552
00553 void CErrorHandler::ReportStaticError(const std::string& rClass,
00554 const std::string& rMethod)
00555 {
00556 if (!m_pLog)
00557 return;
00558
00559 try
00560 {
00561
00562 DWORD dwLastError;
00563 dwLastError=GetLastError();
00564
00565
00566 WriteStaticError(rClass,
00567 rMethod,
00568 ErrorCodeToString(dwLastError));
00569 }
00570 ERROR_UNKNOWN("ReportStaticError")
00571 }
00572
00573 void CErrorHandler::ReportStaticError(const std::string& rClass,
00574 const std::string& rMethod,
00575 const std::string& rMessage)
00576 {
00577 if (!m_pLog)
00578 return;
00579
00580 try
00581 {
00582
00583 WriteStaticError(rClass,
00584 rMethod,
00585 rMessage);
00586 }
00587 ERROR_UNKNOWN("ReportStaticError")
00588 }
00589
00590 void CErrorHandler::ReportStaticError(const std::string& rClass,
00591 const std::string& rMethod,
00592 const std::string& rMessage,
00593 DWORD dwAdditionalData)
00594 {
00595 if (!m_pLog)
00596 return;
00597
00598 try
00599 {
00600
00601 char aTmp[11];
00602 ltoa(dwAdditionalData,aTmp,10);
00603
00604
00605 std::string sNewMessage(rMessage);
00606 sNewMessage+=", Additional data: ";
00607 sNewMessage+=aTmp;
00608
00609
00610 WriteStaticError(rClass,
00611 rMethod,
00612 sNewMessage);
00613 }
00614 ERROR_UNKNOWN("ReportStaticError")
00615 }
00616
00617 const std::string& CErrorHandler::GetClassName()const
00618 {
00619 return m_sClassName;
00620 }
00621
00622 std::string CErrorHandler::GetCurrentDateTime()
00623 {
00624 try
00625 {
00626
00627 std::string sDate;
00628
00629
00630 char cTmp[128];
00631
00632
00633 _strdate(cTmp);
00634 sDate=cTmp;
00635 sDate+=' ';
00636
00637
00638 _strtime(cTmp);
00639 sDate+=cTmp;
00640
00641
00642 return sDate;
00643 }
00644 ERROR_HANDLER_STATIC_RETURN(CErrorHandler_Class,"GetCurrentDateTime","")
00645 }
00646
00647 void CErrorHandler::RegisterError(DWORD dwErrorCode,
00648 const std::string& rDescription)
00649 {
00650 try
00651 {
00652
00653 CErrorsRepository::GetInstance().GetErrorsMap().insert(CErrorsRepository::ErrorMap::value_type(dwErrorCode,rDescription));
00654 }
00655 ERROR_HANDLER_STATIC(CErrorHandler_Class,"RegisterError")
00656 }
00657
00658 CErrorHandler* CErrorHandler::GetSecondaryLog()
00659 {
00660
00661 if (!m_pSecondLevelLog)
00662
00663 m_pSecondLevelLog=new CErrorHandler;
00664
00665
00666 return m_pSecondLevelLog;
00667 }
00668
00669 CErrorHandler* CErrorHandler::GetThirdLog()
00670 {
00671
00672 if (!m_pThirdLevelLog)
00673
00674 m_pThirdLevelLog=new CErrorHandler;
00675
00676
00677 return m_pThirdLevelLog;
00678 }
00679
00680 void CErrorHandler::ClearStaticLogs()
00681 {
00682
00683 delete m_pSecondLevelLog;
00684 m_pSecondLevelLog=NULL;
00685
00686 delete m_pThirdLevelLog;
00687 m_pThirdLevelLog=NULL;
00688 }
00689
00690
00691 KOMODIA_NAMESPACE_END