繁体   English   中英

我们可以为boost库中的每个线程创建单独的日志文件吗?

[英]Can we create separate log files for each thread in boost library

我想知道我们是否可以通过执行某些功能或使用add_file_log函数为每个线程创建单独的日志文件。

以下程序根据创建的线程数创建许多日志文件。 但是我们正在执行相同的代码集相同的次数。 对于大量线程,这可能会导致应用程序速度变慢。

#include <iostream>
#include <boost/move/utility.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <cstring>
#include <stdlib.h>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace keywords = boost::log::keywords;
int count = 0;
class class_logging {
 public:
  void logging_function() {
    char filename[15] = "sample";
    char extension[5] = ".log";
    int c = count++;
    char num[10];
    std::sprintf(num, "%d", c);
    std::strcat(filename, num);
    std::strcat(filename, extension);
    logging::add_file_log(filename);
    logging::add_common_attributes();
    src::logger lg;
    logging::record rec = lg.open_record();
    if (rec) {
      logging::record_ostream strm(rec);
      strm << "Count Value :" << c;
      strm.flush();
      lg.push_record(boost::move(rec));
    }
  }
};
int main(int, char* []) {
  class_logging object[100];
  int total;
  std::cout << "\nEnter no. of threads to produce : ";
  std::cin >> total;
  for (int i = 0; i < total; i++) {
    boost::thread thread1(&class_logging::logging_function, &object[i]);
    std::cout << "\nThread " << i
              << " is created whose id is : " << thread1.get_id();
    thread1.join();
    std::cout << "\nThread " << i << " is done...";
    thread1.detach();
  }
  return 0;
}

Q-1)有什么方法可以动态创建日志文件?

本质上,给定正在执行的单个(此处为线程)任务,将日志写入单独的文件没有任何问题。 如果卷太多,减少日志记录将不仅仅是减少日志文件计数而已。

另一种策略是将线程专用于日志记录。 然后将所有日志消息发送到该线程/任务进行处理。 可以使用生产者/消费者模式来实现。

在日志记录中添加时间戳也是一个好主意。 它将帮助您解决可能需要处理的任何排序问题或日志。


在这行代码中,您似乎对count有一个竞争条件: int c = count++; 增量不是原子的。

我要么在线程创建时将“ count”传递给线程,要么将count变量修改为atomic(使用std::atomic类型之一 )。

std::atomic<int> count = 0;

我想知道我们是否可以通过执行某些功能或使用add_file_log函数为每个线程创建单独的日志文件。

我对Boost Boost不熟悉。 如果您想在线程本地使用变量,可以使用thread_local 参见此页面

以下程序根据创建的线程数创建许多日志文件。 但是我们正在执行相同的代码集相同的次数。 可能会导致大量线程的应用程序运行缓慢

是非题:执行相同的代码不会减慢线程的速度。 线程通过共享资源彼此放慢速度。 我想到的主要资源是cpu核心和内存。

  1. 内存:的确,线程在内存中使用相同的变量(即使没有锁/互斥体)时(一次读取/一次写入或两次写入)都会减慢速度。 您可以阅读此“ 消除虚假共享
  2. Cpu:线程共享cpu,一次只能在一个内核上运行一个线程。 因此,如果有更多线程在运行,而不是计算机上的内核,它们在互相影响。 OS已经停止之一的执行,让下一个片刻运行,等等看调度看上下文切换

Q-1)有什么方法可以动态创建日志文件?

在代码中,您要共享int count ,应使用:

std::atomic<int> count = 0;
...
int c = count.fetch_add(1); // this is thread safe
int c = ++count; // or this

您不必调用thread1.detach(); 然后在您的代码结束。 在thread1.join()之后; thread1已经停止。

是的,可以给每个单独的日志文件创建这样的单独名称,例如:

void ownlogger::start_logger(LPSTR filename, LPSTR file_location)
{
    logging::add_file_log
    (
        keywords::file_name = filename,         /*< file name pattern >*/
        keywords::time_based_rotation = sinks::file::rotation_at_time_interval(boost::posix_time::seconds(6)),
        keywords::format = 
        (
            expr::stream
            //<< std::hex   //To print the LineID in Hexadecimal format
            << std::setw(8) << std::setfill('0') 
            << expr::attr< unsigned int >("LineID")
            << "\t"
            << expr::format_date_time<boost::posix_time::ptime>("TimeStamp","%H:%M:%S.%f")
            << "\t: <" << logging::trivial::severity
            << "> \t" << expr::smessage
        )
    )->locked_backend()->set_file_collector
        ( 
            sinks::file::make_collector
            ( 
            keywords::target = file_location, 
            keywords::max_size = 5 * 1024 * 1024 // just for test limit to 5M
            )
        );

    logging::add_common_attributes();
    WRITE_TO_LOG << "Logger Starts";    
}

在这里,文件名(eargument)可能每次都被赋予不同的名称。

暂无
暂无

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

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