Overview
Features
Download
Documentation
Community
Add-Ons & Services

Is Logger thread-safe?

Please post support and help requests here.

Is Logger thread-safe?

Postby skane2600y » 04 Nov 2009, 21:19

I'm using the Logger class for file logging from functions in a Windows DLL that can be called from multiple threads. Everything runs fine for a while but then I get a corrupted heap dialog. Is the Logger thread-safe? If so, do you know of any common client programmer mistakes that have lead to this scenario?

Thank you.
skane2600y
 
Posts: 3
Joined: 04 Nov 2009, 21:08

Re: Is Logger thread-safe?

Postby guenter » 05 Nov 2009, 08:20

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.
Regarding possible mistakes - there could be a lot. This is in the nature of multithreaded code. Maybe you forgot to lock a mutex somewhere you're accessing shared data?
guenter
 
Posts: 1165
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Is Logger thread-safe?

Postby skane2600y » 05 Nov 2009, 09:46

Günter,

Dankeschoen.

I'm pretty much following the logger sample except that I'm only using the file logger and some of the setup is spread out:


Code: Select all

Partial AgentLogger class:

AgentLogger::AgentLogger() : pFCFile(new FormattingChannel(new PatternFormatter("%Y-%m-%d %H:%M:%S.%c %q:[%P]:%t"))),
fileLogger(Logger::create("AgentLogger", pFCFile, Message::PRIO_INFORMATION))
   {
      // set up a channel chain to a log file.      
      pFCFile->setChannel(new FileChannel("AgentLogger.log"));
      pFCFile->open();

   }

   AgentLogger::~AgentLogger()
   {
      pFCFile->close();
      Logger::destroy("AgentLogger");
   }
   void AgentLogger::Log(const std::string &msg )
   {
      fileLogger.information(msg);
   }

// client

AgentLog->Log("DataBeforeReceive");



The reason for wrapping the logger rather than using it directly was because I wanted additional information automatically appended to a log message. I took this out when I realized that this additional information was not const.

It's not your job to debug my code, but if you have any comments, they would be welcome.
skane2600y
 
Posts: 3
Joined: 04 Nov 2009, 21:08

Re: Is Logger thread-safe?

Postby skane2600y » 06 Nov 2009, 03:06

I created a critical section and used it to control access to the information call as shown in the code below:

Code: Select all
   void AgentLogger::Log(const std::string &msg )
   {
      EnterCriticalSection(&csLog);

      fileLogger.information(msg);

      LeaveCriticalSection(&csLog);
   }


After the change I no longer encountered heap corruption.
skane2600y
 
Posts: 3
Joined: 04 Nov 2009, 21:08


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron