使用boost.log,发现当重新启动程序时log文件被清空,求解决。
本帖最后由 ironskinspirit 于 2011-11-23 16:36:02 编辑 最近在学习boost.log,windows平台,log文件test.log和test1.log都能自动生成,但发现一个奇怪的问题,就是当重新启动程序时原有的log总被清掉,然后写入新的log。
求高人指点,程序很简单,如下:
#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;
}
[Core]
DisableLogging=false
Filter="%Severity% >= 2"
[Sink:2]
Filter="%Tag% = aaab"
Destination=TextFile
FileName=F:\work\mshtml\logtest\log\test.log
Target=F:\work\mshtml\logtest\log
ScanForFiles=All
AutoFlush=true
Format="[%TimeStamp%] %Tag% %_%"
RotationSize=10485760
Asynchronous=false
[Sink:3]
Filter="%Tag% = aaaa"
Destination=TextFile
FileName=F:\work\mshtml\logtest\log\test1.log
Target=F:\work\mshtml\logtest\log
ScanForFiles=All
AutoFlush=true
Format="%Tag% %_%"
Asynchronous=false
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;
}