我们可以为 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
变量修改为原子变量(使用std::atomic
类型之一(。
std::atomic<int> count = 0;
我想知道我们是否可以通过执行某些函数或使用add_file_log函数为每个线程创建单独的日志文件。
我不熟悉这个提升库。 如果你想要一个线程局部的变量,你可以使用thread_local
查看此页面
以下程序根据创建的线程数创建日志文件数。但是我们执行相同的代码集的次数相同。这可能会导致大量线程的应用程序变慢
真和假:线程不会因执行相同的代码而减慢速度。线程通过共享资源来减慢彼此的速度。我想到的主要资源是CPU内核和内存。
- 内存
- :实际上,当线程在内存中使用(一次读取/一次写入或两次写入(相同的变量(即使没有锁定/互斥锁(时,它们可能会变慢。 您可以阅读此"消除错误共享"> CPU
- :线程共享 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";
}
在这里,文件名(即参数(每次可能都不同。
- C++使用params创建线程函数会导致转换错误
- 为什么我不能在 while 循环中创建线程?
- 如何创建线程序列以按照启动顺序执行任务?
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- std::使用类成员函数创建线程 - 最佳实践
- CPP:如何使用需要指针的方法创建线程
- 使用 lambda 表达式创建线程时,如何为每个线程提供自己的 lambda 表达式副本
- C++,如何创建线程限制/受保护的变量和函数
- BOOST_LOG_SEV在创建线程后损坏async_read_some
- 为什么当我在构造函数中创建线程时,实例化对象和对象的指针的行为不同
- 如何通过调用类中的函数来创建线程
- 如何在Silverlight本机C 中创建线程
- 通过类构造函数创建线程
- C++:创建线程时出现问题;错误 C2672:"std::invoke":找不到匹配的重载函数
- 尝试在dll main中创建线程
- 如何在Linux上为C和C 创建线程安全LIB
- 如何在 dll 中创建线程 (c++)?
- 从调度队列块 [ swift / c++ ] 创建线程
- 从成员类功能创建线程
- 创建线程,但不要立即在 Linux 中运行它