提升日志文件未写入

编程入门 行业动态 更新时间:2024-10-07 19:23:29
本文介绍了提升日志文件未写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在努力处理Boost日志-我得到了他们简单的示例,将其写入日志文件( boost-log.sourceforge/libs/log/example/doc/tutorial_file.cpp )。但是,当我尝试将该代码复制到 Logger类中时,无法将其写入日志文件。我可以看到文件 default.log 已创建,但其中没有任何内容。

I've been struggling with boost log for a while now - I got their simple example writing to a log file (boost-log.sourceforge/libs/log/example/doc/tutorial_file.cpp). However, when I try to copy that code into a 'Logger' class, I can't get it to write to the log file. I can see the file default.log get created, but there is nothing in it.

我在debian 7 64位。一切都可以正常编译-编译行是:

I'm on debian 7 64bit. Everything compiles fine - compile line is:

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_ */

推荐答案

创建文件日志时需要此属性

you need to this attribute when creating the file log

keywords::auto_flush = true

以这种方式立即写入日志条目。 默认情况下,超出范围的文件记录器似乎会写入文件,或者在其他神秘之处,文档中没有提及任何内容

that way log entrys get written immediately. by default, the file logger seems to write to file when it goes out of scope, or at some other mysterious point, the documentation doesn´t mention anything about

更多推荐

提升日志文件未写入

本文发布于:2023-11-23 21:43:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622934.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   日志

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!