2010-12-23 4 views
0

최근에 나는 boost.log로 게임을 시작했으며, 손보가 예외가 발생하면 로그 파일에 로그 메시지가 기록되지 않습니다. 롤링 텍스트 파일을 사용 중이며 자동 플래시 옵션이 설정되어 있습니다. 여기 boost.log auto_flush 파일이 앱이 손상되었을 때 저장되지 않습니다.

은 샘플에서 수정 된 소스입니다 :

#include <stdexcept> 
#include <string> 
#include <iostream> 
#include <fstream> 
#include <functional> 
#include <boost/ref.hpp> 
#include <boost/bind.hpp> 
#include <boost/shared_ptr.hpp> 
#include <boost/date_time/gregorian/gregorian.hpp> 
#include <boost/date_time/posix_time/posix_time_types.hpp> 
#include <boost/thread/thread.hpp> 
#include <boost/thread/barrier.hpp> 

#include <boost/log/common.hpp> 
#include <boost/log/filters.hpp> 
#include <boost/log/formatters.hpp> 
#include <boost/log/attributes.hpp> 
#include <boost/log/sinks.hpp> 
#include <boost/log/utility/empty_deleter.hpp> 
#include <boost/log/utility/record_ordering.hpp> 

namespace logging = boost::log; 
namespace attrs = boost::log::attributes; 
namespace src = boost::log::sources; 
namespace sinks = boost::log::sinks; 
namespace fmt = boost::log::formatters; 
namespace keywords = boost::log::keywords; 

using boost::shared_ptr; 
using namespace boost::gregorian; 

enum 
{ 
    LOG_RECORDS_TO_WRITE = 100, 
    LOG_RECORDS_TO_WRITE_BEFORE_EXCEPTION = 10, 
    THREAD_COUNT = 10 

}; 

BOOST_LOG_DECLARE_GLOBAL_LOGGER(test_lg, src::logger_mt) 

//! This function is executed in multiple threads 
void thread_fun(boost::barrier& bar) 
{ 
    // Wait until all threads are created 
    bar.wait(); 

    // Here we go. First, identify the thread. 
    BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::thread::id, boost::this_thread::get_id()); 

    // Now, do some logging 
    for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i) 
    { 
     BOOST_LOG(get_test_lg()) << "Log record " << i; 

     if(i > LOG_RECORDS_TO_WRITE_BEFORE_EXCEPTION) 
     { 
      BOOST_THROW_EXCEPTION(std::exception("unhandled exception")); 
     } 
    } 

} 

int main(int argc, char* argv[]) 
{ 
    try 
    { 
     typedef sinks::synchronous_sink<sinks::text_file_backend> file_sink; 
     shared_ptr<file_sink> sink(new file_sink(
      keywords::file_name = L"%Y%m%d_%H%M%S_%5N.log",  // file name pattern 
      keywords::rotation_size = 10 * 1024 * 1024,   // rotation size, in characters 
      keywords::auto_flush = true       // make each log record flushed to the file 
      )); 

     // Set up where the rotated files will be stored 
     sink->locked_backend()->set_file_collector(sinks::file::make_collector(
      keywords::target = "log"    // where to store rotated files 
      )); 

     // Upon restart, scan the target directory for files matching the file_name pattern 
     sink->locked_backend()->scan_for_files(); 

     sink->locked_backend()->set_formatter(
      fmt::format("%1%: [%2%] [%3%] - %4%") 
      % fmt::attr< unsigned int >("Line #") 
      % fmt::date_time<boost::posix_time::ptime>("TimeStamp") 
      % fmt::attr<boost::thread::id>("ThreadID") 
      % fmt::message() 
      ); 

     // Add it to the core 
     logging::core::get()->add_sink(sink); 

     // Add some attributes too 
     shared_ptr<logging::attribute> attr(new attrs::local_clock); 
     logging::core::get()->add_global_attribute("TimeStamp", attr); 
     attr.reset(new attrs::counter< unsigned int >); 
     logging::core::get()->add_global_attribute("Line #", attr); 

     // Create logging threads 
     boost::barrier bar(THREAD_COUNT); 
     boost::thread_group threads; 
     for (unsigned int i = 0; i < THREAD_COUNT; ++i) 
      threads.create_thread(boost::bind(&thread_fun, boost::ref(bar))); 

     // Wait until all action ends 
     threads.join_all(); 

     return 0; 
    } 
    catch (std::exception& e) 
    { 
     std::cout << "FAILURE: " << e.what() << std::endl; 
     return 1; 
    } 
} 

소스는 비주얼 스튜디오에서 부스트 1.40을 위해 2008 년 boost.log 컴파일 컴파일됩니다.

도움이 매우 감사합니다.

+0

나는 당신의 코드를 시험해 보았다. 그것은 나를 위해 일했다. 하지만 예외가 발생하기 전에는 로그 메시지 만 기대합니다. 그렇죠? –

답변

2

로그 파일이 지정된 파일 수집기 대상 디렉토리 (샘플 코드의 "log")가 아닌 프로세스의 현재 작업 디렉토리에 있는지 확인하십시오. 또한 싱크 "file_name"패턴에 대한 디렉토리를 지정하려고합니다.

"JQ"메모에 예외 로깅이 표시 될 것으로 예상하지 마십시오.

관련 문제