適用于使用COM技術的日志記錄
這個模塊的宏,我幾乎是完全抄襲已存在的一個作品中的代碼, 只是在記錄日志時是調用以前發布的日記模塊功能.
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
}


2

3

4

5

6

7

8

9

10

11

12


13

14

15

16

17

18

19

20

21

22

23

24

25


26

27

28

29

30

31

32

33

34

35

36

37

38

39



40

41

42

43

44



45

46

47

48

49

50



51

52

53

54

55



56

57

58

59

60

61



62

63

64

65

66



67

68

69

70

71

72



73

74

75

76

77



78

79

80

81

82

83



84

85

86

87

88



89

90

91

92

93

94



95

96

97

98

99



100

101

102

103

104

105



106

107



108

109

110

111

112



113

114

115

116

117

118



119

120



121

122

123

124

125



126

127

128

129

130

131

132

133



134

135

136



137

138

139

140

141



142

143

144

145

146

147

148



149

150

151



152

153

154

155

156



157

158

159

160

161

162

163

164



165

166

167

168

169



170

171

172

173

174

175

176



177

178

179

180

181



182

183

184

185

186

187

188



189

190

191



192

193

194

195

196

197

198



199

200

201

202

203



204

205

206



207

208

209



210

211

212

213

214

215

216

217

218



219

220



221

222

223

224

225

226

227

228



229

230



231

232

233

234

235

236

237

238



239

240



241

242

243

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
}


2

3

4

5

6

7

8

9

10

11

12

13

14



15

16

17

18

19

20

21

22

23

24

25

26



27

28

29

30

31

32

33

34

35

36

37

38



39

40

41

42

43

44

45

46



47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63



64

65

66

67



68

69

70

71

72

73

74

75

76

posted on 2010-06-11 16:13 Robertxiao 閱讀(375) 評論(0) 編輯 收藏 引用 所屬分類: RPC/COM/ATL散談