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.

129 lines
4.4 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/cpp/backends/example.cpp.backends.file.lrsplit/example.cpp.backends.file.lrsplit.cpp
  3. *
  4. * Purpose: C++ example program for Pantheios. Demonstrates:
  5. *
  6. * - use of pantheios_be_file_setFilePath()
  7. * - use of pantheios::logputs() in bail-out conditions
  8. *
  9. * Created: 29th November 2006
  10. * Updated: 7th 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. #define PANTHEIOS_NO_INCLUDE_OS_AND_3PTYLIB_STRING_ACCESS // Faster compilation
  24. /* Pantheios Header Files */
  25. #include <pantheios/pantheios.hpp> // Pantheios C++ main header
  26. #include <pantheios/inserters/args.hpp> // for pantheios::args
  27. #include <pantheios/backends/bec.file.h> // be.file header
  28. /* Standard C/C++ Header Files */
  29. #include <exception> // for std::exception
  30. #include <new> // for std::bad_alloc
  31. #include <string> // for std::string
  32. #include <stdlib.h> // for exit codes
  33. /* ////////////////////////////////////////////////////////////////////// */
  34. /* Define the stock front-end process identity, so that it links when using
  35. * fe.N, fe.simple, etc. */
  36. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("example.cpp.backends.file.lrsplit");
  37. /* ////////////////////////////////////////////////////////////////////// */
  38. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  39. /* ////////////////////////////////////////////////////////////////////// */
  40. int main(int argc, char **argv)
  41. {
  42. // Use of be.file involves several steps:
  43. //
  44. // 1. Linking to the back-end, either explicitly or implicitly
  45. // 2. Setting the log file path for the given back-end(s)
  46. // 3. Making log statements
  47. // 4. Changing the log file path for the given back-end(s)
  48. // 5. Closing the log file for the given back-end(s)
  49. // In this case, linking is performed either by the build makefile or the
  50. // IDE project file, to the be.lrsplit back-end, the bel.file local
  51. // back-end, and the ber.file remote back-end.
  52. //
  53. // In this case, only one back-end is specified, so the back-end
  54. // identifier PANTHEIOS_BEID_ALL (=== 0) is used.
  55. //
  56. // In this case, the log file path is set to 'log.single', and the file is
  57. // truncated with the flag and mask PANTHEIOS_BE_FILE_F_TRUNCATE.
  58. //
  59. // In this case, we explicitly close the log-file, then open it again,
  60. // just for illustrative purposes.
  61. try
  62. {
  63. #ifndef PANTHEIOS_USE_WIDE_STRINGS
  64. pantheios::log_DEBUG("main(", pantheios::args(argc, argv), ")");
  65. #else /* ? !PANTHEIOS_USE_WIDE_STRINGS */
  66. STLSOFT_SUPPRESS_UNUSED(argc); STLSOFT_SUPPRESS_UNUSED(argv);
  67. #endif /* !PANTHEIOS_USE_WIDE_STRINGS */
  68. pantheios::log_NOTICE(PSTR("stmt 1"));
  69. // Set the file name for the local back-end, truncating the
  70. // file's existing contents, if any.
  71. pantheios_be_file_setFilePath(PSTR("local.log"), PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BEID_LOCAL);
  72. pantheios::log_NOTICE(PSTR("stmt 2"));
  73. // Set the file name for the remote back-end, not truncating, so that
  74. // its contents accrue each execution.
  75. pantheios_be_file_setFilePath(PSTR("remote.log"), PANTHEIOS_BEID_REMOTE);
  76. pantheios::log_NOTICE(PSTR("stmt 3"));
  77. // Close all files, by setting the path to NULL.
  78. pantheios_be_file_setFilePath(NULL, PANTHEIOS_BEID_ALL);
  79. pantheios::log_NOTICE(PSTR("stmt 4"));
  80. pantheios::log_DEBUG(PSTR("exiting main()"));
  81. return EXIT_SUCCESS;
  82. }
  83. catch(std::bad_alloc&)
  84. {
  85. pantheios::log(pantheios::alert, PSTR("out of memory"));
  86. }
  87. catch(std::exception& x)
  88. {
  89. pantheios::log_CRITICAL(PSTR("Exception: "), x);
  90. }
  91. catch(...)
  92. {
  93. pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));
  94. }
  95. return EXIT_FAILURE;
  96. }
  97. /* ///////////////////////////// end of file //////////////////////////// */