cpp/example_cpp_wrap_3pty_log_lib/example_cpp_wrap_3pty_log_lib.cpp

Demonstrates use of a custom back-end to wrap the log4cxx library, and interaction between log4cxx constructs and Pantheios log statements.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        examples/cpp/example_cpp_wrap_3pty_log_lib/example_cpp_wrap_3pty_log_lib.cpp
00003  *
00004  * Purpose:     C++ example program for Pantheios. Demonstrates:
00005  *
00006  *                - use of a custom back-end to wrap the log4cxx library
00007  *                - interaction between log4cxx constructs and Pantheios log
00008  *                  statements
00009  *                - use of pantheios::puts() in bail-out conditions
00010  *
00011  * Created:     16th August 2006
00012  * Updated:     1st September 2006
00013  *
00014  * www:         http://www.pantheios.org/
00015  *
00016  * License:     This source code is placed into the public domain 2006
00017  *              by Synesis Software Pty Ltd. There are no restrictions
00018  *              whatsoever to your use of the software.
00019  *
00020  *              This software is provided "as is", and any warranties,
00021  *              express or implied, of any kind and for any purpose, are
00022  *              disclaimed.
00023  *
00024  * ////////////////////////////////////////////////////////////////////////// */
00025 
00026 
00027 /* Pantheios Header Files */
00028 #include <pantheios/pantheios.hpp>          // Pantheios C++ main header
00029 #include <pantheios/backend.h>
00030 
00031 /* log4cxx Header Files */
00032 #include <log4cxx/logger.h>
00033 #include <log4cxx/hierarchy.h>
00034 #include <log4cxx/basicconfigurator.h>
00035 #include <log4cxx/helpers/exception.h>
00036 #include <log4cxx/ndc.h>
00037 
00038 /* Standard C/C++ Header Files */
00039 #include <exception>                        // for std::exception
00040 #include <string>                           // for std::string
00041 #include <stdlib.h>                         // for exit codes
00042 #include <string.h>                         // for strdup()
00043 
00044 #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
00045 # if defined(STLSOFT_COMPILER_IS_MSVC)
00046 #  pragma warning(disable : 4702)
00047 # endif /* compiler */
00048 #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00049 
00050 /* ////////////////////////////////////////////////////////////////////////// */
00051 
00052 int main()
00053 {
00054   try
00055   {
00056     using namespace log4cxx;
00057     using namespace log4cxx::helpers;
00058 
00059     // A normal log statement
00060 
00061     pantheios::log_DEBUG("debug stmt");
00062     pantheios::log_INFORMATIONAL("informational stmt");
00063     pantheios::log_NOTICE("notice stmt");
00064     pantheios::log_WARNING("warning stmt");
00065     pantheios::log_ERROR("error stmt");
00066     pantheios::log_CRITICAL("critical stmt");
00067     pantheios::log_ALERT("alert stmt");
00068     pantheios::log_EMERGENCY("emergency stmt");
00069 
00070 
00071     // Now we push a context ...
00072     NDC::push(_T("trivial context"));
00073 
00074     // ... log ...
00075     pantheios::log_INFORMATIONAL("stmt#2");
00076 
00077     // ... pop ...
00078     NDC::pop();
00079 
00080     // ... and log again
00081     pantheios::log_INFORMATIONAL("stmt#3");
00082 
00083 
00084 
00085     return EXIT_SUCCESS;
00086   }
00087   catch(std::bad_alloc &)
00088   {
00089     pantheios::log_CRITICAL("out of memory");
00090   }
00091   catch(std::exception &x)
00092   {
00093     pantheios::log_ALERT("Exception: ", x);
00094   }
00095   catch(...)
00096   {
00097     pantheios::puts(pantheios::emergency, "Unexpected unknown error");
00098   }
00099 
00100   return EXIT_FAILURE;
00101 }
00102 
00103 /* ////////////////////////////////////////////////////////////////////// */
00104 
00105 PANTHEIOS_CALL(int) pantheios_fe_init(  int     /* reserved */
00106                                     ,   void    ** /* ptoken */)
00107 {
00108   return 0;
00109 }
00110 
00111 PANTHEIOS_CALL(void) pantheios_fe_uninit(void * /* token */)
00112 {}
00113 
00114 PANTHEIOS_CALL(char const*) pantheios_fe_getProcessIdentity(void * /* token */)
00115 {
00116   return "example_cpp_wrap_3pty_log_lib";
00117 }
00118 
00119 PANTHEIOS_CALL(int) pantheios_fe_isSeverityLogged(void * /* token */, int severity, int /* backEndId */)
00120 {
00121   using namespace log4cxx;
00122   using namespace log4cxx::helpers;
00123 
00124   LevelPtr  level;
00125 
00126   switch(severity & 0x07)
00127   {
00128     case  pantheios::PANTHEIOS_SEV_EMERGENCY:
00129     case  pantheios::PANTHEIOS_SEV_ALERT:
00130       level = Level::FATAL;
00131       break;
00132     case  pantheios::PANTHEIOS_SEV_CRITICAL:
00133     case  pantheios::PANTHEIOS_SEV_ERROR:
00134       level = Level::ERROR;
00135       break;
00136     case  pantheios::PANTHEIOS_SEV_WARNING:
00137       level = Level::WARN;
00138       break;
00139     case  pantheios::PANTHEIOS_SEV_NOTICE:
00140     case  pantheios::PANTHEIOS_SEV_INFORMATIONAL:
00141       level = Level::INFO;
00142       break;
00143     case  pantheios::PANTHEIOS_SEV_DEBUG:
00144       level = Level::DEBUG;
00145       break;
00146   }
00147 
00148   LoggerPtr rootLogger = Logger::getRootLogger();
00149 
00150   return rootLogger->isEnabledFor(level);
00151 }
00152 
00153 /* ////////////////////////////////////////////////////////////////////////// */
00154 
00155 PANTHEIOS_CALL(int) pantheios_be_init(char const  * /* processIdentity */
00156                                     , void        * /* reserved */
00157                                     , void        ** /* ptoken */)
00158 {
00159   using namespace log4cxx;
00160   using namespace log4cxx::helpers;
00161 
00162   BasicConfigurator::configure();
00163 
00164   return 0;
00165 }
00166 
00167 PANTHEIOS_CALL(void) pantheios_be_uninit(void * /* token */)
00168 {}
00169 
00170 PANTHEIOS_CALL(int) pantheios_be_logEntry(void        * /* feToken */
00171                                         , void        * /* beToken */
00172                                         , int         severity
00173                                         , char const  *entry
00174                                         , size_t      cchEntry)
00175 {
00176   using namespace log4cxx;
00177   using namespace log4cxx::helpers;
00178 
00179   severity &= 0x07;
00180 
00181   LoggerPtr rootLogger = Logger::getRootLogger();
00182 
00183   switch(severity)
00184   {
00185     case  pantheios::PANTHEIOS_SEV_EMERGENCY:
00186     case  pantheios::PANTHEIOS_SEV_ALERT:
00187       rootLogger->fatal(entry);
00188       break;
00189     case  pantheios::PANTHEIOS_SEV_CRITICAL:
00190     case  pantheios::PANTHEIOS_SEV_ERROR:
00191       rootLogger->error(entry);
00192       break;
00193     case  pantheios::PANTHEIOS_SEV_WARNING:
00194       rootLogger->warn(entry);
00195       break;
00196     case  pantheios::PANTHEIOS_SEV_NOTICE:
00197     case  pantheios::PANTHEIOS_SEV_INFORMATIONAL:
00198       rootLogger->info(entry);
00199       break;
00200     case  pantheios::PANTHEIOS_SEV_DEBUG:
00201       rootLogger->debug(entry);
00202       break;
00203   }
00204 
00205   return cchEntry;
00206 }
00207 
00208 /* ////////////////////////////////////////////////////////////////////// */

pantheios Library documentation © Matthew Wilson, 2006 SourceForge.net Logo