2013-08-03 159 views
11

我一直在努力與boost日誌一會兒 - 我有他們的簡單的例子寫入日誌文件(http://boost-log.sourceforge.net/libs/log/example/doc/tutorial_file.cpp)。但是,當我嘗試將該代碼複製到「記錄器」類中時,我無法將其寫入日誌文件。我可以看到文件default.log被創建,但沒有任何內容。boost日誌文件沒有寫入

我在debian 7 64bit。一切編譯罰款 - 編譯行是:

g++ -o build/Logger.o -c -std=c++11 -Wall -g -O0 -DBOOST_LOG_DYN_LINK -DDEBUG src/Logger.cpp 
g++ -o build/logtest build/Logger.o -lboost_log -lboost_log_setup -lboost_date_time -lboost_thread -lboost_wave -lboost_regex -lboost_program_options 

這裏是我的代碼:

Logger.cpp

/* 
* Logger.cpp 
* 
* Created on: 2011-01-17 
*  Author: jarrett 
*/ 

#include "Logger.h" 

namespace logging = boost::log; 
namespace sinks = boost::log::sinks; 
namespace src = boost::log::sources; 
namespace expr = boost::log::expressions; 
namespace attrs = boost::log::attributes; 
namespace keywords = boost::log::keywords; 

namespace dhlogging { 

Logger::Logger(std::string fileName) 
{ 
    initialize(fileName); 
} 

Logger::Logger(Logger const&) 
{ 
} 

Logger::~Logger() 
{ 

} 

Logger* Logger::logger_ = nullptr; 
Logger* Logger::getInstance(std::string logFile) 
{ 
    if (Logger::logger_ == nullptr) { 
     logging::add_file_log(logFile); 

     logging::core::get()->set_filter 
     (
      logging::trivial::severity >= logging::trivial::info 
     ); 

     logging::add_common_attributes(); 
     Logger::logger_ = new Logger(logFile); 
    } 

    return Logger::logger_; 
} 

void Logger::initialize(std::string fileName) 
{ 
    BOOST_LOG(log_) << "Hello, World!"; 
    BOOST_LOG_SEV(log_, info) << "Hello, World2!"; 
} 

void Logger::logInfo(std::string message) 
{ 
    BOOST_LOG_SEV(log_, info) << message; 
} 

void Logger::logDebug(std::string message) 
{ 
    BOOST_LOG_SEV(log_, debug) << message; 
} 

void Logger::logWarn(std::string message) 
{ 
    BOOST_LOG_SEV(log_, warning) << message; 
} 

void Logger::logError(std::string message) 
{ 
    BOOST_LOG_SEV(log_, error) << message; 
} 

void Logger::logFatal(std::string message) 
{ 
    BOOST_LOG_SEV(log_, fatal) << message; 
} 

} 

int main(int, char*[]) 
{ 
    logging::add_common_attributes(); 

    using namespace logging::trivial; 

    dhlogging::Logger::getInstance()->logInfo("himom"); 

    return 0; 
} 

Logger.h

/* 
* Logger.h 
* 
* Created on: 2011-01-17 
*  Author: jarrett 
*/ 

#ifndef LOGGER_H_ 
#define LOGGER_H_ 

#include <map> 
#include <boost/log/core.hpp> 
#include <boost/log/trivial.hpp> 
#include <boost/log/expressions.hpp> 
#include <boost/log/sinks/text_file_backend.hpp> 
#include <boost/log/utility/setup/file.hpp> 
#include <boost/log/utility/setup/common_attributes.hpp> 
#include <boost/log/sources/severity_logger.hpp> 
#include <boost/log/sources/record_ostream.hpp> 

namespace logging = boost::log; 
namespace sinks = boost::log::sinks; 
namespace src = boost::log::sources; 
namespace expr = boost::log::expressions; 
namespace attrs = boost::log::attributes; 
namespace keywords = boost::log::keywords; 

using namespace logging::trivial; 

namespace dhlogging { 
class Logger { 

public: 
    static Logger* getInstance(std::string logFile = "default.log"); 

    void logInfo(std::string message); 
    void logDebug(std::string message); 
    void logWarn(std::string message); 
    void logError(std::string message); 
    void logFatal(std::string message); 


private: 
    Logger(std::string fileName); 
    Logger(Logger const&); 
    Logger& operator=(Logger const&); 
    virtual ~Logger(); 

    void initialize(std::string fileName); 

    src::severity_logger<severity_level> log_; 

    static Logger* logger_; // singleton instance 
}; 
} 
#endif /* LOGGER_H_ */ 

回答

31

在創建文件日誌

時需要這種屬性
keywords::auto_flush = true 

那種方式log entrys立即寫入。 默認情況下,文件記錄器似乎寫入文件,當它超出範圍,或在其他一些神祕的地方,文檔沒有提及任何有關

+4

這就是它 - 太奇怪了。這個例子甚至沒有提到這個關鍵字,但仍然完美。謝謝@ user1283078! – Jarrett

+1

示例:http://stackoverflow.com/questions/15853981/boost-log-2-0-empty-severity-level-in-logs – lepe