- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在创建一个应该用于日志目的的类。
但是这里有一个问题,它不支持线程同步。当我一个线程一个线程运行的时候没有问题。但是当我同时运行两个或多个线程时出现问题。
我面临的问题是:“如果我创建两个线程来创建两个单独的日志,但是两个线程发生冲突并将其日志消息写入两个文件”。
如果有人发现问题,请帮我解决。
BoostLogger.h :
#pragma once
......
///////////////////////////////////////
//Defining Macros
///////////////////////////////////////
#define AddCommonAttr() logging::add_common_attributes()
#define GetLoggingCore() logging::core::get()
#define LoggingSeverity logging::trivial::severity
#define AddFileLog logging::add_file_log
#define ThreadValueType logging::attributes::current_thread_id::value_type
#define Record logging::record
#define Extract logging::extract
#define ExprStream expr::stream
#define ExprAttr expr::attr
#define ExprFormatDateTime expr::format_date_time
#define PosixTimeType boost::posix_time::ptime
#define ExprMessage expr::smessage
#define FileName keywords::file_name
#define RotationSize keywords::rotation_size
#define TimeBasedRotation keywords::time_based_rotation
#define Format keywords::format
#define Target keywords::target
#define MaxSize keywords::max_size
#define MinFreeSpace keywords::min_free_space
#define RotationAtTimeInterval sinks::file::rotation_at_time_interval
#define Reset_Filter reset_filter /*The reset_filter method removes the global logging filter.*/
#define Set_Filter set_filter /*The set_filter method sets the global logging filter to every log record that is processed.*/
#define SetFormatter set_formatter
#define RecordView logging::record_view
#define FormattingOstream logging::formatting_ostream
#define SharedPtr boost::shared_ptr
#define MakeShared boost::make_shared
#define SinkFileBackend sinks::text_file_backend
#define LockedBackend locked_backend
#define SetFileCollector set_file_collector
#define MakeCollector sinks::file::make_collector
#define AddSink add_sink /*The add_sink method adds a new sink. The sink is included into logging process immediately after being added and until being removed. No sink can be added more than once at the same time. If the sink is already registered, the call is ignored.*/
#define RemoveSink remove_sink /*The remove_sink method removes the sink from the output. The sink will not receive any log records after removal. The call has no effect if the sink is not registered.*/
#define RemoveAllSinks remove_all_sinks /*The remove_all_sinks method removes all registered sinks from the output. The sinks will not receive any log records after removal.*/
#define Flush flush
#define ScanForFiles scan_for_files
#define ScanAll sinks::file::scan_all
#define ScanMatching sinks::file::scan_matching
#define SetExceptionHandler set_exception_handler
#define ExceptionSuppressor logging::make_exception_suppressor
#define MakeExceptionHandler logging::make_exception_handler
typedef sinks::synchronous_sink < SinkFileBackend > sink_type;
static src::logger lg;
#define WriteToLog BOOST_LOG(lg)
/*Defining Macros for Writing log with Severity*/
//BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
//static src::severity_logger< logging::trivial::severity_level > slg;
#define LogTrace BOOST_LOG_SEV(obj->slg, logging::trivial::trace)
#define LogDebug BOOST_LOG_SEV(obj->slg, logging::trivial::debug)
#define LogInfo BOOST_LOG_SEV(obj->slg, logging::trivial::info)
#define LogWarning BOOST_LOG_SEV(obj->slg, logging::trivial::warning)
#define LogError BOOST_LOG_SEV(obj->slg, logging::trivial::error)
#define LogFatal BOOST_LOG_SEV(obj->slg, logging::trivial::fatal)
#define _1MB (1 * 1024 * 1024)
#define _10MB (10 * 1024 * 1024)
#define datefmt ("_%Y-%b-%d")
#define timefmt ("_%H-%M-%S")
using namespace std;
class CBoostLogger
{
private:
SharedPtr< SinkFileBackend > backend;
SharedPtr< sink_type > sink;
public:
src::severity_logger< logging::trivial::severity_level > slg;
CBoostLogger(void);
~CBoostLogger(void);
bool StartLogger(struct FileFormat *sff);
bool StopLogger();
bool SetFilter(short severitylevel);
bool SetFormat(struct LogFormat *sle);
private:
friend void Formatter(logging::record_view const& rec, logging::formatting_ostream& strm);
};
/*This Structure is used to set the formats for file*/
struct FileFormat
{
bool includedatetofile;
bool includetimetofile;
string filename;
string filelocation;
unsigned long rotationsize;
unsigned long maxsize;
FileFormat() : includedatetofile(false),
includetimetofile(false),
filename("log") ,
filelocation("C:/Log") ,
rotationsize(_1MB) ,
maxsize(_10MB) {};
};
struct LogFormat
{
bool Set_LineID;
bool Set_Time;
bool Set_Severity;
bool Set_ThreadID;
bool Set_Message;
LogFormat() : Set_LineID(true) ,
Set_Time(true) ,
Set_Severity(true) ,
Set_ThreadID(true) ,
Set_Message(true) {};
LogFormat(bool lineid, bool time, bool severity, bool threadid, bool message)
: Set_LineID(lineid) ,
Set_Time(time) ,
Set_Severity(severity) ,
Set_ThreadID(threadid) ,
Set_Message(message) {};
};
BoostLogger.cpp:
#pragma once
#include "BoostLogger.h"
////////////////////////////////////
//Global Declarations
////////////////////////////////////
bool SetLineID, SetTime, SetSeverity, SetThreadID, SetMessage ;
CBoostLogger::CBoostLogger(void)
{
cout << "Calling CBoostLogger Constructor..." << endl;
SetFilter(2);
//GetLoggingCore()->SetExceptionHandler(MakeExceptionHandler<std::runtime_error,std::exception>(handler()));
GetLoggingCore()->SetExceptionHandler(ExceptionSuppressor());
}
CBoostLogger::~CBoostLogger(void)
{
GetLoggingCore() -> Reset_Filter();
GetLoggingCore() -> RemoveAllSinks();
}
bool CBoostLogger::StartLogger(struct FileFormat *sff )
{
if(sff->includedatetofile)
sff->filename += datefmt;
if(sff->includetimetofile)
sff->filename += timefmt;
sff->filename += ".log";
backend = MakeShared < SinkFileBackend >(
FileName = sff->filename, /*< file name pattern >*/
RotationSize = sff->rotationsize /*< rotate files for every 1M >*/
);
sink = MakeShared < sink_type > (backend);
LogFormat sle;
SetFormat(&sle);
sink->LockedBackend()->SetFileCollector
(
MakeCollector
(
Target = sff->filelocation , /*File Storage Location*/
MaxSize = sff->maxsize /*Limit for folder : maxsize, where initially maxsize = 10M*/
)
);
sink->LockedBackend()->ScanForFiles(ScanAll);
GetLoggingCore()->AddSink(sink);
AddCommonAttr();
BOOST_LOG_SEV(this->slg, logging::trivial::info) << "Logger Starts";
return true;
}
/*This function used to remove the registered sink from core.*/
bool CBoostLogger::StopLogger()
{
BOOST_LOG_SEV(this->slg, logging::trivial::info) << "Logger Stops";
GetLoggingCore()->RemoveSink(sink);
GetLoggingCore()->Flush();
return true;
}
/*This function is used to set filter level. */
bool CBoostLogger::SetFilter(short severitylevel)
{
GetLoggingCore()->Set_Filter
(
LoggingSeverity >= severitylevel
);
return true;
}
/*This function is used to set format for log. */
bool CBoostLogger::SetFormat(struct LogFormat *sle)
{
SetLineID = sle->Set_LineID;
SetTime = sle->Set_Time;
SetSeverity = sle->Set_Severity;
SetThreadID = sle->Set_ThreadID;
SetMessage = sle->Set_Message;
sink->SetFormatter(&Formatter);
return true;
}
/*This function is used to set format for the log file.*/
void Formatter(RecordView const& rec, FormattingOstream& strm)
{
if(SetLineID)
{
strm << Extract < unsigned int > ("LineID", rec) << "\t"; // Get the LineID attribute value and put it into the stream
}
if(SetTime)
{
strm << Extract < PosixTimeType > ("TimeStamp", rec) << "\t"; // Get the TimeStamp attribute value and put it into the stream
}
if(SetSeverity)
{
strm << "[ " << rec[LoggingSeverity] << " ]\t"; // Get the Severity attribute value and put it into the stream
}
if(SetThreadID)
{
strm << Extract < ThreadValueType > ("ThreadID", rec )<<"\t"; // Get the ThreadID attribute value and put into the stream
}
if(SetMessage)
{
strm << rec[ExprMessage]; // Finally, put the record message to the stream
}
}
struct handler
{
void operator()(const runtime_error &ex) const
{
std::cerr << "\nRuntime_error: " << ex.what() << '\n';
}
void operator()(const exception &ex) const
{
std::cerr << "Exception: " << ex.what() << '\n';
}
};
源.cpp :
#include "BoostLogger.h"
void func_thread(std::string fn,string fl,int num)
{
std::string buf = "";
char str[20];
buf += itoa(num, str, 10);
fn += buf;
CBoostLogger *obj = new CBoostLogger();
FileFormat formatobj;
formatobj.filename = fn;
formatobj.filelocation = fl;
formatobj.includedatetofile = true;
formatobj.includetimetofile = true;
obj->StartLogger(&formatobj);
for(int i=0;i<10000;i++)
{
LogTrace << "Trace message new " << fn;
BOOST_LOG_SEV(obj->slg,logging::trivial::trace) << "Test";
LogDebug << "Debug Message new" << fn;
LogInfo << "Info message" << fn;
LogWarning << "Warning message new" << fn;
LogError << "An error message new" << fn;
LogFatal << "A fatal message new" << fn;
}
LogFormat sle(true,false,false,false,true);
obj->SetFormat(&sle);
for(int i=0;i<10000;i++)
{
LogTrace << "Trace message new " << fn;
LogDebug << "Debug Message new" << fn;
LogInfo << "Info message" << fn;
LogWarning << "Warning message new" << fn;
LogError << "An error message new" << fn;
LogFatal << "A fatal message new" << fn;
}
obj->StopLogger();
delete obj;
}
int main()
{
//This following code makes problem.
boost::thread *thread1 = new boost::thread(&func_thread,"Thread_","C:/BoostLog",1);
boost::thread *thread2 = new boost::thread(&func_thread,"Thread_","C:/BoostLog",2);
thread1->join();
thread2->join();
/*
//This following is not making that problem.
boost::thread_group t_groups;
for(int i=1;i<=5;i++)
{
t_groups.create_thread(boost::bind(&func_thread,"Thread","C:/BoostLog",i));
t_groups.join_all();
}
boost::thread_group tgroup;
boost::thread *threads;
for(int i=0;i<20;i++)
{
threads=new boost::thread(&func_thread,"Thread","C:/BoostLog",i);
tgroup.add_thread(threads);
std::cout << "\nThread "<<i<<" is created whose id is : "<<threads->get_id();
threads->join();
}
*/
return 0;
}
如果您需要更多关于此的信息,请问我。
我想使用 boost 库创建线程安全的记录器。如果可以,请帮助我。
还有一件事,如果可能,线程必须并发运行。
谢谢。
最佳答案
为了碰撞使用
BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "标签", std::string)
或者
BOOST_LOG_SCOPED_THREAD_TAG("标签", tag_)
有关详细信息,您可以在下面查看:
Boost.log: How to prevent the output will be duplicated to all added streams when it uses the add_file_log() function?
作者:蒂亚古
关于c++ - 我的程序在使用 boost 库时不支持线程安全日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35698945/
我正在尝试使用boost.spirit的qi库解析某些内容,而我遇到了一个问题。根据spirit docs,a >> b应该产生类型为tuple的东西。但这是boost::tuple(又名 fusio
似乎有/正在努力做到这一点,但到目前为止我看到的大多数资源要么已经过时(带有死链接),要么几乎没有信息来实际构建一个小的工作样本(例如,依赖于boost program_options 以构建可执行文
我对 Boost.Log 的状态有点困惑。这是 Boost 的官方部分,还是尚未被接受?当我用谷歌搜索时,我看到一些帖子谈论它在 2010 年是如何被接受的,等等,但是当我查看最后一个 Boost 库
Boost 提供了两种不同的实现 string_view ,这将成为 C++17 的一部分: boost::string_ref在 utility/string_ref.hpp boost::stri
最近,我被一家GIS公司雇用来重写他们的旧地理信息库。所以我目前正在寻找一个好的计算几何库。我看过CGAL,这真是了不起,但是我的老板想要免费的东西。 所以我现在正在检查Boost.Geometry。
假设我有一个无向图 G。假设我添加以下内容 add_edge(1,2,G); add_edge(1,3,G); add_edge(0,2,G); 现在我再说一遍: add_edge(0,2,G); 我
我使用 CMake 来查找 Boost。找到了 Boost,但 CMake 出错了 Imported targets not available for Boost version 请参阅下面的完整错
我是 boost::fusion 和 boost::mpl 库的新手。谁能告诉我这两个库之间的主要区别? 到目前为止,我只使用 fusion::vector 和其他一些简单的东西。现在我想使用 fus
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: What are the benefits of using Boost.Phoenix? 所以我开始阅读 boos
我正在尝试获得一个使用 Boost.Timer 的简单示例,用于一些秒表性能测量,但我不明白为什么我无法成功地将 Boost.Timer 链接到 Boost.Chrono。我使用以下简单脚本从源代码构
我有这样的东西: enum EFood{ eMeat, eFruit }; class Food{ }; class Meat: public Food{ void someM
有人可以告诉我,我如何获得boost::Variant处理无序地图? typedef boost::variant lut_value;unordered_map table; 我认为有一个用于boo
我对 Boost.Geometry 中的环和多边形感到困惑。 在文档中,没有图形显示什么是环,什么是多边形。 谁能画图解释两个概念的区别? 最佳答案 在 Boost.Geometry 中,多边形被定义
我正在使用 boost.pool,但我不知道何时使用 boost::pool<>::malloc和 boost::pool<>::ordered_malloc ? 所以, boost::pool<>:
我正在尝试通过 *boost::fast_pool_allocator* 使用 *boost::container::flat_set*。但是,我收到编译错误。非常感谢您的意见和建议。为了突出这个问题
sau_timer::sau_timer(int secs, timerparam f) : strnd(io), t(io, boost::posix_time::seconds(secs)
我无法理解此功能的文档,我已多次看到以下内容 tie (ei,ei_end) = out_edges(*(vi+a),g); **g**::out_edge_iterator ei, ei_end;
我想在 C++ 中序列化分层数据结构。我正在处理的项目使用 boost,所以我使用 boost::property_tree::ptree 作为我的数据节点结构。 我们有像 Person 这样的高级结
我需要一些帮助来解决这个异常,我正在实现一个 NPAPI 插件,以便能够使用来自浏览器扩展的本地套接字,为此我正在使用 Firebreath 框架。 对于套接字和连接,我使用带有异步调用的 Boost
我尝试将 boost::bind 与 boost::factory 结合使用但没有成功 我有这个类 Zambas 有 4 个参数(2 个字符串和 2 个整数)和 class Zambas { publ
我是一名优秀的程序员,十分优秀!