首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

使用boost.log,发现当重新启动程序时log文件被清空,求解决。该如何处理

2012-02-06 
使用boost.log,发现当重新启动程序时log文件被清空,求解决。最近在学习boost.log,windows平台,log文件test.

使用boost.log,发现当重新启动程序时log文件被清空,求解决。
最近在学习boost.log,windows平台,log文件test.log和test1.log都能自动生成,但发现一个奇怪的问题,就是当重新启动程序时原有的log总被清掉,然后写入新的log。
求高人指点,程序很简单,如下:

C/C++ code
#include "stdafx.h"#include <string>#include <iostream>#include <fstream>#include <boost/log/core.hpp>#include <boost/log/trivial.hpp>#include <boost/log/filters.hpp>#include <boost/log/sinks.hpp>#include <boost/log/attributes.hpp>#include <boost/log/common.hpp>#include <boost/log/formatters.hpp>#include <boost/log/utility/init/to_file.hpp>#include <boost/log/utility/init/from_stream.hpp>namespace logging = boost::log;namespace fmt = boost::log::formatters;namespace flt = boost::log::filters; namespace attrs = boost::log::attributes;namespace src = boost::log::sources;namespace sinks = boost::log::sinks; using boost::shared_ptr;using namespace std;enum severity_level{    debug,    info,    warn,    error};BOOST_LOG_DECLARE_GLOBAL_LOGGER(logger, src::severity_logger_mt<>)void try_logging( std::string c ){    BOOST_LOG_SCOPED_THREAD_TAG("Tag", string, c.c_str());        src::severity_logger_mt<>& lg = get_logger();    BOOST_LOG_SEV(lg, debug) << "This is a debug severity record";    BOOST_LOG_SEV(lg, info) << "This is a info severity record";    BOOST_LOG_SEV(lg, warn) << "This is a warn severity record";    BOOST_LOG_SEV(lg, error) << "This is a error severity record";}int _tmain(int argc, _TCHAR* argv[]){        ifstream settings("F:\\work\\mshtml\\logtest\\log.setting");        if (!settings.is_open())        {            cout << "Could not open settings.txt file" << std::endl;            return 1;        }        cout << "init_from_stream" << endl;        // Read the settings and initialize logging library        logging::init_from_stream(settings);        cout << "end init_from_stream" << endl;        shared_ptr< logging::attribute > attr(new attrs::local_clock);        logging::core::get()->add_global_attribute("TimeStamp", attr);        boost::thread_group g;         for (char i = 'a'; i < 'd'; i++){           g.create_thread(boost::bind(&try_logging, std::string("aaa") + i));            cout << "create thread " << i << endl;        }        g.join_all();        cout << "finish" << endl;}


配置文件如下:
C/C++ code
[Core]DisableLogging=falseFilter="%Severity% >= 2"[Sink:2]Filter="%Tag% = aaab"Destination=TextFileFileName=F:\work\mshtml\logtest\log\test.logTarget=F:\work\mshtml\logtest\logScanForFiles=AllAutoFlush=trueFormat="[%TimeStamp%] %Tag% %_%"RotationSize=10485760Asynchronous=false[Sink:3]Filter="%Tag% = aaaa"Destination=TextFileFileName=F:\work\mshtml\logtest\log\test1.logTarget=F:\work\mshtml\logtest\logScanForFiles=AllAutoFlush=trueFormat="%Tag% %_%"Asynchronous=false


[解决办法]
/*
cl /EHsc /DBOOST_FILESYSTEM_VERSION=2 /DWIN32=0x0501 /Ig:\src\boost_1_47_0 logtest.cpp /link /verbose:lib /libpath:g:\src\boost_1_47_0\stage\lib

*/
#include <map>
#include <string>
#include <iostream>
#include <fstream>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/filters.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/common.hpp>


#include <boost/log/formatters.hpp>
#include <boost/log/utility/init/to_file.hpp>
#include <boost/log/utility/init/from_stream.hpp>
#include <boost/log/utility/init/from_settings.hpp>
#include <boost/any.hpp>
#include <boost/function/function1.hpp>
#include <boost/regex.hpp>
#include <tchar.h>
namespace logging = boost::log;
namespace fmt = boost::log::formatters;
namespace flt = boost::log::filters; 
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks; 
using boost::shared_ptr;
using namespace std;

enum severity_level
{
debug,
info,
warn,
error
};

BOOST_LOG_DECLARE_GLOBAL_LOGGER(logger, src::severity_logger_mt<>)

shared_ptr< logging::sinks::sink< char > > customize_sink(map< string, boost::any > const& params)
{
// Read parameters for the backend and create it
// std::map< std::string, std::string >::const_iterator it = params.find("FileName");
// if (it == params.end()) {;}

shared_ptr< logging::sinks::text_file_backend > backend = boost::make_shared< logging::sinks::text_file_backend >(logging::keywords::open_mode = ios::app);
/*logging::keywords::open_mode = ios::app,
logging::keywords::auto_flush = */


typedef logging::sinks::synchronous_sink< logging::sinks::text_file_backend > sink_t;
shared_ptr< sink_t > sink(new sink_t(backend));
return sink;
}

// void init()
// {
// logging::init_log_to_file
// (
// keywords::file_name = "sample_%N.log",
// keywords::format = "[%TimeStamp%]: %_%",
// keywords::open_mode=std::ios::app
// );
// 
// logging::core::get()->set_filter
// (
// flt::attr< logging::trivial::severity_level >("Severity") >= logging::trivial::info
// );
// }

void try_logging( std::string c )
{
BOOST_LOG_SCOPED_THREAD_TAG("Tag", string, c.c_str());

src::severity_logger_mt<>& lg = get_logger();

BOOST_LOG_SEV(lg, debug) << "This is a debug severity record";
BOOST_LOG_SEV(lg, info) << "This is a info severity record";
BOOST_LOG_SEV(lg, warn) << "This is a warn severity record";
BOOST_LOG_SEV(lg, error) << "This is a error severity record";
}

int _tmain(int argc, _TCHAR* argv[])
{
ifstream settings("log.setting");
if (!settings.is_open())
{
cout << "Could not open settings.txt file" << std::endl;
return 1;
}

cout << "init_from_stream" << endl;
// Read the settings and initialize logging library
boost::function1<shared_ptr< logging::sinks::sink< char > > , std::map< string, boost::any > const&> fa = &customize_sink;
logging::register_sink_factory( _T("TextFile"), fa );
logging::init_from_stream(settings);
cout << "end init_from_stream" << endl; 

shared_ptr< logging::attribute > attr(new attrs::local_clock);
logging::core::get()->add_global_attribute("TimeStamp", attr);
boost::thread_group g; 
for (char i = 'a'; i < 'd'; i++){
g.create_thread(boost::bind(&try_logging, std::string("aaa") + i));
cout << "create thread " << i << endl;
}

g.join_all();


cout << "finish" << endl;
}

热点排行