reSIProcate/stack  9694
testExternalLogger.cxx
Go to the documentation of this file.
00001 #include "rutil/Logger.hxx"
00002 #include "rutil/Data.hxx"
00003 #include "rutil/ThreadIf.hxx"
00004 #include "rutil/Timer.hxx"
00005 
00006 using namespace resip;
00007 using namespace std;
00008 
00009 #ifdef WIN32
00010 #define usleep(x) Sleep(x/1000)
00011 #define sleep(x) Sleep(x*1000)
00012 #endif
00013 
00014 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
00015 
00016 
00017 class LogThread : public ThreadIf
00018 {
00019     public:
00020       LogThread(const Data& description, const Log::ThreadSetting& s)
00021          : mDescription(description),
00022            mSetting(s)
00023       {}
00024 
00025       void thread()
00026       {
00027 #ifdef LOG_ENABLE_THREAD_SETTING
00028          Log::setThreadSetting(mSetting);
00029 #endif
00030          while(!waitForShutdown(100))
00031          {
00032             DebugLog(<< mDescription << "  DEBUG");
00033             InfoLog(<< mDescription << "  INFO");
00034          }
00035       }
00036    private:
00037       Data mDescription;
00038       Log::ThreadSetting mSetting;
00039 };
00040 
00041 int logsInCall()
00042 {
00043    InfoLog(<< "Got here?");
00044    return 17;
00045 }
00046 
00047 
00048 int debugLogsInCall()
00049 {
00050    DebugLog(<< "Got here?");
00051    return 17;
00052 }
00053 
00054 class ExampleExternalLogger : public ExternalLogger
00055 {
00056       virtual bool operator()(Log::Level level,
00057                               const Subsystem& subsystem, 
00058                               const Data& appName,
00059                               const char* file,
00060                               int line,
00061                               const Data& message,
00062                               const Data& messageWithHeaders)
00063       {
00064          resipCerr << "ExampleExternalLogger " << level << " $ "
00065                    << subsystem << " $ " 
00066                    << appName << " $ "
00067                    << file << ":" << line
00068                    << " $ "
00069                    << message << std::endl;
00070 
00071          // supress normal logging
00072          return false;
00073       }
00074 };
00075 
00076 int
00077 main(int argc, char* argv[])
00078 {
00079    ExampleExternalLogger exampleExternalLogger;
00080    Log::initialize(Log::Syslog, Log::Info, argv[0], exampleExternalLogger);
00081 
00082    DebugLog(<<"This should not appear.");
00083    InfoLog(<<"This should appear.");
00084 
00085    LogThread service1a("service1----A", Log::ThreadSetting(1, Log::Debug));
00086    LogThread service1b("service1-------B", Log::ThreadSetting(1, Log::Debug));
00087    LogThread service1c("service1---------C", Log::ThreadSetting(1, Log::Debug));
00088 
00089    LogThread service2a("service2-----------A", Log::ThreadSetting(2, Log::Debug));
00090    LogThread service2b("service2------------------B", Log::ThreadSetting(2, Log::Err));
00091 
00092    service1a.run();
00093    service1b.run();
00094    service1c.run();
00095    service2a.run();
00096    service2b.run();
00097 
00098 #if !defined(WIN32) && !defined(TARGET_OS_MAC)
00099    sleep(2);
00100 
00101    InfoLog(<<"Setting service 1 to INFO\n");
00102    Log::setServiceLevel(1, Log::Info);
00103    sleep(2);
00104 
00105    InfoLog(<<"Setting service 1 to CRIT\n");
00106    Log::setServiceLevel(1, Log::Crit);
00107    sleep(2);
00108 
00109    InfoLog(<<"Setting service 2 to STACK\n");
00110    Log::setServiceLevel(2, Log::Stack);
00111    sleep(2);
00112 
00113    InfoLog(<<"Setting service 1 to DEBUG\n");
00114    Log::setServiceLevel(1, Log::Debug);
00115    sleep(2);
00116 #endif 
00117 
00118    DebugLog(<<"This should still not appear.");
00119    InfoLog(<<"This should still appear.");
00120 
00121    service1a.shutdown();
00122    service1b.shutdown();
00123    service1c.shutdown();
00124    service2a.shutdown();
00125    service2b.shutdown();
00126 
00127    service1a.join();
00128    service1b.join();
00129    service1c.join();
00130    service2a.join();
00131    service2b.join();
00132 
00133    Log::setLevel(Log::Info);
00134 
00135    if (false)
00136    {
00137       UInt64 start = Timer::getTimeMs();
00138       for (int i = 0; i < 10000; i++)
00139       {
00140          InfoLog(<< "string");
00141       }
00142       cerr << "Info Took: " << Timer::getTimeMs() - start << endl;
00143    }
00144 
00145    if (false)
00146    {
00147       UInt64 start = Timer::getTimeMs();
00148       for (int i = 0; i < 10000; i++)
00149       {
00150          DebugLog(<< "string");
00151       }
00152       cerr << "Debug Took: " << Timer::getTimeMs() - start << endl;
00153    }
00154 
00155    InfoLog(<< "Recursive debug: " << debugLogsInCall());
00156    DebugLog(<< "Recursive non-debug OK: " << logsInCall());
00157 
00158    InfoLog(<< "Recursive non-debug OK!: " << logsInCall());
00159    return 0;
00160 }
00161 
00162 /* ====================================================================
00163  * The Vovida Software License, Version 1.0 
00164  * 
00165  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00166  * 
00167  * Redistribution and use in source and binary forms, with or without
00168  * modification, are permitted provided that the following conditions
00169  * are met:
00170  * 
00171  * 1. Redistributions of source code must retain the above copyright
00172  *    notice, this list of conditions and the following disclaimer.
00173  * 
00174  * 2. Redistributions in binary form must reproduce the above copyright
00175  *    notice, this list of conditions and the following disclaimer in
00176  *    the documentation and/or other materials provided with the
00177  *    distribution.
00178  * 
00179  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00180  *    and "Vovida Open Communication Application Library (VOCAL)" must
00181  *    not be used to endorse or promote products derived from this
00182  *    software without prior written permission. For written
00183  *    permission, please contact vocal@vovida.org.
00184  *
00185  * 4. Products derived from this software may not be called "VOCAL", nor
00186  *    may "VOCAL" appear in their name, without prior written
00187  *    permission of Vovida Networks, Inc.
00188  * 
00189  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00190  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00191  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00192  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00193  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00194  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00195  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00196  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00197  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00198  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00199  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00200  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00201  * DAMAGE.
00202  * 
00203  * ====================================================================
00204  * 
00205  * This software consists of voluntary contributions made by Vovida
00206  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00207  * Inc.  For more information on Vovida Networks, Inc., please see
00208  * <http://www.vovida.org/>.
00209  *
00210  */
00211