cpp/example_cpp_strings/example_cpp_strings.cpp

Demonstrates the use of Pantheios to log instances of string types in a C++ program.

00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        examples/cpp/example_cpp_strings/example_cpp_strings.cpp
00003  *
00004  * Purpose:     C++ example program for Pantheios. Demonstrates:
00005  *
00006  *                - use of Pantheios logging statements for string types
00007  *                - use of pantheios::puts() in bail-out conditions
00008  *
00009  * Created:     17th May 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 
00028 /* Standard C/C++ Header Files */
00029 #include <exception>                        // for std::exception
00030 #include <string>                           // for std::string
00031 #include <stdlib.h>                         // for exit codes
00032 
00033 /* ////////////////////////////////////////////////////////////////////////// */
00034 
00035 // Define the fe.simple process identity, so that it links if using fe.simple
00036 PANTHEIOS_EXTERN_C const char   FE_SIMPLE_PROCESS_IDENTITY[]    =   "example_cpp_strings";
00037 
00038 /* ////////////////////////////////////////////////////////////////////////// */
00039 
00040 static void concat(std::string const &s1, std::string const &s2, std::string &result)
00041 {
00042     result = s1 + s2;
00043 }
00044 
00045 int main()
00046 {
00047   try
00048   {
00049     std::string   result;
00050     char          s1[]  = "abc";
00051     char const    *s2   = "def";
00052 
00053     // Log two C-style strings
00054 
00055     pantheios::log_NOTICE("Concatenating '", s1, "' and '", s2, "'");
00056 
00057     concat(s1, s2, result);
00058 
00059     // Log two C-style strings and a std::string
00060 
00061     pantheios::log_NOTICE("Concatenation of '", s1, "' and '", s2, "' succeeded; result=", result);
00062 
00063 
00064     return EXIT_SUCCESS;
00065   }
00066   catch(std::bad_alloc &)
00067   {
00068     pantheios::log_CRITICAL("out of memory");
00069   }
00070   catch(std::exception &x)
00071   {
00072     pantheios::log_ALERT("Exception: ", x);
00073   }
00074   catch(...)
00075   {
00076     pantheios::puts(pantheios::emergency, "Unexpected unknown error");
00077   }
00078 
00079   return EXIT_FAILURE;
00080 }
00081 
00082 /* ////////////////////////////////////////////////////////////////////////// */

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