Is Poco::Logger threadsafe?
Asked Answered
M

1

8

It seems like threadsafe in my test code below. Can I use Poco::Logger in a multithreaded program?

static Poco::Logger *pLogger;    
class MyRunnable : public Poco::Runnable {
   private:
      std::string _name;
      Poco::Random _rnd;
   public:
      void setName(std::string name) {
            _name = name;
         }
      void run() {
         for (int i=0; i<200; i++) {
            pLogger->information("info from: " + _name);
            _rnd.seed(_rnd.next(65532) * _name.size());
            Poco::Thread::sleep(_rnd.next(13) + 1);
         }
      }
};

here is test main:

int
main ( int argc, char *argv[] )
{
   Poco::Thread thr1, thr2, thr3;
   MyRunnable *pMyR1 = new MyRunnable(),
              *pMyR2 = new MyRunnable(),
              *pMyR3 = new MyRunnable();
   pMyR1->setName("r1");
   pMyR2->setName("ra2");
   pMyR3->setName("runable3");

   Poco::FormattingChannel *pFCFile = new Poco::FormattingChannel(new Poco::PatternFormatter("%Y-%m-%d %H:%M:%S.%c %N[%P]:%s: %q:%t"));
   pFCFile->setChannel(new Poco::FileChannel("test.log"));
   pFCFile->open();
   pLogger = &(Poco::Logger::create("FileLogger", pFCFile, Poco::Message::PRIO_INFORMATION));


   thr1.start(*pMyR1);
   thr2.start(*pMyR2);
   thr3.start(*pMyR3);

   std::cout << "starting..." << std::endl;
   thr1.join();
   thr2.join();
   thr3.join();
   std::cout << "end." << std::endl;
   return EXIT_SUCCESS;
}           /* ----------  end of function main  ---------- */
Martinic answered 10/1, 2013 at 3:42 Comment(2)
This page only says unsafeGet is not thread safe, so I presume the rest are.Cowherd
Generally, unless explicitly specified you should always consider functionality as not being thread safe.Vigilantism
L
13

This question is very old, but I had the same doubt, so looking on the library Forum I found: http://pocoproject.org/forum/viewtopic.php?f=12&t=1233&p=2681&hilit=logger#p2681
The important quotation is: "The Logger is thread safe regarding the different logging function. If you try to change the Channel connected to a Logger while another thread is currently using the Logger, this may lead to problems."

Liverish answered 15/1, 2013 at 11:17 Comment(3)
Please summarize links instead of just posting them.Ptyalin
So the quotation of the day is: "The Logger is thread safe regarding the different logging function. If you try to change the Channel connected to a Logger while another thread is currently using the Logger, this may lead to problems."Liverish
Thanks, upvoted. You should modify your post (vs leaving it as a comment).Ptyalin

© 2022 - 2024 — McMap. All rights reserved.