cpp/example_cpp_custom_severity_levels/example_cpp_custom_severity_levels.cpp

Demonstrates how to implement a custom back-end with custom severity levels to provide tabbed output.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        examples/cpp/example_cpp_custom_severity_levels/example_cpp_custom_severity_levels.cpp
00003  *
00004  * Purpose:     C++ example program for Pantheios. Demonstrates:
00005  *
00006  *                - use of custom severity level information for tabbing output
00007  *                - definition of a custom back-end that supports tabbed output
00008  *                - use of pantheios::puts() in bail-out conditions
00009  *
00010  * Created:     31st August 2006
00011  * Updated:     2nd September 2006
00012  *
00013  * www:         http://www.pantheios.org/
00014  *
00015  * License:     This source code is placed into the public domain 2006
00016  *              by Synesis Software Pty Ltd. There are no restrictions
00017  *              whatsoever to your use of the software.
00018  *
00019  *              This software is provided "as is", and any warranties,
00020  *              express or implied, of any kind and for any purpose, are
00021  *              disclaimed.
00022  *
00023  * ////////////////////////////////////////////////////////////////////////// */
00024 
00025 
00026 /* Pantheios Header Files */
00027 #include <pantheios/pantheios.hpp>          // Pantheios C++ main header
00028 #include <pantheios/backend.h>
00029 
00030 /* STLSoft Header Files */
00031 #include <stlsoft/memory/auto_buffer.hpp>   // for stlsoft::auto_buffer
00032 
00033 /* Standard C/C++ Header Files */
00034 #include <exception>                        // for std::exception
00035 #include <string>                           // for std::string
00036 #include <stdio.h>                          // for fprintf()
00037 #include <stdlib.h>                         // for exit codes
00038 #include <string.h>                         // for strdup()
00039 
00040 #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
00041 # if defined(STLSOFT_COMPILER_IS_MSVC)
00042 #  pragma warning(disable : 4702)
00043 # endif /* compiler */
00044 #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00045 
00046 /* ////////////////////////////////////////////////////////////////////////// */
00047 
00048 // Define the fe.simple process identity, so that it links when using fe.simple
00049 PANTHEIOS_EXTERN_C const char   FE_SIMPLE_PROCESS_IDENTITY[]    =   "example_cpp_custom_severity_levels";
00050 
00051 /* ////////////////////////////////////////////////////////////////////////// */
00052 
00053 int main()
00054 {
00055   try
00056   {
00057     // Logging at standard "notice" level; Output: "Hello"
00058 
00059     pantheios::log(pantheios::notice, "Hello");
00060 
00061     // Logging at standard "notice" level, with additional severity level
00062     // information; Output: "  Hello"
00063 
00064     pantheios::log(pantheios::notice(2), "Hello");
00065 
00066 
00067     return EXIT_SUCCESS;
00068   }
00069   catch(std::bad_alloc &)
00070   {
00071     pantheios::log_CRITICAL("out of memory");
00072   }
00073   catch(std::exception &x)
00074   {
00075     pantheios::log_ALERT("Exception: ", x);
00076   }
00077   catch(...)
00078   {
00079     pantheios::puts(pantheios::emergency, "Unexpected unknown error");
00080   }
00081 
00082   return EXIT_FAILURE;
00083 }
00084 
00085 /* ////////////////////////////////////////////////////////////////////////// */
00086 
00087 PANTHEIOS_CALL(int) pantheios_be_init(  char const  *processIdentity
00088                                     ,   void        * /* reserved */
00089                                     ,   void        **ptoken)
00090 {
00091   *ptoken = ::strdup(processIdentity);
00092 
00093   return (NULL == *ptoken) ? -2 : 0;
00094 }
00095 
00096 PANTHEIOS_CALL(void) pantheios_be_uninit(void *token)
00097 {
00098   ::free(token);
00099 }
00100 
00101 PANTHEIOS_CALL(int) pantheios_be_logEntry(  void        * /* feToken */
00102                                         ,   void        *beToken
00103                                         ,   int         severity
00104                                         ,   char const  *entry
00105                                         ,   size_t      cchEntry)
00106 {
00107   int     severityLevel   =   severity & 0x07;
00108   int     customInfo24    =   severity >> 8;
00109 
00110   try
00111   {
00112     stlsoft::auto_buffer<char, 256> prefixes(static_cast<size_t>(customInfo24));
00113     char const                      *severity   = pantheios::getSeverityString(severityLevel);
00114     FILE                            *stm        = (severityLevel < pantheios::notice) ? stderr : stdout;
00115     char const                      *processId  = static_cast<char const*>(beToken);
00116 
00117     memset(&prefixes[0], ' ', prefixes.size());
00118 
00119     return ::fprintf( stm
00120                     , "[%s; %s]:%.*s %.*s\n"
00121                     , processId, severity
00122                     , int(prefixes.size()), prefixes.data()
00123                     , int(cchEntry), entry);
00124   }
00125   catch(std::exception &)
00126   {
00127     return 0;
00128   }
00129 }
00130 
00131 /* ////////////////////////////////////////////////////////////////////////// */

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