poco
poco copied to clipboard
Logger can not display thread id
#define LOG_INFO(logger, msg) logger->information(msg, FILE, LINE) #define LOG_DEBUG(logger, msg) logger->debug(msg, FILE, LINE); #define LOG_WARNING(logger, msg) logger->warning(msg, FILE, LINE) #define LOG_ERROR(logger, msg) logger->error(msg, FILE, LINE) #define LOG_FATAL(logger, msg) logger->fatal(msg, FILE, LINE) #define LOG_DESTROY(name) Poco::Logger::destroy(name) #define LOG_SHUTDOWN() Poco::Logger::shutdown() #define LOG_FMT(fmt, args...) Poco::format(fmt, ##args)
static Poco::Logger* NewFileLogger(const std::string& name, const std::string& level) { Poco::AutoPtrPoco::FileChannel pChannel(new Poco::FileChannel); pChannel->setProperty("rotation", "100M"); pChannel->setProperty("archive", "timestamp"); pChannel->setProperty("path", "./log.log"); Poco::AutoPtrPoco::PatternFormatter patternFormatter( new Poco::PatternFormatter()); patternFormatter->setProperty( "pattern", "[%Y-%m-%e %H:%M:%S:%i %U:%u] [%I:%J][%s:%p]: %t"); patternFormatter->setProperty("times", "local"); Poco::AutoPtrPoco::FormattingChannel formattingChannel( new Poco::FormattingChannel(patternFormatter, pChannel)); auto& log = Poco::Logger::root(); // init root log.setChannel(formattingChannel); log.setLevel(level); return &Poco::Logger::get(name); }
int main(int argc, char** argv) { std::vectorPoco::Logger* logs; logs.resize(3); for (int i = 0; i < 3; i++) { logs[i] = NewFileLogger("root." + std::to_string(i), "debug"); }
std::vectorstd::thread workers; for (int i = 0; i < 10; i++) { workers.emplace_back(&, i { LOG_INFO(logs[i % 3], LOG_FMT("test: %d, tid is: %lu", getpid(), pthread_self())); }); }
for (auto& w : workers) { w.join(); } std::cout << "join" << std::endl;
std::cout << "get tid" << Poco::Thread::currentTid() << std::endl; return 0; }
execute it and the thread id can not be written to file; [2023-03-6 21:01:57:111 ./p_log.cc:56] [0:][root.1:Information]: test: 2132180, tid is: 139880484640512 [2023-03-6 21:01:57:111 ./p_log.cc:56] [0:][root.2:Information]: test: 2132180, tid is: 139880476247808 [2023-03-6 21:01:57:111 ./p_log.cc:56] [0:][root.0:Information]: test: 2132180, tid is: 139880493033216 [2023-03-6 21:01:57:111 ./p_log.cc:56] [0:][root.1:Information]: test: 2132180, tid is: 139880459462400 [2023-03-6 21:01:57:111 ./p_log.cc:56] [0:][root.2:Information]: test: 2132180, tid is: 139880451069696 [2023-03-6 21:01:57:111 ./p_log.cc:56] [0:][root.0:Information]: test: 2132180, tid is: 139880467855104 [2023-03-6 21:01:57:111 ./p_log.cc:56] [0:][root.1:Information]: test: 2132180, tid is: 139880097113856 [2023-03-6 21:01:57:111 ./p_log.cc:56] [0:][root.0:Information]: test: 2132180, tid is: 139880105506560 [2023-03-6 21:01:57:111 ./p_log.cc:56] [0:][root.2:Information]: test: 2132180, tid is: 139880088721152 [2023-03-6 21:01:57:111 ./p_log.cc:56] [0:][root.0:Information]: test: 2132180, tid is: 139880080328448
May be you can try use Poco::Message
and try:
Poco::Message message(module, msg, priority, fileName.c_str(), line);
message.setTid(Poco::Thread::currentOsTid());
The thread number is not automatically obtained.
May be you can try use
Poco::Message
and try:Poco::Message message(module, msg, priority, fileName.c_str(), line); message.setTid(Poco::Thread::currentOsTid());
The thread number is not automatically obtained.
but I can see that before calling logger::log a new message will construct, but I am fuzzed that the tid can't be got
This issue is stale because it has been open for 365 days with no activity.
This issue was closed because it has been inactive for 60 days since being marked as stale.