下载地址http://log4cpp.sourceforge.net
log4cpp只提供了VC6的项目文件,在msvc6目录下
用vs2005打开msvc6.dsw文件,打开时会有错误弹出,不用管它,转换完项目后有多个工程只有留下log4cpp和log4cppDll,并删除两个项目中的NTEventLogCategories.mc 文件引用
要解决一个 mode_t 类型可能出现的重定义问题,需要打开以下文件
c:\log4cpp-0.3.5rc3\include\log4cpp\config-win32.h
查找 typedef int mode_t; 改为以下内容,(即加上 namespace log4cpp)
namespace log4cpp
{
typedef int mode_t;
}
{
typedef int mode_t;
}
现在可以开始编译了,编译log4cpp时没有问题,但编译log4cppdll时出错,连接时找不到符号地址,解决方式就是将FactoryParams.cpp和Localtime.cpp加入工程即可。
先来个简单的例子
#include "stdafx.h"
#include <iostream>
#include "log4cpp/Category.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/PatternLayout.hh"
#include "log4cpp/FileAppender.hh"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
log4cpp::OstreamAppender* osAppender =new log4cpp::OstreamAppender("osAppender", &cout);
log4cpp::FileAppender* osfileAppender=new log4cpp::FileAppender("osfileAppender","e:\\VideoTmp\\log\\test.log");
log4cpp::PatternLayout* pLayout = new log4cpp::PatternLayout();
pLayout->setConversionPattern("%d: %p %c %x: %m%n");
osAppender->setLayout(pLayout);
log4cpp::PatternLayout* pfileLayout = new log4cpp::PatternLayout();
pfileLayout->setConversionPattern("%d: %p %c %x: %m%n");
osfileAppender->setLayout(pfileLayout);
log4cpp::Category& root = log4cpp::Category::getRoot();
root.addAppender(osAppender);
root.addAppender(osfileAppender);
root.setPriority(log4cpp::Priority::DEBUG);
root.error("Hello log4cpp in a Error Message!");
root.warn("Hello log4cpp in a Warning Message!");
log4cpp::Category::shutdown();
cin.get();
return 0;
#include <iostream>
#include "log4cpp/Category.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/PatternLayout.hh"
#include "log4cpp/FileAppender.hh"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
log4cpp::OstreamAppender* osAppender =new log4cpp::OstreamAppender("osAppender", &cout);
log4cpp::FileAppender* osfileAppender=new log4cpp::FileAppender("osfileAppender","e:\\VideoTmp\\log\\test.log");
log4cpp::PatternLayout* pLayout = new log4cpp::PatternLayout();
pLayout->setConversionPattern("%d: %p %c %x: %m%n");
osAppender->setLayout(pLayout);
log4cpp::PatternLayout* pfileLayout = new log4cpp::PatternLayout();
pfileLayout->setConversionPattern("%d: %p %c %x: %m%n");
osfileAppender->setLayout(pfileLayout);
log4cpp::Category& root = log4cpp::Category::getRoot();
root.addAppender(osAppender);
root.addAppender(osfileAppender);
root.setPriority(log4cpp::Priority::DEBUG);
root.error("Hello log4cpp in a Error Message!");
root.warn("Hello log4cpp in a Warning Message!");
log4cpp::Category::shutdown();
cin.get();
return 0;
}