log/threads.cpp

/* This example shows how to use the thread policy to lock a log as needed.
   Currently the only thread policy provided by this library is neutral.
   But others may be defined by the user or added to the library in the
   future.  Calling message, and forced_message already lock, but everything
   else would need to be locked by the user.
*/

#include <toast/log.hpp>

using namespace toast::log;

typedef logger<always_output, to_file<> > Log;

void thread_log_example()
{
  Log log;

  Log::scoped_lock l(log);
  log.open("example.log");
  l.unlock();

  log.message("When you're done here, go check out example.log!");
  
  l.lock();
  log.close();
  log.open("example.log"); // let's pretend we opened a different file :)
  l.unlock();

  log.message("If a thread policy were used here, this would all be thread safe!");
}

SourceForge.net Logo