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.

108 lines
4.1 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/cpp/inserters/example.cpp.inserter.pointer/example.cpp.inserter.pointer.cpp
  3. *
  4. * Purpose: C++ example program for Pantheios. Demonstrates:
  5. *
  6. * - use of Pantheios inserters for pointer types
  7. * - use of pantheios::logputs() in bail-out conditions
  8. *
  9. * Created: 25th August 2006
  10. * Updated: 6th August 2012
  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/pointer.hpp> // for pantheios::pointer
  26. #include <pantheios/inserters/hex_ptr.hpp> // for pantheios::hex_ptr
  27. /* Standard C/C++ Header Files */
  28. #include <exception> // for std::exception
  29. #include <new> // for std::bad_alloc
  30. #include <string> // for std::string
  31. #include <stdlib.h> // for exit codes
  32. /* ////////////////////////////////////////////////////////////////////// */
  33. /* Define the stock front-end process identity, so that it links when using
  34. * fe.N, fe.simple, etc. */
  35. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("example.cpp.inserter.pointer");
  36. /* ////////////////////////////////////////////////////////////////////// */
  37. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  38. /* ////////////////////////////////////////////////////////////////////// */
  39. int main()
  40. {
  41. try
  42. {
  43. void* pv = &pv;
  44. // Log a pointer of precise length; Output: "pv: [12fed0]"
  45. pantheios::log_NOTICE(PSTR("pv: ["), pantheios::pointer(pv, 0), PSTR("]"));
  46. // Log a pointer into 8 spaces, right justified; Output: "pv: [ 12fed0]"
  47. pantheios::log_NOTICE(PSTR("pv: ["), pantheios::pointer(pv, 8), PSTR("]"));
  48. // Log a pointer into 8 spaces, left justified; Output: "pv: [12fed0 ]"
  49. pantheios::log_NOTICE(PSTR("pv: ["), pantheios::pointer(pv, -8, pantheios::fmt::hex), PSTR("]"));
  50. // Log a pointer of precise length with 0x prefix; Output: "pv: [0x12fed0]"
  51. pantheios::log_NOTICE(PSTR("pv: ["), pantheios::pointer(pv, 0, pantheios::fmt::zeroXPrefix), PSTR("]"));
  52. // Log a pointer into 8 spaces, right justified, with 0x prefix; Output: "pv: [ 0x12fed0]"
  53. pantheios::log_NOTICE(PSTR("pv: ["), pantheios::pointer(pv, 8, pantheios::fmt::zeroXPrefix), PSTR("]"));
  54. // Log a pointer into 8 spaces, left justified, with 0x prefix; Output: "pv: [0x12fed0 ]"
  55. pantheios::log_NOTICE(PSTR("pv: ["), pantheios::pointer(pv, -8, pantheios::fmt::zeroXPrefix), PSTR("]"));
  56. // Log a pointer of precise length with 0x prefix, a length of 8, and zero-padded; Output: "pv: [0x0012fed0]"
  57. pantheios::log_NOTICE(PSTR("pv: ["), pantheios::pointer(pv, 8, pantheios::fmt::zeroPad | pantheios::fmt::zeroXPrefix), PSTR("]"));
  58. // Log a pointer of precise length with 0x prefix, a length of 8, and zero-padded; Output: "pv: [0x000000000012fed0]"
  59. pantheios::log_NOTICE(PSTR("pv: ["), pantheios::pointer(pv, 16, pantheios::fmt::fullHex), PSTR("]"));
  60. // Log a pointer of precise length with 0x prefix, a length of 8, and zero-padded; Output: "pv: [0x0012fed0]"
  61. pantheios::log_NOTICE(PSTR("pv: ["), pantheios::hex_ptr(pv), PSTR("]"));
  62. return EXIT_SUCCESS;
  63. }
  64. catch(std::bad_alloc&)
  65. {
  66. pantheios::log(pantheios::alert, PSTR("out of memory"));
  67. }
  68. catch(std::exception& x)
  69. {
  70. pantheios::log_CRITICAL(PSTR("Exception: "), x);
  71. }
  72. catch(...)
  73. {
  74. pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));
  75. }
  76. return EXIT_FAILURE;
  77. }
  78. /* ///////////////////////////// end of file //////////////////////////// */