glxhyt
C++博客
::
首页
::
新随笔
::
联系
::
聚合
::
管理
::
15 随笔 :: 0 文章 :: 4 评论 :: 0 Trackbacks
<
2010年8月
>
日
一
二
三
四
五
六
25
26
27
28
29
30
31
1
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
1
2
3
4
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
给我留言
查看公开留言
查看私人留言
随笔档案
2013年5月 (2)
2012年5月 (1)
2012年2月 (1)
2011年11月 (5)
2010年10月 (1)
2010年9月 (1)
2010年8月 (4)
搜索
最新评论
1. re: 【转】C/C++开源项目中的100个Bugs | WHO1753
.
--zgpxgame
2. re: 自己工作中犯的几个小问题
可以把你遇到的问题描述的详细点吗?
--钟谢伟
3. re: 自己工作中犯的几个小问题
评论内容较长,点击标题查看
--郭龙
4. re: 自己工作中犯的几个小问题
评论内容较长,点击标题查看
--钟谢伟
阅读排行榜
1. [转]二维数组和二级指针 (5580)
2. 一个简单的用c++写的log(1659)
3. linux 下的一个程序代码统计的程序(类图,时序图,测试例子,代码,心得)(1527)
4. 以前的一些mfc的笔记(关于孙鑫的那本书)(1119)
5. 【转】字符串查找算法总结(696)
评论排行榜
1. 自己工作中犯的几个小问题(3)
2. 【转】C/C++开源项目中的100个Bugs | WHO1753(1)
3. 【转】字符串查找算法总结(0)
4. 著名的Josephus问题(0)
5. 一个简单的用c++写的log(0)
一个简单的用c++写的log
1
#ifndef _LOGING_
2
#define
_LOGING_
3
#include
<
iostream
>
4
#include
<
string
>
5
#include
<
fstream
>
6
#include
<
time.h
>
7
#include
<
sys
/
time.h
>
8
#include
<
cstdlib
>
9
#include
<
sstream
>
10
#include
<
cstdarg
>
11
12
using
namespace
std;
13
14
typedef
enum
en_logleval
15
{
16
fatal
=
0
,
17
error,
18
warning,
19
debug
20
21
}
logleval_en;
22
23
typedef
struct
tag_LogManangeInfo
24
{
25
int
logleval;
26
int
logoutype;
27
string
logfilepath;
28
29
}
LogManagementInfo_st;
30
31
string
strFileName;
32
string
strFileMainPath;
33
34
struct
timeval t_time;
35
unsigned
int
__NR_gettid;
36
enum
en_logleval leval_t;
37
struct
tm
*
ptm
=
NULL;
38
39
LogManagementInfo_st LogManagementInfo
{
-
1
,
-
1
,
""
}
;
40
41
string
GetFileMainPath();
42
43
void
LogPrint(
int
level,
char
*
info,
);
44
45
void
LogInit(
struct
timeval t);
46
47
void
OutPrint(
int
leval1,
48
struct
timeval t1, unsigned
int
id1,
49
string
fuction1,
int
line1,
string
file1,
char
*
info1);
50
51
void
LogReture(
struct
timeval t1, unsigned
int
id1,
52
string
fuction1,
int
line1,
string
file1,
53
char
*
info1);
54
55
void
LogCheckArg(
bool
bl,
struct
timeval t1, unsigned
int
id1,
56
string
fuction1,
int
line1,
string
file1,
57
char
*
info1);
58
59
#define
SWITCHLEVEL(level,out) switch(level) \
60
{
case
0
:\
61
out
<<
"
Fatal log:
"
; \
62
break
; \
63
case
1
:\
64
out
<<
"
Error log:
"
;\
65
break
;\
66
case
2
:\
67
out
<<
"
warning log:
"
;\
68
break
;\
69
case
3
:\
70
out
<<
"
debug log:
"
;\
71
break
;\
72
default
:\
73
out
<<
"
Info
"
<<
"
[
"
<<
level
-
4
<<
"
]
"
<<
"
log:
"
;\
74
break
;\
75
}
\
76
77
#define
WRITELOG(out) ptm = localtime(&t1.tv_sec);\
78
out
<<
ptm
->
tm_year
+
1900
<<
"
-
"
<<
ptm
->
tm_mon
+
1
<<
"
-
"
<<
ptm
->
tm_mday;\
79
out
<<
"
-
"
<<
ptm
->
tm_hour
<<
"
:
"
<<
ptm
->
tm_min
<<
"
:
"
<<
ptm
->
tm_sec
<<
"
.
"
;\
80
out
<<
t1.tv_usec; \
81
file1
=
strFileMainPath
+
file1.substr( file1.find(
'
/
'
) );\
82
out
<<
"
"
<<
id1;\
83
out
<<
"
"
<<
fuction1;\
84
out
<<
"
"
<<
file1;\
85
out
<<
"
:
"
<<
line1;\
86
out
<<
"
"
<<
"
Info:
"
<<
info1
<<
endl;\
87
88
#define
LOGINIT struct timeval t;\
89
gettimeofday(
&
t, NULL); \
90
LogInit(t)
91
92
#define
LOGFATAL(info) gettimeofday(&t_time, NULL); \
93
syscall(__NR_gettid);\
94
leval_t
=
fatal;\
95
OutPrint(leval_t, t, __NR_gettid, __FUNCTION__ ,__LINE__, __FILE__, info)
96
97
#define
LOGERROR(info) gettimeofday(&t_time, NULL); \
98
syscall(__NR_gettid);\
99
leval_t
=
error;\
100
OutPrint(leval_t, t, __NR_gettid, __FUNCTION__, __LINE__, __FILE__, info)
101
102
#define
LOGWARNING(info) gettimeofday(&t_time, NULL); \
103
syscall(__NR_gettid);\
104
leval_t
=
warning;\
105
OutPrint(leval_t, t, __NR_gettid, __FUNCTION__, __LINE__, __FILE__, info)
106
107
#define
LOGDEBUG(info) gettimeofday(&t_time, NULL); \
108
syscall(__NR_gettid);\
109
leval_t
=
debug;\
110
OutPrint(leval_t, t, __NR_gettid, __FUNCTION__, __LINE__, __FILE__, info)
111
112
#define
LOGINFO(level, info) gettimeofday(&t_time, NULL); \
113
syscall(__NR_gettid);\
114
OutPrint(level, t, __NR_gettid, __FUNCTION__, __LINE__, __FILE__, info)
115
116
117
#define
RETURN_NULL gettimeofday(&t_time, NULL); \
118
syscall(__NR_gettid);\
119
LogReture( t_time, __NR_gettid, __FUNCTION__, __LINE__, __FILE__,
"
return 0
"
);\
120
LogManagementInfo.logleval
=
debug;\
121
return
0
;\
122
123
#define
RETURN(val) gettimeofday(&t_time, NULL); \
124
syscall(__NR_gettid);\
125
LogReture( t_time, __NR_gettid, __FUNCTION__, __LINE__, __FILE__,
"
return val
"
);\
126
LogManagementInfo.logleval
=
debug;\
127
return
(val);\
128
129
#define
LOGCHECKARG(bl, info) gettimeofday(&t_time, NULL); \
130
syscall(__NR_gettid);\
131
LogCheckArg(bl, t_time, __NR_gettid, __FUNCTION__, __LINE__, __FILE__, info)
132
133
134
#endif
135
1
2
//
============================================================================
3
//
Name : loging.cpp
4
//
Author :
5
//
Version :
6
//
Copyright : Your copyright notice
7
//
Description : Hello World in C++, Ansi-style
8
//
============================================================================
9
10
#include
"
loging.h
"
11
#include
<
cstdio
>
12
#include
<
string
>
13
using
namespace
std;
14
15
string
GetFileMainPath()
16
{
17
char
buffer[
256
];
18
getcwd(buffer,
256
);
19
string
strTemp
=
buffer;
20
return
strTemp;
21
}
22
23
void
LogInit(
struct
timeval t)
24
{
25
ifstream
out
(
"
/home/ganhm/logging/loging.conf
"
);
26
while
(NULL
!=
out
)
27
{
28
string
str;
29
string
begsubstr;
30
string
endsubstr;
31
getline(
out
, str,
'
\n
'
);
32
unsigned
int
logbegpos
=
str.find_first_of(
'
=
'
,
0
);
33
unsigned
int
logendpos
=
str.find_first_of(
'
\0
'
, logbegpos);
34
35
begsubstr
=
str.substr(
0
, logbegpos);
36
if
(begsubstr
==
"
loglevel
"
)
37
{
38
endsubstr
=
str.substr(logbegpos
+
1
, logendpos
-
logbegpos);
39
if
(endsubstr
==
"
fatal
"
)
40
{
41
LogManagementInfo.logleval
=
fatal;
42
}
43
else
if
(endsubstr
==
"
error
"
)
44
{
45
LogManagementInfo.logleval
=
error;
46
}
47
else
if
(endsubstr
==
"
warning
"
)
48
{
49
LogManagementInfo.logleval
=
warning;
50
}
51
else
if
(endsubstr
==
"
debug
"
)
52
{
53
LogManagementInfo.logleval
=
debug;
54
}
55
else
if
((endsubstr.substr(
0
,
4
))
==
"
info
"
)
56
{
57
cout
<<
"
endlsubstr
"
<<
endsubstr
<<
endl;
58
string
strInfoNum
=
endsubstr.substr(endsubstr.find(
'
[
'
)
+
1
,
1
);
59
cout
<<
"
strInfoNum
"
<<
strInfoNum
<<
endl;
60
int
iInfoNum;
61
stringstream stream;
62
stream
<<
strInfoNum;
63
stream
>>
iInfoNum;
64
cout
<<
"
iInfoNum
"
<<
iInfoNum
<<
endl;
65
LogManagementInfo.logleval
=
iInfoNum
+
4
;
66
cout
<<
"
LogManagementInfo.logleval
"
<<
LogManagementInfo.logleval
<<
endl;
67
}
68
}
69
else
if
(begsubstr
==
"
logouttype
"
)
70
{
71
endsubstr
=
str.substr(logbegpos
+
1
, logendpos
-
logbegpos);
72
stringstream stream;
73
int
ilogouttype;
74
stream
<<
endsubstr;
75
stream
>>
ilogouttype;
76
cout
<<
"
ilogouttype
"
<<
ilogouttype
<<
endl;
77
LogManagementInfo.logoutype
=
ilogouttype;
78
}
79
else
if
(begsubstr
==
"
logfilepath
"
)
80
{
81
endsubstr
=
str.substr(logbegpos
+
1
, logendpos
-
logbegpos);
82
LogManagementInfo.logfilepath
=
endsubstr;
83
}
84
85
}
86
87
if
(LogManagementInfo.logleval
==
-
1
)
88
{
89
LogManagementInfo.logleval
=
2
;
90
}
91
cout
<<
"
leval
"
<<
LogManagementInfo.logleval
<<
endl;
92
if
(LogManagementInfo.logoutype
==
-
1
)
93
{
94
LogManagementInfo.logoutype
=
0
;
95
}
96
97
cout
<<
"
type
"
<<
LogManagementInfo.logoutype
<<
endl;
98
strFileMainPath
=
GetFileMainPath();
99
cout
<<
"
FeilManiPath
"
<<
strFileMainPath
<<
endl;
100
101
string
strFileNameAfer
=
strFileMainPath.substr(strFileMainPath.rfind(
'
/
'
)
+
1
)
+
"
.log
"
;
102
if
(LogManagementInfo.logfilepath
==
""
)
103
{
104
strFileName
=
strFileNameAfer;
105
}
106
else
107
{
108
string
strManagementINfoTemp
=
(LogManagementInfo.logfilepath).substr(
0
,(LogManagementInfo.logfilepath).rfind(
'
/
'
)
+
1
);
109
strFileName
=
strManagementINfoTemp
+
strFileNameAfer;
110
}
111
cout
<<
"
GETFILENAME
"
<<
strFileName
<<
endl;
112
}
113
114
void
OutPrint(
int
leval1,
115
struct
timeval t1, unsigned
int
id1,
116
string
fuction1,
int
line1,
string
file1,
char
*
info1)
117
{
118
if
(LogManagementInfo.logleval
>=
leval1)
119
{
120
if
(LogManagementInfo.logoutype
==
0
)
121
{
122
SWITCHLEVEL(leval1, cout);
123
WRITELOG(cout);
124
}
125
else
if
(LogManagementInfo.logoutype
==
1
)
126
{
127
ofstream fout(strFileName.c_str(), ios::ate
|
ios::app );
128
SWITCHLEVEL(leval1, fout);
129
WRITELOG(fout);
130
}
131
}
132
}
133
134
void
LogReture(
struct
timeval t1, unsigned
int
id1,
135
string
fuction1,
int
line1,
string
file1,
136
char
*
info1)
137
{
138
if
(LogManagementInfo.logoutype
==
0
)
139
{
140
cout
<<
"
RETURN log:
"
;
141
WRITELOG(cout);
142
}
143
else
if
(LogManagementInfo.logoutype
==
1
)
144
{
145
ofstream fout((strFileName.c_str()), ios::ate
|
ios::app );
146
if
(fout
==
NULL)
147
{
148
cout
<<
"
fout = NULL
"
<<
endl;
149
}
150
fout
<<
"
RETURN log:
"
;
151
WRITELOG(fout);
152
}
153
}
154
155
void
LogCheckArg(
bool
bl,
struct
timeval t1, unsigned
int
id1,
156
string
fuction1,
int
line1,
string
file1,
157
char
*
info1)
158
{
159
if
(LogManagementInfo.logoutype
==
0
)
160
{
161
cout
<<
"
Check log:
"
;
162
WRITELOG(cout);
163
if
(bl
==
true
)
164
{
165
LogManagementInfo.logleval
=
error;
166
cout
<<
"
正确
"
<<
endl;
167
}
168
else
169
{
170
LogManagementInfo.logleval
=
debug;
171
cout
<<
"
错误
"
<<
endl;
172
}
173
}
174
else
if
(LogManagementInfo.logoutype
==
1
)
175
{
176
ofstream fout((strFileName.c_str()), ios::ate
|
ios::app );
177
cout
<<
"
Check log:
"
;
178
WRITELOG(fout);
179
if
(bl
==
true
)
180
{
181
LogManagementInfo.logleval
=
error;
182
fout
<<
"
正确
"
<<
endl;
183
}
184
else
185
{
186
LogManagementInfo.logleval
=
debug;
187
fout
<<
"
错误
"
<<
endl;
188
}
189
}
190
}
191
void
LogPrint(
int
level,
char
*
info,
)
192
{
193
if
(LogManagementInfo.logleval
>=
level)
194
{
195
if
(LogManagementInfo.logoutype
==
0
)
196
{
197
va_list argptr;
198
va_start(argptr, info);
199
char
buffer[
256
];
200
vsprintf( buffer,info,argptr);
201
va_end(argptr);
202
cout
<<
"
附加信息 log:
"
;
203
cout
<<
"
"
<<
"
Info
"
<<
buffer
<<
endl;
204
}
205
else
if
(LogManagementInfo.logoutype
==
1
)
206
{
207
ofstream fout((strFileName.c_str()), ios::ate
|
ios::app );
208
va_list argptr;
209
va_start(argptr, info);
210
char
buffer[
256
];
211
vsprintf( buffer,info,argptr);
212
va_end(argptr);
213
fout
<<
"
Info
"
<<
endl;
214
fout
<<
"
附加信息 log:
"
;
215
fout
<<
"
"
<<
"
Info
"
<<
buffer
<<
endl;
216
}
217
}
218
}
219
220
221
222
223
1
#include
"
loging.h
"
2
#include
<
iostream
>
3
4
using
namespace
std;
5
int
fun1();
6
int
fun()
7
{
8
RETURN_NULL;
9
}
10
int
fun1()
11
{
12
RETURN(
2
);
13
}
14
int
main()
15
{
16
char
*
pFatalInfo
=
"
Fatal Log
"
;
17
char
*
pErrorInfo
=
"
Error Log
"
;
18
char
*
pWarringInfo
=
"
Waring Log
"
;
19
char
*
pDebugInfo
=
"
Debug Log
"
;
20
char
*
pInfo1
=
"
info1
"
;
21
char
*
pInfo2
=
"
info2
"
;
22
char
*
pInfo3
=
"
info3
"
;
23
char
*
b
=
"
bool bool
"
;
24
LOGINIT;
25
26
27
28
//
LOGFATAL(pFatalInfo);
29
//
LOGERROR(pErrorInfo);
30
//
LOGWARNING(pWarringInfo);
31
//
LOGDEBUG(pDebugInfo);
32
//
LOGINFO(0, pInfo1);
33
LOGINFO(
1
, pInfo1);
34
LOGINFO(
2
, pInfo1);
35
LOGINFO(
3
, pInfo1);
36
LOGINFO(
4
, pInfo1);
37
LOGINFO(
5
, pInfo1);
38
LOGINFO(
6
, pInfo1);
39
LOGINFO(
7
, pInfo1);
40
LOGINFO(
8
, pInfo1);
41
42
43
LogPrint(
4
,
"
%d %f %c
"
,
1
,
1.2
,
'
c
'
);
44
45
unsigned
int
__NR_gettid;
46
syscall(__NR_gettid);
47
cout
<<
"
__NR_gettid
"
<<
__NR_gettid
<<
endl;
48
fun();
49
fun1();
50
51
52
LOGCHECKARG(
false
, b);
53
return
0
;
54
}
55
posted on 2010-08-19 19:18
郭龙
阅读(1659)
评论(0)
编辑
收藏
引用
只有注册用户
登录
后才能发表评论。
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
网站导航:
博客园
IT新闻
BlogJava
知识库
博问
管理
Powered by:
C++博客
Copyright © 郭龙