|
这个模块的宏,我几乎是完全抄袭已存在的一个作品中的代码, 只是在记录日志时是调用以前发布的日记模块功能.
1 /**//** 2 * @file: Log_for_com.h 3 * 4 * @brief: provide write log message function, macro, and assistant class. 5 * 6 * @author: Robert xiao 7 * 8 */ 9 10 #pragma once 11 12 /**/////////////////////////////////////////////////////////////////////////// 13 // FUNCTION 14 // 15 void PrintSkipNote(HRESULT hRes, LPCTSTR szFunction); 16 17 BOOL CheckHResFn(HRESULT hRes,LPCTSTR szFunction,UINT uidErrorMsg,LPCTSTR szFile,int iLine); 18 19 BOOL WarnHResFn(HRESULT hRes,LPCTSTR szFunction,UINT uidErrorMsg,LPCTSTR szFile,int iLine); 20 21 HRESULT DialogOnWin32Error(LPCTSTR szFile,int iLine, LPCTSTR szFunction); 22 23 HRESULT WarnOnWin32Error(LPCTSTR szFile,int iLine, LPCTSTR szFunction); 24 25 /**/////////////////////////////////////////////////////////////////////////// 26 // MACROS 27 // 28 #define HRES_TO_BOOL(hr) \ 29 (SUCCEEDED(hr)? TRUE: FALSE) 30 31 #define RETURN_HRES_TO_INTEGER(hr, su, fa)\ 32 return (SUCCEEDED(hr)? su: fa) 33 34 #define RETURN_HRES_TO_SPECVAL(hr, su, fa)\ 35 return (SUCCEEDED(hr)? su: fa) 36 37 #define EC_H(fnx) \ 38 if (SUCCEEDED(hRes)) \ 39  { \ 40 hRes = (fnx); \ 41 CheckHResFn(hRes,_T(#fnx),NULL,__TFILE__,__LINE__); \ 42 } \ 43 else \ 44  { \ 45 PrintSkipNote(hRes,_T(#fnx));\ 46 } 47 48 #define WC_H(fnx) \ 49 if (SUCCEEDED(hRes)) \ 50  { \ 51 hRes = (fnx); \ 52 WarnHResFn(hRes,_T(#fnx),NULL,__TFILE__,__LINE__); \ 53 } \ 54 else \ 55  { \ 56 PrintSkipNote(hRes,_T(#fnx));\ 57 } 58 59 #define EC_H_MSG(fnx,uidErrorMsg) \ 60 if (SUCCEEDED(hRes)) \ 61  { \ 62 hRes = (fnx); \ 63 CheckHResFn(hRes,_T(#fnx),uidErrorMsg,__TFILE__,__LINE__); \ 64 } \ 65 else \ 66  { \ 67 PrintSkipNote(hRes,_T(#fnx));\ 68 } 69 70 #define WC_H_MSG(fnx,uidErrorMsg) \ 71 if (SUCCEEDED(hRes)) \ 72  { \ 73 hRes = (fnx); \ 74 WarnHResFn(hRes,_T(#fnx),uidErrorMsg,__TFILE__,__LINE__); \ 75 } \ 76 else \ 77  { \ 78 PrintSkipNote(hRes,_T(#fnx));\ 79 } 80 81 #define EC_W32(fnx) \ 82 if (SUCCEEDED(hRes)) \ 83  { \ 84 hRes = (fnx); \ 85 CheckHResFn(HRESULT_FROM_WIN32(hRes),_T(#fnx),NULL,__TFILE__,__LINE__); \ 86 } \ 87 else \ 88  { \ 89 PrintSkipNote(hRes,_T(#fnx));\ 90 } 91 92 #define WC_W32(fnx) \ 93 if (SUCCEEDED(hRes)) \ 94  { \ 95 hRes = (fnx); \ 96 WarnHResFn(HRESULT_FROM_WIN32(hRes),_T(#fnx),NULL,__TFILE__,__LINE__); \ 97 } \ 98 else \ 99  { \ 100 PrintSkipNote(hRes,_T(#fnx));\ 101 } 102 103 #define EC_B(fnx) \ 104 if (SUCCEEDED(hRes)) \ 105  { \ 106 if (!(fnx)) \ 107  { \ 108 hRes = DialogOnWin32Error(__TFILE__,__LINE__,_T(#fnx)); \ 109 } \ 110 } \ 111 else \ 112  { \ 113 PrintSkipNote(hRes,_T(#fnx));\ 114 } 115 116 #define WC_B(fnx) \ 117 if (SUCCEEDED(hRes)) \ 118  { \ 119 if (!(fnx)) \ 120  { \ 121 hRes = WarnOnWin32Error(__TFILE__,__LINE__,_T(#fnx));\ 122 } \ 123 } \ 124 else \ 125  { \ 126 PrintSkipNote(hRes,_T(#fnx));\ 127 } 128 129 130 #define EC_D(_ret,fnx) \ 131 \ 132 if (SUCCEEDED(hRes)) \ 133  { \ 134 _ret = (fnx); \ 135 if (!_ret) \ 136  { \ 137 hRes = DialogOnWin32Error(__TFILE__,__LINE__,_T(#fnx)); \ 138 } \ 139 } \ 140 else \ 141  { \ 142 PrintSkipNote(hRes,_T(#fnx));\ 143 _ret = NULL; \ 144 } 145 146 #define WC_D(_ret,fnx) \ 147 if (SUCCEEDED(hRes)) \ 148  { \ 149 _ret = (fnx); \ 150 if (!_ret) \ 151  { \ 152 hRes = WarnOnWin32Error(__TFILE__,__LINE__,_T(#fnx));\ 153 } \ 154 } \ 155 else \ 156  { \ 157 PrintSkipNote(hRes,_T(#fnx));\ 158 _ret = NULL; \ 159 } 160 161 #define EC_H_GETPROPS(fnx) \ 162 \ 163 if (SUCCEEDED(hRes)) \ 164  { \ 165 hRes = (fnx); \ 166 if (MAPI_W_ERRORS_RETURNED != hRes) CheckHResFn(hRes,_T(#fnx),NULL,__TFILE__,__LINE__); \ 167 } \ 168 else \ 169  { \ 170 PrintSkipNote(hRes,_T(#fnx));\ 171 } 172 173 #define WC_H_GETPROPS(fnx) \ 174 \ 175 if (SUCCEEDED(hRes)) \ 176  { \ 177 hRes = (fnx); \ 178 if (MAPI_W_ERRORS_RETURNED != hRes) WarnHResFn(hRes,_T(#fnx),NULL,__TFILE__,__LINE__); \ 179 } \ 180 else \ 181  { \ 182 PrintSkipNote(hRes,_T(#fnx));\ 183 } 184 185 #define EC_H_CANCEL(fnx) \ 186 \ 187 if (SUCCEEDED(hRes)) \ 188  { \ 189 hRes = (fnx); \ 190 if (MAPI_E_USER_CANCEL == hRes || MAPI_E_CANCEL == hRes) \ 191  { \ 192 WarnHResFn(hRes,_T(#fnx),IDS_USERCANCELLED,__TFILE__,__LINE__); \ 193 hRes = S_OK; \ 194 } \ 195 else CheckHResFn(hRes,_T(#fnx),NULL,__TFILE__,__LINE__); \ 196 } \ 197 else \ 198  { \ 199 PrintSkipNote(hRes,_T(#fnx));\ 200 } 201 202 #define EC_D_DIALOG(fnx) \ 203  { \ 204 iDlgRet = (fnx); \ 205 if (IDCANCEL == iDlgRet) \ 206  { \ 207 DWORD err = CommDlgExtendedError(); \ 208 if (err) \ 209  { \ 210 ErrDialog(__TFILE__,__LINE__,IDS_EDCOMMONDLG,_T(#fnx),err); \ 211 hRes = MAPI_E_CALL_FAILED; \ 212 } \ 213 else hRes = S_OK; \ 214 } \ 215 } 216 217 #define EC_PROBLEMARRAY(problemarray) \ 218  { \ 219 if (problemarray) \ 220  { \ 221 CString szProbArray = ProblemArrayToString((problemarray)); \ 222 ErrDialog(__TFILE__,__LINE__,IDS_EDPROBLEMARRAY,(LPCTSTR) szProbArray); \ 223 DebugPrint(DBGGeneric,_T("Problem array:\n%s\n"),(LPCTSTR) szProbArray); \ 224 } \ 225 } 226 227 #define EC_MAPIERR(__ulflags,__lperr) \ 228  { \ 229 if (__lperr) \ 230  { \ 231 CString szErr = MAPIErrToString((__ulflags),(__lperr)); \ 232 ErrDialog(__TFILE__,__LINE__,IDS_EDMAPIERROR,(LPCTSTR) szErr); \ 233 DebugPrint(DBGGeneric,_T("LPMAPIERROR:\n%s\n"),(LPCTSTR) szErr); \ 234 } \ 235 } 236 237 #define EC_TNEFERR(problemarray) \ 238  { \ 239 if (problemarray) \ 240  { \ 241 CString szProbArray = TnefProblemArrayToString((problemarray)); \ 242 ErrDialog(__TFILE__,__LINE__,IDS_EDTNEFPROBLEMARRAY,(LPCTSTR) szProbArray); \ 243 DebugPrint(DBGGeneric,_T("TNEF Problem array:\n%s\n"),(LPCTSTR) szProbArray); \ 244 } \ 245 }
1 /**//** 2 * @file: Log_for_com.cpp 3 * 4 * @brief: provide write log message function, macro, and assistant class. 5 * 6 * @author: Robert xiao 7 * 8 */ 9 10 #include "stdafx.h" 11 #include "Log_for_com.h" 12 13 BOOL CheckHResFn(HRESULT hRes,LPCTSTR szFunction,UINT uidErrorMsg,LPCTSTR szFile,int iLine) 14  { 15 if (S_OK == hRes) return true; 16 Log_entity::GetInstance()->Log(Log_entity::LL_ERROR, 17 szFile, 18 iLine, 19 _T("%s failed, error:%d"), 20 szFunction, 21 GetLastError()); 22 return false; 23 } 24 25 BOOL WarnHResFn(HRESULT hRes,LPCTSTR szFunction,UINT uidErrorMsg,LPCTSTR szFile,int iLine) 26  { 27 if (S_OK == hRes) return true; 28 Log_entity::GetInstance()->Log(Log_entity::LL_WARNING, 29 szFile, 30 iLine, 31 _T("%s failed, error:%d"), 32 szFunction, 33 GetLastError()); 34 return SUCCEEDED(hRes); 35 } 36 37 void PrintSkipNote(HRESULT hRes,LPCTSTR szFunc) 38  { 39 Log_entity::GetInstance()->Log(Log_entity::LL_INFO, 40 _T("Skinnping %s because hRes = 0x%8x.\n"), 41 szFunc, 42 hRes); 43 } 44 45 HRESULT DialogOnWin32Error(LPCTSTR szFile,int iLine, LPCTSTR szFunction) 46  { 47 DWORD dwErr = GetLastError(); 48 if (0 == dwErr) return S_OK; 49 50 HRESULT hRes = HRESULT_FROM_WIN32(dwErr); 51 if (S_OK == hRes) return S_OK; 52 Log_entity::GetInstance()->Log(Log_entity::LL_ERROR, 53 szFile, 54 iLine, 55 _T("%s failed, error:%d"), 56 szFunction, 57 GetLastError()); 58 59 return hRes; 60 } 61 62 HRESULT WarnOnWin32Error(LPCTSTR szFile,int iLine, LPCTSTR szFunction) 63  { 64 DWORD dwErr = GetLastError(); 65 HRESULT hRes = HRESULT_FROM_WIN32(dwErr); 66 67 if (S_OK != hRes) { 68 Log_entity::GetInstance()->Log(Log_entity::LL_WARNING, 69 szFile, 70 iLine, 71 _T("%s failed, error:%d"), 72 szFunction, 73 GetLastError()); 74 } 75 return hRes; 76 }
|