Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我的程序没有';使用boost库时不支持线程安全日志记录_C++_Multithreading_Boost_Boost Log_Boost Logging - Fatal编程技术网

C++ 我的程序没有';使用boost库时不支持线程安全日志记录

C++ 我的程序没有';使用boost库时不支持线程安全日志记录,c++,multithreading,boost,boost-log,boost-logging,C++,Multithreading,Boost,Boost Log,Boost Logging,我目前正在创建一个用于日志记录的类 但这里有一个问题,它不支持线程同步。当我一个接一个地运行线程时,没有问题。但当我一次运行两个或多个线程时会出现问题 我面临的问题是:“如果我创建两个线程来创建两个单独的日志,但两个线程发生冲突并将其日志消息写入这两个文件” 如果有人发现问题,请帮我解决 BoostLogger.h: #pragma once ...... /////////////////////////////////////// //Defining Macros ////////////

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

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

我面临的问题是:“如果我创建两个线程来创建两个单独的日志,但两个线程发生冲突并将其日志消息写入这两个文件”

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

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)    {};
};
#pragma一次
......
///////////////////////////////////////
//定义宏
///////////////////////////////////////
#定义AddCommonAttr()日志::添加公共属性()
#定义GetLoggingCore()日志::core::get()
#定义日志严重性日志记录::普通::严重性
#定义AddFileLog日志::添加\u文件\u日志
#定义ThreadValueType日志::属性::当前线程\u id::值\u类型
#定义记录日志::记录
#定义提取日志::提取
#定义ExprStream expr::stream
#定义ExprAttr expr::attr
#定义ExprFormatDateTime expr::格式\日期\时间
#定义PosixTimeType boost::posix_时间::ptime
#定义ExprMessage expr::smessage
#定义文件名关键字::文件名
#定义旋转大小关键字::旋转大小
#定义基于时间的旋转关键字::基于时间的旋转
#定义格式关键字::格式
#定义目标关键字::目标
#定义MaxSize关键字::最大大小
#定义最小自由空间关键字::最小自由空间
#定义旋转时间间隔接收器::文件::旋转时间间隔
#定义Reset\u Filter Reset\u Filter/*Reset\u Filter方法删除全局日志记录筛选器*/
#定义Set_Filter Set_Filter/*Set_Filter方法将全局日志记录筛选器设置为处理的每个日志记录*/
#定义SetFormatter集合\格式化程序
#定义记录视图日志::记录视图
#定义格式化ostream日志::格式化ostream
#定义SharedPtr boost::shared\u ptr
#定义MakeShared boost::make_shared
#定义SinkFileBackend接收器::文本\文件\后端
#定义锁定的后端锁定的后端
#定义SetFileCollector集合\u文件\u收集器
#定义MakeCollector接收器::文件::make_收集器
#定义AddSink add_sink/*add_sink方法添加一个新的sink。水槽在添加后立即被包括在日志记录过程中,直到被移除。不能同时多次添加水槽。如果接收器已注册,则忽略该调用*/
#定义RemoveSink remove_sink/*remove_sink方法从输出中删除接收器。删除后,接收器将不会收到任何日志记录。如果接收器未注册,则调用无效*/
#定义RemoveAllSinks remove_all_sinks/*remove_all_sinks方法从输出中删除所有已注册的接收器。删除后,接收器将不会收到任何日志记录*/
#定义齐平齐平
#定义ScanForFiles扫描\u查找\u文件
#定义ScanAll接收器::文件::全部扫描
#定义扫描匹配接收器::文件::扫描匹配
#定义SetExceptionHandler集合\u异常\u处理程序
#定义异常抑制器日志::生成异常抑制器
#定义MakeExceptionHandler日志::生成异常处理程序
typedef-sinks::同步_-sinksink_-type;
静态src::lg;
#定义写日志BOOST_LOG(lg)
/*定义用于写入具有严重性的日志的宏*/
//BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_LOGGER,src::LOGGER_mt)
//静态src::severity\u loggerslg;
#定义LogTrace BOOST\u LOG\u SEV(obj->slg,logging::triple::trace)
#定义LogDebug BOOST\u LOG\u SEV(obj->slg,logging::triple::debug)
#定义LogInfo BOOST\u LOG\u SEV(obj->slg,logging::triple::info)
#定义LogWarning BOOST\u LOG\u SEV(obj->slg,logging::triple::warning)
#定义LogError BOOST\u LOG\u SEV(obj->slg,logging::triple::error)
#定义LogFatal BOOST\u LOG\u SEV(obj->slg,logging::triple::fatal)
#定义1MB(1*1024*1024)
#定义10MB(10*1024*1024)
#定义日期Fmt(“%Y-%b-%d”)
#定义时间段(“\u%H-%M-%S”)
使用名称空间std;
CBoostLogger类
{
私人:
SharedPtrbackend;
SharedPtrsink;
公众:
src::severity\u loggerslg;
CBoostLogger(无效);
~CBoostLogger(无效);
boolstartogger(结构文件格式*sff);
bool停止记录器();
布尔设置过滤器(短严重级别);
bool SetFormat(结构日志格式*sle);
私人:
friend void格式化程序(记录::记录\u视图常量和记录,记录::格式化\u ostream和strm);
};
/*此结构用于设置文件的格式*/
结构文件格式
{
bool包括atetofile;
bool-includetimefile;
字符串文件名;
字符串文件位置;
无符号长旋转;
无符号长最大值;
FileFormat():includedatetofile(false),
IncludeTimeTable(假),
文件名(“日志”),
文件位置(“C:/Log”),
旋转大小(_1MB),
最大大小(_10MB){};
};
结构日志格式
{
bool Set_LineID;
布尔设定时间;
bool集_严重性;
bool Set_ThreadID;
boolset_消息;
LogFormat():设置\u LineID(true),
设置时间(真),
设置_严重性(真),
设置线程ID(真),
设置_消息(真){};
对数形式
#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';
  }
};
#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;
}   
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);

    // ...
}