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.

135 lines
4.5 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/cpp/backends/example.cpp.backends.file.callback/example.cpp.backends.file.callback.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.callback");
  37. /* ////////////////////////////////////////////////////////////////////// */
  38. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  39. #ifdef PANTHEIOS_USE_WIDE_STRINGS
  40. # define pan_strcpy_ wcscpy
  41. #else
  42. # define pan_strcpy_ strcpy
  43. #endif
  44. /* /////////////////////////////////////////////////////////////////////////
  45. * Application-defined functions
  46. */
  47. /** Cause the file to be opened when the application is starting up.
  48. */
  49. PANTHEIOS_CALL(void) pantheios_be_file_getAppInit(
  50. int /* backEndId */
  51. , pan_be_file_init_t* init
  52. ) /* throw() */
  53. {
  54. init->flags |= PANTHEIOS_BE_FILE_F_TRUNCATE; // Truncate the contents
  55. init->flags |= PANTHEIOS_BE_FILE_F_DELETE_IF_EMPTY; // Delete the file if nothing written
  56. init->fileName = pan_strcpy_(init->buff, PSTR("callback-test-%D-%T.log"));
  57. }
  58. /* ////////////////////////////////////////////////////////////////////// */
  59. int main(int argc, char **argv)
  60. {
  61. // Use of be.file involves several steps:
  62. //
  63. // 1. Linking to the back-end, either explicitly or implicitly
  64. // 2. Setting the log file path for the given back-end(s)
  65. // 3. Making log statements
  66. // 4. Changing the log file path for the given back-end(s)
  67. // 5. Closing the log file for the given back-end(s)
  68. // In this case, linking is performed either by the build makefile or the
  69. // IDE project file, to the be.file back-end.
  70. //
  71. // In this case, the log file path via the application-defined callback
  72. // function pantheios_be_file_getAppInit(), which is called during program
  73. // initialisation, prior to main() being called. At the same time, the
  74. // flags PANTHEIOS_BE_FILE_F_TRUNCATE and
  75. // PANTHEIOS_BE_FILE_F_DELETE_IF_EMPTY are specified
  76. //
  77. // In this case, the file is closed automatically during program
  78. // uninitialisation.
  79. try
  80. {
  81. #ifndef PANTHEIOS_USE_WIDE_STRINGS
  82. pantheios::log_DEBUG("main(", pantheios::args(argc, argv), ")");
  83. #else /* ? !PANTHEIOS_USE_WIDE_STRINGS */
  84. STLSOFT_SUPPRESS_UNUSED(argc); STLSOFT_SUPPRESS_UNUSED(argv);
  85. #endif /* !PANTHEIOS_USE_WIDE_STRINGS */
  86. pantheios::log_NOTICE(PSTR("stmt 1"));
  87. pantheios::log_NOTICE(PSTR("stmt 2"));
  88. pantheios::log_NOTICE(PSTR("stmt 3"));
  89. pantheios::log_NOTICE(PSTR("stmt 4"));
  90. pantheios::log_DEBUG(PSTR("exiting main()"));
  91. return EXIT_SUCCESS;
  92. }
  93. catch(std::bad_alloc&)
  94. {
  95. pantheios::log(pantheios::alert, PSTR("out of memory"));
  96. }
  97. catch(std::exception& x)
  98. {
  99. pantheios::log_CRITICAL(PSTR("Exception: "), x);
  100. }
  101. catch(...)
  102. {
  103. pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));
  104. }
  105. return EXIT_FAILURE;
  106. }
  107. /* ///////////////////////////// end of file //////////////////////////// */