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.3 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/cpp/backends/example.cpp.backends.callback/example.cpp.backends.callback.cpp
  3. *
  4. * Purpose: C++ example program for Pantheios. Demonstrates:
  5. *
  6. * - use of a back-end library that uses callbacks.
  7. * - use of pantheios::logputs() in bail-out conditions
  8. *
  9. * Created: 25th August 2006
  10. * Updated: 11th 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. #include <pantheios/util/test/compiler_warnings_suppression.first_include.h>
  24. #define PANTHEIOS_NO_INCLUDE_OS_AND_3PTYLIB_STRING_ACCESS // Faster compilation
  25. /* Pantheios Header Files */
  26. #include <pantheios/pantheios.hpp> // Pantheios C++ main header
  27. #include <platformstl/platformstl.h>
  28. #if defined(PLATFORMSTL_OS_IS_UNIX)
  29. # include <pantheios/backends/bec.fprintf.h> // Include the API for bec.fprintf
  30. #elif defined(PLATFORMSTL_OS_IS_WINDOWS)
  31. # include <pantheios/backends/bec.WindowsConsole.h> // Include the API for bec.WindowsConsole
  32. #else /* ? OS */
  33. # error Platform not discriminated
  34. #endif /* OS */
  35. /* Standard C/C++ Header Files */
  36. #include <exception> // for std::exception
  37. #include <new> // for std::bad_alloc
  38. #include <string> // for std::string
  39. #include <stdlib.h> // for exit codes
  40. #if defined(PLATFORMSTL_OS_IS_WINDOWS)
  41. /* Windows Header Files */
  42. # include <windows.h> // for console colour constants
  43. #endif /* PLATFORMSTL_OS_IS_WINDOWS */
  44. #include <pantheios/util/test/compiler_warnings_suppression.last_include.h>
  45. /* ////////////////////////////////////////////////////////////////////// */
  46. /* Define the stock front-end process identity, so that it links when using
  47. * fe.N, fe.simple, etc. */
  48. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("example.cpp.backends.callback");
  49. /* ////////////////////////////////////////////////////////////////////// */
  50. #if defined(PLATFORMSTL_OS_IS_UNIX)
  51. PANTHEIOS_CALL(void) pantheios_be_fprintf_getAppInit(
  52. int /* backEndId */
  53. , pan_be_fprintf_init_t* init) /* throw() */
  54. {
  55. init->flags |= PANTHEIOS_BE_INIT_F_NO_DATETIME; // Don't show date/time
  56. }
  57. #elif defined(PLATFORMSTL_OS_IS_WINDOWS)
  58. PANTHEIOS_CALL(void) pantheios_be_WindowsConsole_getAppInit(
  59. int /* backEndId */
  60. , pan_be_WindowsConsole_init_t* init
  61. ) /* throw() */
  62. {
  63. init->flags |= PANTHEIOS_BE_INIT_F_NO_DATETIME; // Don't show date/time
  64. init->colours[pantheios::debug] = FOREGROUND_BLUE | FOREGROUND_INTENSITY; // Lose the white background
  65. init->colours[pantheios::notice] = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED; // Lose the intensity
  66. }
  67. #else /* ? OS */
  68. # error Platform not discriminated
  69. #endif /* OS */
  70. /* ////////////////////////////////////////////////////////////////////// */
  71. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  72. /* ////////////////////////////////////////////////////////////////////// */
  73. int main()
  74. {
  75. try
  76. {
  77. pantheios::log_DEBUG(PSTR("debug"));
  78. pantheios::log_INFORMATIONAL(PSTR("informational"));
  79. pantheios::log_NOTICE(PSTR("notice"));
  80. pantheios::log_WARNING(PSTR("warning"));
  81. pantheios::log_ERROR(PSTR("error"));
  82. pantheios::log_CRITICAL(PSTR("critical"));
  83. pantheios::log_ALERT(PSTR("alert"));
  84. pantheios::log_EMERGENCY(PSTR("emergency"));
  85. return EXIT_SUCCESS;
  86. }
  87. catch(std::bad_alloc&)
  88. {
  89. pantheios::log(pantheios::alert, PSTR("out of memory"));
  90. }
  91. catch(std::exception& x)
  92. {
  93. pantheios::log_CRITICAL(PSTR("Exception: "), x);
  94. }
  95. catch(...)
  96. {
  97. pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));
  98. }
  99. return EXIT_FAILURE;
  100. }
  101. /* ///////////////////////////// end of file //////////////////////////// */