cpp/example_cpp_custom_fe/example_cpp_custom_fe.cpp

Demonstrates how to implement a custom front-end that operates with a severity filtering level set at runtime.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        examples/cpp/example_cpp_custom_fe/example_cpp_custom_fe.cpp
00003  *
00004  * Purpose:     C++ example program for Pantheios. Demonstrates:
00005  *
00006  *                - definition of a custom front-end that supports tabbed output
00007  *                - use of pantheios::puts() in bail-out conditions
00008  *
00009  * Created:     31st August 2006
00010  * Updated:     31st August 2006
00011  *
00012  * www:         http://www.pantheios.org/
00013  *
00014  * License:     This source code is placed into the public domain 2006
00015  *              by Synesis Software Pty Ltd. There are no restrictions
00016  *              whatsoever to your use of the software.
00017  *
00018  *              This software is provided "as is", and any warranties,
00019  *              express or implied, of any kind and for any purpose, are
00020  *              disclaimed.
00021  *
00022  * ////////////////////////////////////////////////////////////////////////// */
00023 
00024 
00025 /* Pantheios Header Files */
00026 #include <pantheios/pantheios.hpp>          // Pantheios C++ main header
00027 #include <pantheios/frontend.h>
00028 
00029 /* STLSoft Header Files */
00030 #include <stlsoft/memory/auto_buffer.hpp>   // for stlsoft::auto_buffer
00031 
00032 /* Standard C/C++ Header Files */
00033 #include <exception>                        // for std::exception
00034 #include <string>                           // for std::string
00035 #include <stdlib.h>                         // for exit codes
00036 #include <string.h>                         // for strdup()
00037 
00038 #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
00039 # if defined(STLSOFT_COMPILER_IS_MSVC)
00040 #  pragma warning(disable : 4702)
00041 # endif /* compiler */
00042 #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00043 
00044 /* ////////////////////////////////////////////////////////////////////////// */
00045 
00046 namespace
00047 {
00048   // By default, we will log everything at NOTICE and below (remember that
00049   // they get more serious as the values get lower).
00050 
00051   static int  s_severityCeiling = pantheios::notice;
00052 
00053 } // anonymous namespace
00054 
00055 /* ////////////////////////////////////////////////////////////////////////// */
00056 
00057 // USAGE: [<severity-ceiling>]
00058 //
00059 // where:
00060 // <severity-ceiling> - a number between 0 and 7, which sets the maximum level
00061 //                      displayed, or -1 to suppress all output.
00062 
00063 int main(int argc, char **argv)
00064 {
00065   if(argc > 1)
00066   {
00067     s_severityCeiling = atoi(argv[1]);
00068   }
00069 
00070   try
00071   {
00072     pantheios::log_DEBUG("debug statement");
00073     pantheios::log_INFORMATIONAL("informational statement");
00074     pantheios::log_NOTICE("notice statement");
00075     pantheios::log_WARNING("warning statement");
00076     pantheios::log_ERROR("error statement");
00077     pantheios::log_CRITICAL("critical statement");
00078     pantheios::log_ALERT("alert statement");
00079     pantheios::log_EMERGENCY("emergency statement");
00080 
00081 
00082     return EXIT_SUCCESS;
00083   }
00084   catch(std::bad_alloc &)
00085   {
00086     pantheios::log_CRITICAL("out of memory");
00087   }
00088   catch(std::exception &x)
00089   {
00090     pantheios::log_ALERT("Exception: ", x);
00091   }
00092   catch(...)
00093   {
00094     pantheios::puts(pantheios::emergency, "Unexpected unknown error");
00095   }
00096 
00097   return EXIT_FAILURE;
00098 }
00099 
00100 /* ////////////////////////////////////////////////////////////////////////// */
00101 
00102 PANTHEIOS_CALL(int) pantheios_fe_init(  void    * /* reserved */
00103                                     ,   void    **ptoken)
00104 {
00105   *ptoken = NULL;
00106 
00107   return 0;
00108 }
00109 
00110 PANTHEIOS_CALL(void) pantheios_fe_uninit(void * /* token */)
00111 {}
00112 
00113 PANTHEIOS_CALL(char const*) pantheios_fe_getProcessIdentity(void * /* token */)
00114 {
00115   return "example_cpp_custom_fe";
00116 }
00117 
00118 PANTHEIOS_CALL(int) pantheios_fe_isSeverityLogged(  void    * /* token */
00119                                                 ,   int     severity
00120                                                 ,   int     /* backEndId */)
00121 {
00122   return severity <= s_severityCeiling;
00123 }
00124 
00125 /* ////////////////////////////////////////////////////////////////////// */

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