testLoggerConcurrency.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003 
00004 This file is part of MeRMaID.
00005 
00006 MeRMaID is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation, either version 3 of the License, or
00009 (at your option) any later version.
00010 
00011 MeRMaID is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with MeRMaID.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 
00022 /**
00023  * @Filename testLoggerConcurrency.cpp
00024  * @Description Tests for the multithreaded logging framework.
00025  * @Status Finished
00026  * @Version $Id: testLoggerConcurrency.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer Joao Reis (joaocgreis@gmail.com)
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include "ace/Thread.h"
00033 #include <ace/Trace.h>
00034 
00035 
00036 
00037 #include "Logger.hpp"
00038 
00039 using namespace std;
00040 using namespace mermaid::support::logging;
00041 using boost::shared_ptr;
00042 
00043 /** The concurrently accessed logger */
00044 shared_ptr<Logger> logger;
00045 
00046 /**
00047  * @Description This thread will disable and enable the output.
00048  * @Author Joao Reis
00049  */
00050 static void* outputFlicker (void* arg);
00051 
00052 /**
00053  * @Description This thread will disable and enable the prefix.
00054  * @Author Joao Reis
00055  */
00056 static void* prefixFlicker (void* arg);
00057 
00058 /**
00059  * @Description This thread log a lot of messages.
00060  * @Author Joao Reis
00061  */
00062 static void* repeatLogger (void* arg);
00063 
00064 /**
00065  * @Description Logger tests entry point.
00066  * @Author Joao Reis
00067  */
00068 int main()
00069 {
00070   ACE_Trace::stop_tracing();
00071   
00072   logger = ThreadSafeLogger::build();
00073   logger->enableAllLogLevels (TerminalOutput::build());
00074   
00075   ACE_thread_t *threadID = new ACE_thread_t[12];
00076   ACE_hthread_t *threadHandles = new ACE_hthread_t[12];
00077   
00078   ACE_Thread::spawn_n (threadID, //id's for each of the threads
00079                        4,            //number of threads to spawn
00080                        (ACE_THR_FUNC) repeatLogger, //entry point for new thread
00081                        0,                   //args to worker
00082                        THR_JOINABLE | THR_NEW_LWP, //flags
00083                        ACE_DEFAULT_THREAD_PRIORITY,
00084                        0, 0, threadHandles);
00085                        
00086   ACE_Thread::spawn_n (threadID + 4, //id's for each of the threads
00087                        4,            //number of threads to spawn
00088                        (ACE_THR_FUNC) outputFlicker, //entry point for new thread
00089                        0,                   //args to worker
00090                        THR_JOINABLE | THR_NEW_LWP, //flags
00091                        ACE_DEFAULT_THREAD_PRIORITY,
00092                        0, 0, threadHandles + 4);
00093                        
00094   ACE_Thread::spawn_n (threadID + 8, //id's for each of the threads
00095                        4,            //number of threads to spawn
00096                        (ACE_THR_FUNC) prefixFlicker, //entry point for new thread
00097                        0,                   //args to worker
00098                        THR_JOINABLE | THR_NEW_LWP, //flags
00099                        ACE_DEFAULT_THREAD_PRIORITY,
00100                        0, 0, threadHandles + 8);
00101                        
00102                        
00103                        
00104 #if 0
00105   ACE_Thread::spawn ( (ACE_THR_FUNC) repeatLogger);
00106   ACE_Thread::spawn ( (ACE_THR_FUNC) repeatLogger);
00107   ACE_Thread::spawn ( (ACE_THR_FUNC) repeatLogger);
00108   ACE_Thread::spawn ( (ACE_THR_FUNC) repeatLogger);
00109   ACE_Thread::spawn ( (ACE_THR_FUNC) outputFlicker);
00110   ACE_Thread::spawn ( (ACE_THR_FUNC) outputFlicker);
00111   ACE_Thread::spawn ( (ACE_THR_FUNC) outputFlicker);
00112   ACE_Thread::spawn ( (ACE_THR_FUNC) outputFlicker);
00113   ACE_Thread::spawn ( (ACE_THR_FUNC) prefixFlicker);
00114   ACE_Thread::spawn ( (ACE_THR_FUNC) prefixFlicker);
00115   ACE_Thread::spawn ( (ACE_THR_FUNC) prefixFlicker);
00116   ACE_Thread::spawn ( (ACE_THR_FUNC) prefixFlicker);
00117 #endif
00118   
00119   repeatLogger (0);
00120   
00121   for (int i = 0; i < 12; i++)
00122     ACE_Thread::join (threadHandles[i]);
00123     
00124   return 0;
00125 }
00126 
00127 static void* outputFlicker (void* arg)
00128 {
00129   for (int i = 0; i < 1000; i++) {
00130     logger->disableAllLogLevels();
00131     LSTEP (logger, "This is " << i);
00132     logger->enableAllLogLevels (TerminalOutput::build());
00133     LSTEP (logger, "This is " << i);
00134   }
00135   return 0;
00136 }
00137 
00138 static void* prefixFlicker (void* arg)
00139 {
00140   for (int i = 0; i < 1000; i++) {
00141     logger->setPrefix ("PREFIX");
00142     LSTEP (logger, "This is " << i);
00143     logger->setPrefix ("");
00144     LSTEP (logger, "This is " << i);
00145   }
00146   return 0;
00147 }
00148 
00149 static void* repeatLogger (void* arg)
00150 {
00151   for (int i = 0; i < 1000; i++) {
00152     LSTEP (logger, "This is " << i);
00153   }
00154   return 0;
00155 }
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3