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

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

本文关键字:创建 线程 单独 日志 文件 boost 我们      更新时间:2023-10-16

我想知道我们是否可以通过执行某些函数或使用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变量修改为原子变量(使用std::atomic类型之一(。

std::atomic<int> count = 0;

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

我不熟悉这个提升库。 如果你想要一个线程局部的变量,你可以使用thread_local查看此页面

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

真和假:线程不会因执行相同的代码而减慢速度。线程通过共享资源来减慢彼此的速度。我想到的主要资源是CPU内核和内存。

    内存
  1. :实际上,当线程在内存中使用(一次读取/一次写入或两次写入(相同的变量(即使没有锁定/互斥锁(时,它们可能会变慢。 您可以阅读此"消除错误共享">
  2. CPU
  3. :线程共享 CPU,一次只能在一个内核上运行一个线程。因此,如果有更多的线程在主动运行,而不是计算机上的内核,它们会相互减慢速度。操作系统必须停止执行一个才能让下一个运行片刻,依此类推......请参阅调度,请参阅上下文切换

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(( 之后; 线程 1 已停止。

是的,可以通过像这样给出单独的名称为每个配置文件创建单独的日志文件,

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";    
}
在这里,文件名

(即参数(每次可能都不同。