cpp/example_cpp_hetero1/example_cpp_hetero1.cpp

Demonstrates the use of Pantheios to log instances of heterogeneous types (for which string access shims are defined) in a C++ program.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        examples/cpp/example_cpp_hetero1/example_cpp_hetero1.cpp
00003  *
00004  * Purpose:     C++ example program for Pantheios. Demonstrates:
00005  *
00006  *                - use of implicit support for heterogeneous non-string types
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 
00028 /* STLSoft Header Files */
00029 #include <platformstl/platformstl.hpp>      // PlatformSTL main C++ header
00030 
00031 /* Standard C/C++ Header Files */
00032 #include <exception>                        // for std::exception
00033 #include <string>                           // for std::string
00034 #include <stdlib.h>                         // for exit codes
00035 
00036 #if defined(PLATFORMSTL_OS_IS_UNIX)
00037 # include <dirent.h>
00038 #endif /* OS */
00039 
00040 /* ////////////////////////////////////////////////////////////////////////// */
00041 
00042 // Define the fe.simple process identity, so that it links when using fe.simple
00043 PANTHEIOS_EXTERN_C const char   FE_SIMPLE_PROCESS_IDENTITY[]    =   "example_cpp_hetero1";
00044 
00045 /* ////////////////////////////////////////////////////////////////////////// */
00046 
00047 int main()
00048 {
00049   try
00050   {
00051 
00052 #if defined(PLATFORMSTL_OS_IS_UNIX)
00053 
00054     // Demonstrates handling of heterogeneous types by using UNIX/std
00055     // types: struct tm, struct dirent
00056     //
00057     // Output:
00058     // "Heterogeneous values: tm=Aug 31 15:12:18 2006; de=."
00059 
00060     time_t          t   =   ::time(NULL);
00061     struct tm       *tm =   ::localtime(&t);
00062     DIR             *d  =   ::opendir(".");
00063     struct dirent   *de =   (NULL != d) ? readdir(d) : NULL;
00064 
00065 
00066     pantheios::log_NOTICE("Heterogeneous values: tm=", tm, "; de=", de);
00067 
00068     if(NULL != d)
00069     {
00070       ::closedir(d);
00071     }
00072 
00073 #elif defined(PLATFORMSTL_OS_IS_WIN32)
00074 
00075     // Demonstrates handling of heterogeneous types by using Win32
00076     // types: GUID, SYSTEMTIME, and VARIANT
00077     //
00078     // Output:
00079     // "Heterogeneous values: g={00000000-0000-0000-0000-000000000000}; s=31/08/2006 2:48:41 pm; v=123456789"
00080 
00081     GUID        g   =   GUID_NULL;
00082     SYSTEMTIME  s;
00083     VARIANT     v;
00084 
00085     ::GetLocalTime(&s);
00086 
00087     ::VariantInit(&v);
00088     v.vt    =   VT_I4;
00089     v.lVal  =   123456789;
00090 
00091     pantheios::log_NOTICE("Heterogeneous values: g=", g, "; s=", s, "; v=", v);
00092 
00093 #else /* ? OS */
00094 # error Operating system not discriminated
00095 #endif /* OS */
00096 
00097     return EXIT_SUCCESS;
00098   }
00099   catch(std::bad_alloc &)
00100   {
00101     pantheios::log_CRITICAL("out of memory");
00102   }
00103   catch(std::exception &x)
00104   {
00105     pantheios::log_ALERT("Exception: ", x);
00106   }
00107   catch(...)
00108   {
00109     pantheios::puts(pantheios::emergency, "Unexpected unknown error");
00110   }
00111 
00112   return EXIT_FAILURE;
00113 }
00114 
00115 /* ////////////////////////////////////////////////////////////////////////// */

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