繁体   English   中英

我的程序在使用Boost库时不支持线程安全日志记录

[英]My program doesn't support thread safe logging while using boost library

我目前正在创建一个应用于日志记录目的的类。

但是这里有一个问题,它不支持线程同步。 当我一个线程运行一个线程时没有问题。 但是当我一次运行两个或多个线程时出现问题。

我面临的问题是:“如果我创建两个线程来创建两个单独的日志,但是两个线程正在碰撞并将其日志消息写入两个文件”。

如果有人发现问题,请帮助我解决。

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';
  }
};

Source.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,“ Tag”,std :: string)
要么
BOOST_LOG_SCOPED_THREAD_TAG(“标记”,标记_)

有关更多详细信息,您可以在下面进行检查:

Boost.log:当使用add_file_log()函数时,如何防止输出重复到所有添加的流?
由,蒂亚古

我建议您查看boost :: scoped_lock 范围锁文档如果您只是想使每个函数线程都安全,请使用带有互斥锁成员变量的范围锁。 但是,如果要同步线程并运行它们,请查看boost :: condition_variable

我不确定“两个线程碰撞”是什么意思,但是代码的第一个直接问题是您使用的是非线程安全的记录器src::loggersrc::severity_logger 线程安全的对应对象是src::logger_mtsrc::severity_logger_mt (例如,请参见此处 )。

如果要将不同线程生成的日志分成不同的文件,则可以尝试使用基于“ ThreadID”属性的文件名生成器使用text_multifile_backend ,并在初始化时通过logging::add_common_attributes()添加码。 链接页面中有一个示例。

顺便说一句,您不应在每次调用CBoostLogger::StartLogger都调用logging::add_common_attributes() ,这在每个线程中都会发生。 这些属性仅需要添加一次。

由于您要为每个线程创建CBoostLogger ,因此另一种解决方案是为接收器CBoostLogger::StartLogger添加的设置过滤器。 筛选器必须传递具有当前线程ID的记录,并丢弃其余记录。 您可以这样做:

bool CBoostLogger::StartLogger(struct FileFormat *sff )
{
    // ...
    sink = MakeShared < sink_type > (backend);

    // Get the current thread id
    typedef logging::attributes::current_thread_id::value_type thread_id;
    thread_id id = logging::attributes::current_thread_id().get_value().extract_or_throw< thread_id >();

    // Set the filter
    sink->set_filter(expr::attr< thread_id >("ThreadID") == id);

    // ...
}

如果要提高代码的并发性,可以尝试避免在代码中使用全局记录器。 而是在线程本地存储中创建非线程安全的记录器。 根据您的语言版本,您可以使用thread_localboost::thread_specific_ptr 请注意,在后一种情况下,必须确保记录器在首次使用之前已初始化。 在某些情况下,还可以将记录器保持在线程堆栈上。

由于需要从多个线程进行低延迟日志记录,我最近做了一些尝试。

基本要求

就我而言,基本要求是记录事件的时间应固定,时间越短越好。 我见过的许多“多线程”日志记录选项都与此有关(据我所知,log4cpp,boost mt loggers)。

为了确保线程安全,单个记录器中嵌入了某种互斥锁,不会为使用它的每个线程提供最大的记录时间。 当一个线程尝试记录日志时,可能是有十几个其他线程都争用同一个互斥锁。 因此,线程安全不会扩大规模,并且还保证了低延迟。

理想

所需要的是,记录器始终可用于该线程,事件被塞入某种队列中,并且另一端有一个单独的线程将事件从队列中拉出并写入文件或其他内容。

Apache log4cpp

Apache log4cpp与此接近-它具有AsyncAppender。 我说亲密; 尽管它的后端线程调用了连接的其他任何附加程序,但如果没有所有线程都在同一个互斥体之间进行争用,则无法在多个线程之间共享前端。 但是至少写入文件的行为与日志是分离的。 这对于确保线程不会被日志阻止有很大帮助。

Log4cpp

Log4cpp有一个BufferingAppender(在文档中没有提到,但是在代码中)。 它有一个队列,但没有调度程序线程,例如log4cxx的AsyncAppender。 可以用作构建更好模板的模板。

提升日志

这是一堆令人讨厌的可怕丑陋大堆,难以理解并且没有完整记录的代码。 我还无法确切了解它在vis线程中的实际工作以及我的理想状态,但是我认为它的serial_sink可以完成工作。 像Apache log4cxx一样,日志记录前端看起来也使用互斥锁使其线程安全。

ZeroMQ

我在Log4cxx和Boost日志中看到的问题是,没有太多空间可以记录架构灵活性。 我真正想要的是可以使用ZeroMQ执行的模式类型。

我变得确信我需要编写自己的日志记录库。 ZeroMQ具有一些非常好的模式(尤其是PUB / SUB),使用该模式将日志消息从线程推送到中央日志记录线程似乎是一个好主意,特别是考虑到ZeroMQ的体系结构灵活性。

ZeroMQ套接字将充当缓冲区,以吸收突然的日志记录需求,而中央线程则将所有这些都写入磁盘。 只要持续的日志记录速率不超过硬盘驱动器带宽,就可以了。

我可能会尝试将其压缩到log4cpp中,并逐字地将其用作每个线程类别/附加程序与执行实际输出的中央附加程序之间的传输。 但是,这将有些棘手。 ZeroMQ传输字节,而不是对象。 因此,我可能不得不对log4cpp事件进行序列化...这便引发了一个问题,即要使用什么序列化,等等。在我不知道它会变成一个讨厌的,效率低下的庞然大物之前。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM