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 CErrorHandler::CErrorLog* CErrorHandler::m_pLog=NULL;
00089
00090 #define CErrorHandler_Class "CErrorHandler"
00091
00092 CErrorHandler::CErrorHandler() : m_pLocalLog(NULL)
00093 {
00094 try
00095 {
00096
00097 SetName(CErrorHandler_Class);
00098 }
00099 ERROR_HANDLER("CErrorHandler")
00100 }
00101
00102 CErrorHandler::CErrorHandler(const CErrorHandler& rHandler) : m_pLocalLog(NULL)
00103 {
00104 try
00105 {
00106
00107 SetName(CErrorHandler_Class);
00108 }
00109 ERROR_HANDLER("CErrorHandler")
00110 }
00111
00112 CErrorHandler::~CErrorHandler()
00113 {
00114 }
00115
00116 void CErrorHandler::SetName(const std::string& rName)const
00117 {
00118
00119 m_sClassName=rName;
00120 }
00121
00122 std::string CErrorHandler::ErrorCodeToString(DWORD dwErrorCode)
00123 {
00124 try
00125 {
00126
00127 LPVOID lpMsgBuf;
00128 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00129 FORMAT_MESSAGE_FROM_SYSTEM |
00130 FORMAT_MESSAGE_IGNORE_INSERTS,
00131 NULL,
00132 dwErrorCode,
00133 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00134 (LPTSTR) &lpMsgBuf,
00135 0,
00136 NULL);
00137
00138
00139 std::string sMessage;
00140 sMessage+=(char*)lpMsgBuf;
00141
00142
00143 LocalFree(lpMsgBuf);
00144
00145
00146 return sMessage;
00147 }
00148 catch (...)
00149 {
00150 return "Unknown";
00151 }
00152 }
00153
00154 void CErrorHandler::ReportError(const std::string& rMethod,
00155 const std::string& rMessage,
00156 int iErrorCode)const
00157 {
00158 if (!GetLog())
00159 return;
00160
00161 try
00162 {
00163
00164 CErrorLog* pLog;
00165 pLog=GetLog();
00166
00167
00168 char aTmp[11];
00169 sprintf(aTmp,"%d",iErrorCode);
00170
00171
00172 std::string sError;
00173 sError=rMessage;
00174 sError+=", and Socket error: ";
00175 sError+=aTmp;
00176 sError+=", ";
00177 sError+=ErrorCodeToString(iErrorCode);
00178
00179
00180 pLog->ReportError(m_sClassName,
00181 rMethod,
00182 sError);
00183 }
00184 ERROR_UNKNOWN("ReportError")
00185 }
00186
00187 void CErrorHandler::ReportError(const std::string& rMethod,
00188 int iErrorCode)const
00189 {
00190 if (!GetLog())
00191 return;
00192
00193 try
00194 {
00195
00196 CErrorLog* pLog;
00197 pLog=GetLog();
00198
00199
00200 char aTmp[11];
00201 sprintf(aTmp,"%d",iErrorCode);
00202
00203
00204 std::string sError;
00205 sError="Socket error: ";
00206 sError+=aTmp;
00207 sError+=", ";
00208 sError+=ErrorCodeToString(iErrorCode);
00209
00210
00211 pLog->ReportError(m_sClassName,
00212 rMethod,
00213 sError);
00214 }
00215 ERROR_UNKNOWN("ReportError")
00216 }
00217
00218 void CErrorHandler::ReportError(const std::string& rMethod,
00219 const std::string& rMessage)const
00220 {
00221 if (!GetLog())
00222 return;
00223
00224 try
00225 {
00226 CErrorLog* pLog;
00227 pLog=GetLog();
00228
00229
00230 pLog->ReportError(m_sClassName,
00231 rMethod,
00232 rMessage);
00233 }
00234 ERROR_UNKNOWN("ReportError")
00235 }
00236
00237 void CErrorHandler::ReportErrorOS(const std::string& rMethod,
00238 const std::string& rMessage)const
00239 {
00240 if (!GetLog())
00241 return;
00242
00243 try
00244 {
00245
00246 DWORD dwLastError;
00247 dwLastError=GetLastError();
00248
00249
00250 std::string sMessage;
00251 sMessage=rMessage;
00252 sMessage+=", with error code: ";
00253
00254
00255 char aTmp[11];
00256 itoa(dwLastError,aTmp,10);
00257
00258
00259 sMessage+=aTmp;
00260 sMessage+=" ";
00261 sMessage+=ErrorCodeToString(dwLastError);
00262
00263
00264 CErrorLog* pLog;
00265 pLog=GetLog();
00266
00267
00268 pLog->ReportError(m_sClassName,
00269 rMethod,
00270 sMessage);
00271 }
00272 ERROR_UNKNOWN("ReportErrorOS")
00273 }
00274
00275 void CErrorHandler::ReportError(const std::string& rMethod,
00276 const std::string& rMessage,
00277 DWORD dwAdditionalData)const
00278 {
00279 if (!GetLog())
00280 return;
00281
00282 try
00283 {
00284
00285 CErrorLog* pLog;
00286 pLog=GetLog();
00287
00288
00289 char aTmp[11];
00290 ltoa(dwAdditionalData,aTmp,10);
00291
00292
00293 std::string sNewMessage(rMessage);
00294 sNewMessage+="Additional data: ";
00295 sNewMessage+=aTmp;
00296
00297
00298 pLog->ReportError(m_sClassName,
00299 rMethod,
00300 sNewMessage);
00301 }
00302 ERROR_UNKNOWN("ReportError")
00303 }
00304
00305 void CErrorHandler::SetLog(CErrorLog *pLog)
00306 {
00307
00308 m_pLog=pLog;
00309 }
00310
00311 void CErrorHandler::ReportError(const std::string& rMethod)const
00312 {
00313 if (!GetLog())
00314 return;
00315
00316 try
00317 {
00318 #ifdef WIN32
00319
00320 DWORD dwLastError;
00321 dwLastError=GetLastError();
00322
00323
00324 GetLog()->ReportError(m_sClassName,
00325 rMethod,
00326 ErrorCodeToString(dwLastError));
00327
00328 #else
00329 GetLog()->ReportCatchError(m_sClassName,
00330 rMethod,
00331 "Unknown error!");
00332 #endif
00333 }
00334 ERROR_UNKNOWN("ReportError")
00335 }
00336
00337 void CErrorHandler::WriteMessage(const std::string& rMethod,
00338 const std::string& rMessage,
00339 LogPriority aPriority)
00340 {
00341 if (!GetLog())
00342 return;
00343
00344 try
00345 {
00346
00347 GetLog()->WriteMessage(m_sClassName,
00348 rMethod,
00349 rMessage,
00350 aPriority);
00351 }
00352 ERROR_UNKNOWN("WriteMessage")
00353 }
00354
00355 void CErrorHandler::WriteMessage(const std::string& rMethod,
00356 const std::string& rMessage,
00357 DWORD dwAdditionalData,
00358 LogPriority aPriority)
00359 {
00360 if (!GetLog())
00361 return;
00362
00363 try
00364 {
00365
00366 char aTmp[11];
00367 ltoa(dwAdditionalData,aTmp,10);
00368
00369
00370 std::string sNewMessage(rMessage);
00371 sNewMessage+="Additional data: ";
00372 sNewMessage+=aTmp;
00373
00374
00375 GetLog()->WriteMessage(m_sClassName,
00376 rMethod,
00377 sNewMessage,
00378 aPriority);
00379 }
00380 ERROR_UNKNOWN("WriteMessage")
00381 }
00382
00383 void CErrorHandler::WriteStaticMessage(const std::string& rClass,
00384 const std::string& rMethod,
00385 const std::string& rMessage,
00386 LogPriority aPriority)
00387
00388 {
00389 if (!m_pLog)
00390 return;
00391
00392 try
00393 {
00394
00395 m_pLog->WriteMessage(rClass,
00396 rMethod,
00397 rMessage,
00398 aPriority);
00399 }
00400 ERROR_UNKNOWN("WriteStaticMessage")
00401 }
00402
00403 void CErrorHandler::WriteStaticMessage(const std::string& rClass,
00404 const std::string& rMethod,
00405 const std::string& rMessage,
00406 DWORD dwAdditionalData,
00407 LogPriority aPriority)
00408 {
00409 if (!m_pLog)
00410 return;
00411
00412 try
00413 {
00414
00415 char aTmp[11];
00416 ltoa(dwAdditionalData,aTmp,10);
00417
00418
00419 std::string sNewMessage(rMessage);
00420 sNewMessage+="Additional data: ";
00421 sNewMessage+=aTmp;
00422
00423
00424 m_pLog->WriteMessage(rClass,
00425 rMethod,
00426 sNewMessage,
00427 aPriority);
00428 }
00429 ERROR_UNKNOWN("WriteStaticMessage")
00430 }
00431
00432 CErrorHandler::CErrorLog* CErrorHandler::GetLog()const
00433 {
00434
00435 if (m_pLocalLog)
00436 return m_pLocalLog;
00437 else
00438 return m_pLog;
00439 }
00440
00441 void CErrorHandler::SetLocalLog(CErrorLog *pLog)
00442 {
00443 m_pLocalLog=pLog;
00444 }
00445
00446 void CErrorHandler::ReportStaticError(const std::string& rClass,
00447 const std::string& rMethod)
00448 {
00449 if (!m_pLog)
00450 return;
00451
00452 try
00453 {
00454 #ifdef WIN32
00455
00456 DWORD dwLastError;
00457 dwLastError=GetLastError();
00458
00459
00460 m_pLog->ReportError(rClass,
00461 rMethod,
00462 ErrorCodeToString(dwLastError));
00463 #else
00464 m_pLog->ReportError(rClass,
00465 rMethod,
00466 "Unknown error!");
00467 #endif
00468 }
00469 ERROR_UNKNOWN("ReportStaticError")
00470 }
00471
00472 void CErrorHandler::ReportStaticError(const std::string& rClass,
00473 const std::string& rMethod,
00474 const std::string& rMessage)
00475 {
00476 if (!m_pLog)
00477 return;
00478
00479 try
00480 {
00481
00482 m_pLog->ReportError(rClass,
00483 rMethod,
00484 rMessage);
00485 }
00486 ERROR_UNKNOWN("ReportStaticError")
00487 }
00488
00489 void CErrorHandler::ReportStaticError(const std::string& rClass,
00490 const std::string& rMethod,
00491 const std::string& rMessage,
00492 DWORD dwAdditionalData)
00493 {
00494 if (!m_pLog)
00495 return;
00496
00497 try
00498 {
00499
00500 char aTmp[11];
00501 ltoa(dwAdditionalData,aTmp,10);
00502
00503
00504 std::string sNewMessage(rMessage);
00505 sNewMessage+="Additional data: ";
00506 sNewMessage+=aTmp;
00507
00508
00509 m_pLog->ReportError(rClass,
00510 rMethod,
00511 sNewMessage);
00512 }
00513 ERROR_UNKNOWN("ReportStaticError")
00514 }
00515
00516 const std::string& CErrorHandler::GetClassName()const
00517 {
00518 return m_sClassName;
00519 }
00520
00521 std::string CErrorHandler::GetCurrentDateTime()
00522 {
00523 try
00524 {
00525
00526 std::string sDate;
00527
00528
00529 char cTmp[128];
00530
00531
00532 _strdate(cTmp);
00533 sDate=cTmp;
00534 sDate+=' ';
00535
00536
00537 _strtime(cTmp);
00538 sDate+=cTmp;
00539
00540
00541 return sDate;
00542 }
00543 ERROR_HANDLER_STATIC_RETURN(CErrorHandler_Class,"GetCurrentDateTime","")
00544 }
00545
00546 KOMODIA_NAMESPACE_END