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.

130 lines
4.4 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/cpp/backends/example.cpp.backends.file/example.cpp.backends.file.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");
  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.file back-end.
  51. //
  52. // In this case, only one back-end is specified, so the back-end
  53. // identifier PANTHEIOS_BEID_ALL (=== 0) is used.
  54. //
  55. // In this case, the log file path is set to 'log.single', and the file is
  56. // truncated with the flag and mask PANTHEIOS_BE_FILE_F_TRUNCATE.
  57. //
  58. // In this case, we explicitly close the log-file, then open it again,
  59. // just for illustrative purposes.
  60. try
  61. {
  62. #ifndef PANTHEIOS_USE_WIDE_STRINGS
  63. pantheios::log_DEBUG("main(", pantheios::args(argc, argv), ")");
  64. #else /* ? !PANTHEIOS_USE_WIDE_STRINGS */
  65. STLSOFT_SUPPRESS_UNUSED(argc); STLSOFT_SUPPRESS_UNUSED(argv);
  66. #endif /* !PANTHEIOS_USE_WIDE_STRINGS */
  67. pantheios::log_NOTICE(PSTR("stmt 1"));
  68. // Set the file name for the only back-end, truncating the file's
  69. // existing contents, if any.
  70. pantheios_be_file_setFilePath(PSTR("single.log"), PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BEID_ALL);
  71. pantheios::log_NOTICE(PSTR("stmt 2"));
  72. // Close the file, by setting the path to NULL.
  73. pantheios_be_file_setFilePath(NULL, PANTHEIOS_BEID_ALL);
  74. pantheios::log_NOTICE(PSTR("stmt 3"));
  75. // (Re)set the file name for the only back-end, and change it to append
  76. // to the file's existing contents. Note that this must be done by
  77. // changing the truncate bit in the mask to 0.
  78. pantheios_be_file_setFilePath(PSTR("single.log"), PANTHEIOS_BE_FILE_F_TRUNCATE, 0, 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 //////////////////////////// */