求助:log4cplus的使用中,如何消除产生的多余的重复信息?

fengbingchun
博客专家认证
2011-12-13 09:02:11
下面是一个例子程序:
#include "stdafx.h"
#include <string>

#include <log4cplus/logger.h>//定义Log对象
#include <log4cplus/consoleappender.h>//输出到控制台
#include <log4cplus/layout.h>//输出格式
#include <log4cplus/configurator.h>

using namespace std;
using namespace log4cplus;
using namespace log4cplus::helpers;


int _tmain(int argc, _TCHAR* argv[])
{
for (int i=0; i<5; i++)
{
//定义控制台Appender
SharedAppenderPtr pConsoleAppender(new ConsoleAppender());

//定义Layout,并绑定到Appender上
log4cplus::tstring pattern = LOG4CPLUS_TEXT("%D{%Y/%m/%d %X} %p %% %m %% %n");
pConsoleAppender->setLayout(std::auto_ptr<Layout>(new PatternLayout(pattern)));

//定义Logger
Logger pTestLogger = Logger::getInstance(LOG4CPLUS_TEXT("LoggerName"));

//设置Logger优先级
pTestLogger.setLogLevel(INFO_LOG_LEVEL);

//将需要关联Logger的Appender添加到Logger上
pTestLogger.addAppender(pConsoleAppender);

char c[10];
itoa(i+1, c, 10);
string strItoStr = c;

//输出日志信息
LOG4CPLUS_INFO(pTestLogger, strItoStr);

}
return 0;
}

输出结果为:
2011/12/13 08:54:28 INFO % 1 %
2011/12/13 08:54:28 INFO % 2 %
2011/12/13 08:54:28 INFO % 2 %
2011/12/13 08:54:28 INFO % 3 %
2011/12/13 08:54:28 INFO % 3 %
2011/12/13 08:54:28 INFO % 3 %
2011/12/13 08:54:28 INFO % 4 %
2011/12/13 08:54:28 INFO % 4 %
2011/12/13 08:54:28 INFO % 4 %
2011/12/13 08:54:28 INFO % 4 %
2011/12/13 08:54:28 INFO % 5 %
2011/12/13 08:54:28 INFO % 5 %
2011/12/13 08:54:28 INFO % 5 %
2011/12/13 08:54:28 INFO % 5 %
2011/12/13 08:54:28 INFO % 5 %

希望输出的结果为:
2011/12/13 08:54:28 INFO % 1 %
2011/12/13 08:54:28 INFO % 2 %
2011/12/13 08:54:28 INFO % 3 %
2011/12/13 08:54:28 INFO % 4 %
2011/12/13 08:54:28 INFO % 5 %

为何每次都会重复产生比上一次多一次的重复结果呢?如果将此作为一个函数被工程调用的话,会生成很多重复的结果,该如何修改才能产生希望的结果呢?望高手指点,谢谢!
...全文
373 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengbingchun 2011-12-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhao4zhong1 的回复:]
更新了,能显示毫秒。

C/C++ code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys……
[/Quote]

谢谢!
赵4老师 2011-12-16
  • 打赏
  • 举报
回复
更新了,能显示毫秒。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define _vsnprintf vsnprintf
#endif
//Log{
#define MAXLOGSIZE 100000000
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
char logstr[16000];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
struct timeb tb;


if (NULL==pszFmt||0==pszFmt[0]) return;
if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
flog=fopen(logfilename1,"a");
if (NULL==flog) return;
}
fclose(flog);
}
}
void Log(const char *pszFmt,...) {
va_list argp;

Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
int i;
#ifdef WIN32
InitializeCriticalSection(&cs_log);
#else
pthread_mutex_init(&cs_log,NULL);
#endif
for (i=0;i<10000;i++) {
Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
}
#ifdef WIN32
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
fengbingchun 2011-12-13
  • 打赏
  • 举报
回复
我以前也没注意到这个问题,当把它应用到项目中去时才发现,会生成越来越多的重复信息。该如何解决呢?
望高手们指点,谢谢!
龙哥依旧 2011-12-13
  • 打赏
  • 举报
回复
帮顶,我那个也有类似的问题,但是我都不考虑了,因为那个项目也没人怎么管了,呵呵!
同时关注一下,看看怎么解决!
:)
fengbingchun 2011-12-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhao4zhong1 的回复:]
改用我这个试试?

C/C++ code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/t……
[/Quote]

我试了试确实能运行,非常谢谢!
赵4老师 2011-12-13
  • 打赏
  • 举报
回复
改用我这个试试?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define _vsnprintf vsnprintf
#endif
//Log{
#define MAXLOGSIZE 100000000
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#include <time.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
char logstr[16000];
char datestr[16];
char timestr[16];
char ms10[3];
CRITICAL_SECTION cs_log;
FILE *flog;
int centisec() {
#ifdef WIN32
return ((GetTickCount()%1000L)/10)%100;
#else
struct timeval tv;

if (!gettimeofday(&tv,NULL)) {
return ((tv.tv_usec%1000000L)/10000)%100;
} else {
return 0;
}
#endif
}
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
time_t aclock;


if (NULL==pszFmt||0==pszFmt[0]) return;
if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;
time(&aclock);
now=localtime(&aclock);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(ms10,"%02d",centisec());
printf("%s %s.%s %s",datestr,timestr,ms10,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,ms10,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
flog=fopen(logfilename1,"a");
if (NULL==flog) return;
}
fclose(flog);
}
}
void Log(const char *pszFmt,...) {
va_list argp;

Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
int i;
#ifdef WIN32
InitializeCriticalSection(&cs_log);
#else
pthread_mutex_init(&cs_log,NULL);
#endif
for (i=0;i<10000;i++) {
Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
}
#ifdef WIN32
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
fengbingchun 2011-12-13
  • 打赏
  • 举报
回复
高手们帮帮忙呀,难道你们没有遇到过类似的问题吗?

64,691

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧