You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.8 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/cpp/inserters/example.cpp.inserter.blob/example.cpp.inserter.blob.cpp
  3. *
  4. * Purpose: C++ example program for Pantheios. Demonstrates:
  5. *
  6. * - use of Pantheios inserters for blob types
  7. * - use of pantheios::logputs() in bail-out conditions
  8. *
  9. * Created: 25th August 2006
  10. * Updated: 6th December 2010
  11. *
  12. * www: http://www.pantheios.org/
  13. *
  14. * License: This source code is placed into the public domain 2006
  15. * by Synesis Software Pty Ltd. There are no restrictions
  16. * whatsoever to your use of the software.
  17. *
  18. * This software is provided "as is", and any warranties,
  19. * express or implied, of any kind and for any purpose, are
  20. * disclaimed.
  21. *
  22. * ////////////////////////////////////////////////////////////////////// */
  23. /* Pantheios Header Files */
  24. #include <pantheios/pantheios.hpp> // Pantheios C++ main header
  25. #include <pantheios/inserters/blob.hpp> // for pantheios::blob
  26. /* Standard C/C++ Header Files */
  27. #include <exception> // for std::exception
  28. #include <new> // for std::bad_alloc
  29. #include <string> // for std::string
  30. #include <stdlib.h> // for exit codes
  31. /* ////////////////////////////////////////////////////////////////////// */
  32. /* Define the stock front-end process identity, so that it links when using
  33. * fe.N, fe.simple, etc. */
  34. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("example.cpp.inserter.blob");
  35. /* ////////////////////////////////////////////////////////////////////// */
  36. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  37. /* ////////////////////////////////////////////////////////////////////// */
  38. int main()
  39. {
  40. try
  41. {
  42. // Make a blob with some arbitrary values
  43. pantheios::uint8_t bytes[20];
  44. { for(size_t i = 0; i < STLSOFT_NUM_ELEMENTS(bytes); ++i)
  45. {
  46. //bytes[i] = static_cast<pantheios::uint8_t>((i << 8) | (i & 0x0f));
  47. bytes[i] = static_cast<pantheios::uint8_t>(i);
  48. }}
  49. // Log the blob with default formatting; Output: "bytes: [03020100070605040b0a09080f0e0d0c13121110]"
  50. pantheios::log_NOTICE(PSTR("bytes: ["), pantheios::blob(bytes, sizeof(bytes)), PSTR("]"));
  51. // Log the blob, splitting into groups of 1 (8-bits) without a separator; Output: "bytes: [000102030405060708090a0b0c0d0e0f10111213]"
  52. pantheios::log_NOTICE(PSTR("bytes: ["), pantheios::blob(bytes, sizeof(bytes), 1, NULL), PSTR("]"));
  53. // 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]"
  54. pantheios::log_NOTICE(PSTR("bytes: ["), pantheios::blob(bytes, sizeof(bytes), 1, PSTR(" ")), PSTR("]"));
  55. // 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]"
  56. pantheios::log_NOTICE(PSTR("bytes: ["), pantheios::blob(bytes, sizeof(bytes), 2, PSTR(" ")), PSTR("]"));
  57. // Log the blob, splitting into groups of 4 (32-bits), separated by a space; Output: "bytes: [03020100 07060504 0b0a0908 0f0e0d0c 13121110]"
  58. pantheios::log_NOTICE(PSTR("bytes: ["), pantheios::blob(bytes, sizeof(bytes), 4, PSTR(" ")), PSTR("]"));
  59. // Log the blob, splitting into groups of 8 (64-bits), separated by a space; Output: "bytes: [0706050403020100 0f0e0d0c0b0a0908 13121110]"
  60. pantheios::log_NOTICE(PSTR("bytes: ["), pantheios::blob(bytes, sizeof(bytes), 8, PSTR(" ")), PSTR("]"));
  61. // Log the blob, splitting into groups of 1 (8-bits) with a space separator,
  62. // with 4 groups per line, where each line is separated by a line-feed
  63. // and two spaces; Output:
  64. // "bytes: [00 01 02 03
  65. // 04 05 06 07
  66. // 08 09 0a 0b
  67. // 0c 0d 0e 0f
  68. // 10 11 12 13]"
  69. pantheios::log_NOTICE(PSTR("bytes: ["), pantheios::blob(bytes, sizeof(bytes), 1, PSTR(" "), 4, PSTR("\n ")), PSTR("]"));
  70. // Log the blob, splitting into groups of 1 (8-bits) without a separator,
  71. // with 4 groups per line, where each line is separated by a line-feed
  72. // and two spaces; Output:
  73. // "bytes: [03020100-07060504-0b0a0908-0f0e0d0c-13121110]"
  74. pantheios::log_NOTICE(PSTR("bytes: ["), pantheios::blob(bytes, sizeof(bytes), 4, NULL, 1, PSTR("-")), PSTR("]"));
  75. return EXIT_SUCCESS;
  76. }
  77. catch(std::bad_alloc&)
  78. {
  79. pantheios::log(pantheios::alert, PSTR("out of memory"));
  80. }
  81. catch(std::exception& x)
  82. {
  83. pantheios::log_CRITICAL(PSTR("Exception: "), x);
  84. }
  85. catch(...)
  86. {
  87. pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));
  88. }
  89. return EXIT_FAILURE;
  90. }
  91. /* ///////////////////////////// end of file //////////////////////////// */