Add tons of new loggings
diff --git a/scheduler/random-interval-generator.h b/scheduler/random-interval-generator.h
index bf30156..bd46214 100644
--- a/scheduler/random-interval-generator.h
+++ b/scheduler/random-interval-generator.h
@@ -27,6 +27,7 @@
 #include <boost/random/mersenne_twister.hpp>
 #include <boost/random/uniform_real.hpp>
 #include <boost/random/variate_generator.hpp>
+#include <boost/date_time/posix_time/posix_time_types.hpp>
 
 // generates intervals with uniform distribution
 class RandomIntervalGenerator : public IntervalGenerator
@@ -45,7 +46,8 @@
   // direction shifts the random range; e.g. in the above example, UP would produce a range of
   // 10 ~ 12, DOWN of 8 ~ 10, and EVEN of 9 ~ 11
   RandomIntervalGenerator(double interval, double percent, Direction direction = EVEN)
-  : m_rng(time(NULL))
+  // : m_rng(time(NULL))
+    : m_rng (static_cast<int> (boost::posix_time::microsec_clock::local_time().time_of_day ().total_nanoseconds ()))
   , m_dist(0.0, fractional(percent))
   , m_random(m_rng, m_dist)
   , m_direction(direction)
@@ -54,7 +56,7 @@
   { }
 
   virtual ~RandomIntervalGenerator(){}
-  
+
   virtual double
   nextInterval() _OVERRIDE
   {
@@ -70,7 +72,7 @@
 
     return interval;
   }
-  
+
 private:
   inline double fractional(double x) { double dummy; return abs(modf(x, &dummy)); }
 
diff --git a/scheduler/scheduler.cc b/scheduler/scheduler.cc
index ee6a124..236d5e8 100644
--- a/scheduler/scheduler.cc
+++ b/scheduler/scheduler.cc
@@ -22,10 +22,13 @@
 #include "scheduler.h"
 #include "one-time-task.h"
 #include "periodic-task.h"
+#include "logging.h"
 
 #include <utility>
 #include <boost/make_shared.hpp>
 
+INIT_LOGGER ("Scheduler");
+
 using namespace std;
 using namespace boost;
 
@@ -36,7 +39,7 @@
 
 void errorCallback(int err)
 {
-  cout << "Fatal error: " << err << endl;
+  _LOG_ERROR ("Fatal error: " << err);
 }
 
 Scheduler::Scheduler()
@@ -49,6 +52,7 @@
 
 Scheduler::~Scheduler()
 {
+  shutdown ();
   event_base_free(m_base);
 }
 
@@ -59,14 +63,14 @@
   {
     if (event_base_loop(m_base, EVLOOP_NO_EXIT_ON_EMPTY) < 0)
     {
-      cout << "scheduler loop break error" << endl;
+      _LOG_DEBUG ("scheduler loop break error");
     }
-    
+
     {
-      ReadLock lock(m_mutex);
+      ScopedLock lock(m_mutex);
       if (!m_running)
         {
-          cout << "scheduler loop break normal" << endl;
+          _LOG_DEBUG ("scheduler loop break normal");
           break;
         }
     }
@@ -76,7 +80,7 @@
 void
 Scheduler::start()
 {
-  WriteLock lock(m_mutex);
+  ScopedLock lock(m_mutex);
   if (!m_running)
   {
     m_thread = boost::thread(&Scheduler::eventLoop, this);
@@ -87,13 +91,21 @@
 void
 Scheduler::shutdown()
 {
+  bool breakAndWait = false;
   {
-    WriteLock lock(m_mutex);
-    m_running = false;
+    ScopedLock lock (m_mutex);
+    if (m_running)
+      {
+        m_running = false;
+        breakAndWait = true;
+      }
   }
-  
-  event_base_loopbreak(m_base);
-  m_thread.join();
+
+  if (breakAndWait)
+    {
+      event_base_loopbreak(m_base);
+      m_thread.join();
+    }
 }
 
 TaskPtr
@@ -128,13 +140,13 @@
     int res = evtimer_add(newTask->ev(), newTask->tv());
     if (res < 0)
     {
-      cout << "evtimer_add failed for " << newTask->tag() << endl;
+      _LOG_ERROR ("evtimer_add failed for " << newTask->tag());
     }
     return true;
   }
   else
   {
-    cout << "fail to add task: " << newTask->tag() << endl;
+    _LOG_ERROR ("fail to add task: " << newTask->tag());
   }
 
   return false;
@@ -149,7 +161,7 @@
 void
 Scheduler::rescheduleTask(const TaskPtr &task)
 {
-  ReadLock lock(m_mutex);
+  ScopedLock lock(m_mutex);
   TaskMapIt it = m_taskMap.find(task->tag());
   if (it != m_taskMap.end())
   {
@@ -158,7 +170,7 @@
     int res = evtimer_add(task->ev(), task->tv());
     if (res < 0)
     {
-      cout << "evtimer_add failed for " << task->tag() << endl;
+      _LOG_ERROR ("evtimer_add failed for " << task->tag());
     }
   }
   else
@@ -170,7 +182,7 @@
 void
 Scheduler::rescheduleTask(const Task::Tag &tag)
 {
-  ReadLock lock(m_mutex);
+  ScopedLock lock(m_mutex);
   TaskMapIt it = m_taskMap.find(tag);
   if (it != m_taskMap.end())
   {
@@ -187,7 +199,7 @@
 bool
 Scheduler::addToMap(const TaskPtr &task)
 {
-  WriteLock lock(m_mutex);
+  ScopedLock lock(m_mutex);
   if (m_taskMap.find(task->tag()) == m_taskMap.end())
   {
     m_taskMap.insert(make_pair(task->tag(), task));
@@ -199,7 +211,7 @@
 void
 Scheduler::deleteTask(const Task::Tag &tag)
 {
-  WriteLock lock(m_mutex);
+  ScopedLock lock(m_mutex);
   TaskMapIt it = m_taskMap.find(tag);
   if (it != m_taskMap.end())
   {
@@ -212,7 +224,7 @@
 void
 Scheduler::deleteTask(const Task::TaskMatcher &matcher)
 {
-  WriteLock lock(m_mutex);
+  ScopedLock lock(m_mutex);
   TaskMapIt it = m_taskMap.begin();
   while(it != m_taskMap.end())
   {
@@ -235,6 +247,6 @@
 int
 Scheduler::size()
 {
-  ReadLock lock(m_mutex);
+  ScopedLock lock(m_mutex);
   return m_taskMap.size();
 }
diff --git a/scheduler/scheduler.h b/scheduler/scheduler.h
index e783cf0..6ba826a 100644
--- a/scheduler/scheduler.h
+++ b/scheduler/scheduler.h
@@ -68,7 +68,7 @@
   static TaskPtr
   schedulePeriodicTask (SchedulerPtr scheduler, IntervalGeneratorPtr delayGenerator,
                         const Task::Callback &callback, const Task::Tag &tag);
-  
+
   // if task with the same tag exists, the task is not added and return false
   virtual bool
   addTask(TaskPtr task);
@@ -76,7 +76,7 @@
   // delete task by task->tag, regardless of whether it's invoked or not
   virtual void
   deleteTask(TaskPtr task);
-  
+
   // delete task by tag, regardless of whether it's invoked or not
   // if no task is found, no effect
   virtual void
@@ -121,11 +121,11 @@
   typedef std::map<Task::Tag, TaskPtr> TaskMap;
   typedef std::map<Task::Tag, TaskPtr>::iterator TaskMapIt;
   typedef boost::shared_mutex Mutex;
-  typedef boost::unique_lock<Mutex> WriteLock;
-  typedef boost::shared_lock<Mutex> ReadLock;
+  typedef boost::unique_lock<Mutex> ScopedLock;
+
   TaskMap m_taskMap;
   Mutex m_mutex;
-  bool m_running;
+  volatile bool m_running;
   event_base *m_base;
   boost::thread m_thread;
 };