cpp/example_cpp_blob/example_cpp_blob.cpp

Demonstrates the use of Pantheios to log binary regions in a C++ program, using the pantheios::blob inserter class.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        examples/cpp/example_cpp_blob/example_cpp_blob.cpp
00003  *
00004  * Purpose:     C++ example program for Pantheios. Demonstrates:
00005  *
00006  *                - use of Pantheios inserters for blob types
00007  *                - use of pantheios::puts() in bail-out conditions
00008  *
00009  * Created:     25th 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/inserters/blob.hpp>     // for pantheios::blob
00028 
00029 /* Standard C/C++ Header Files */
00030 #include <exception>                        // for std::exception
00031 #include <string>                           // for std::string
00032 #include <stdlib.h>                         // for exit codes
00033 
00034 /* ////////////////////////////////////////////////////////////////////////// */
00035 
00036 // Define the fe.simple process identity, so that it links when using fe.simple
00037 PANTHEIOS_EXTERN_C const char   FE_SIMPLE_PROCESS_IDENTITY[]    =   "example_cpp_blob";
00038 
00039 /* ////////////////////////////////////////////////////////////////////////// */
00040 
00041 int main()
00042 {
00043   try
00044   {
00045     // Make a blob with some arbitrary values
00046 
00047     pantheios::uint8_t  bytes[20];
00048 
00049     { for(size_t i = 0; i < STLSOFT_NUM_ELEMENTS(bytes); ++i)
00050     {
00051       //bytes[i] = static_cast<pantheios::uint8_t>((i << 8) | (i & 0x0f));
00052       bytes[i] = static_cast<pantheios::uint8_t>(i);
00053     }}
00054 
00055     // Log the blob with default formatting; Output: "bytes: [03020100070605040b0a09080f0e0d0c13121110]"
00056 
00057     pantheios::log_NOTICE("bytes: [", pantheios::blob(bytes, sizeof(bytes)), "]");
00058 
00059 
00060     // Log the blob, splitting into groups of 1 (8-bits) without a separator; Output: "bytes: [000102030405060708090a0b0c0d0e0f10111213]"
00061 
00062     pantheios::log_NOTICE("bytes: [", pantheios::blob(bytes, sizeof(bytes), 1, NULL), "]");
00063 
00064     // Log the blob, splitting into groups of 1 (8-bits) separated by a space; Output: "bytes: [00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13]"
00065 
00066     pantheios::log_NOTICE("bytes: [", pantheios::blob(bytes, sizeof(bytes), 1, " "), "]");
00067 
00068     // Log the blob, splitting into groups of 2 (16-bits), separated by a space; Output: "bytes: [0100 0302 0504 0706 0908 0b0a 0d0c 0f0e 1110 1312]"
00069 
00070     pantheios::log_NOTICE("bytes: [", pantheios::blob(bytes, sizeof(bytes), 2, " "), "]");
00071 
00072     // Log the blob, splitting into groups of 4 (32-bits), separated by a space; Output: "bytes: [03020100 07060504 0b0a0908 0f0e0d0c 13121110]"
00073 
00074     pantheios::log_NOTICE("bytes: [", pantheios::blob(bytes, sizeof(bytes), 4, " "), "]");
00075 
00076     // Log the blob, splitting into groups of 8 (64-bits), separated by a space; Output: "bytes: [0706050403020100 0f0e0d0c0b0a0908 13121110]"
00077 
00078     pantheios::log_NOTICE("bytes: [", pantheios::blob(bytes, sizeof(bytes), 8, " "), "]");
00079 
00080 
00081     // Log the blob, splitting into groups of 1 (8-bits) with a space separator,
00082     // with 4 groups per line, where each line is separated by a line-feed
00083     // and two spaces; Output:
00084     // "bytes: [00 01 02 03
00085     //   04 05 06 07
00086     //   08 09 0a 0b
00087     //   0c 0d 0e 0f
00088     //   10 11 12 13]"
00089 
00090     pantheios::log_NOTICE("bytes: [", pantheios::blob(bytes, sizeof(bytes), 1, " ", 4, "\n  "), "]");
00091 
00092     // Log the blob, splitting into groups of 1 (8-bits) without a separator,
00093     // with 4 groups per line, where each line is separated by a line-feed
00094     // and two spaces; Output:
00095     // "bytes: [03020100-07060504-0b0a0908-0f0e0d0c-13121110]"
00096 
00097     pantheios::log_NOTICE("bytes: [", pantheios::blob(bytes, sizeof(bytes), 4, NULL, 1, "-"), "]");
00098 
00099 
00100     return EXIT_SUCCESS;
00101   }
00102   catch(std::bad_alloc &)
00103   {
00104     pantheios::log_CRITICAL("out of memory");
00105   }
00106   catch(std::exception &x)
00107   {
00108     pantheios::log_ALERT("Exception: ", x);
00109   }
00110   catch(...)
00111   {
00112     pantheios::puts(pantheios::emergency, "Unexpected unknown error");
00113   }
00114 
00115   return EXIT_FAILURE;
00116 }
00117 
00118 
00119 /* ////////////////////////////////////////////////////////////////////////// */

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