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.

140 lines
4.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/c/example.c.101/example.c.101.c
  3. *
  4. * Purpose: C example program for introducing the basic essential
  5. * features of Pantheios when using only the C API.
  6. * Demonstrates:
  7. *
  8. * - how the Pantheios libraries must be explicitly
  9. * initialised in a C program; this is not the case in
  10. * C++ programs
  11. * - use of pantheios_logputs()
  12. * - use of pantheios_logprintf()
  13. * - the statement size limitation imposed by
  14. * pantheios_logprintf()
  15. *
  16. * Created: 17th January 2008
  17. * Updated: 7th December 2010
  18. *
  19. * www: http://www.pantheios.org/
  20. *
  21. * License: This source code is placed into the public domain 2006
  22. * by Synesis Software Pty Ltd. There are no restrictions
  23. * whatsoever to your use of the software.
  24. *
  25. * This software is provided "as is", and any warranties,
  26. * express or implied, of any kind and for any purpose, are
  27. * disclaimed.
  28. *
  29. * ////////////////////////////////////////////////////////////////////// */
  30. /* Pantheios Header Files */
  31. #include <pantheios/pantheios.h> /* The root header for Panthieos when using the C-API. */
  32. #include <pantheios/frontends/stock.h> /* Declares the process identity symbol PANTHEIOS_FE_PROCESS_IDENTITY */
  33. /* Standard C Header Files */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. /* /////////////////////////////////////////////////////////////////////////
  38. * Globals
  39. */
  40. const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("example.c.101");
  41. /* /////////////////////////////////////////////////////////////////////////
  42. * main
  43. */
  44. int main()
  45. {
  46. int retCode = EXIT_SUCCESS;
  47. int res;
  48. /* Initialise the Pantheios libraries. This will cause the Core to
  49. * initialise, which will itself initialise the front-end and back-end
  50. * libraries.
  51. *
  52. * Note: This is not required when your program contains 1 or more
  53. * C++ compilation units that #include <pantheios/pantheios.hpp>, since
  54. * that file contains mechanisms within it that cause the initialisation
  55. * to be done automatically. However, it is still best practice to do so
  56. * because that other compilation unit might be removed or replaced with
  57. * a C compilation unit at a future time.
  58. */
  59. res = pantheios_init();
  60. if(0 != res)
  61. {
  62. /* If initialisation failed, we report why using
  63. * pantheios_getInitCodeString()
  64. */
  65. fprintf(stderr, "Failed to initialise the Pantheios libraries: %s\n", pantheios_getInitCodeString(res));
  66. retCode = EXIT_FAILURE;
  67. }
  68. else
  69. {
  70. /* The libraries are now initialised, so we can output diagnostic
  71. * logging statements.
  72. */
  73. PAN_CHAR_T bigBuff[5000];
  74. size_t n;
  75. int i = 10;
  76. double d = 20.20;
  77. PAN_CHAR_T c = 'c';
  78. /* 1. use pantheios_logputs(), which takes a severity level and a
  79. * single string
  80. */
  81. pantheios_logputs(PANTHEIOS_SEV_NOTICE, PANTHEIOS_LITERAL_STRING("pantheios_logputs() can output a single C-style string"));
  82. /* 2. use pantheios_logprintf() to output some fundamental type
  83. * instances
  84. */
  85. pantheios_logprintf(PANTHEIOS_SEV_WARNING, PANTHEIOS_LITERAL_STRING("pantheios_logprintf() uses a format string like this one, and can output all the types compatible with the printf() family, including ints (e.g. i = %d), doubles (e.g. d = %G), and chars (e.g. c = %c)"), i, d, c);
  86. /* 3. use pantheios_logprintf() to illustrate the size limit imposed by
  87. * this API function
  88. *
  89. * Note: the C++ API log() method overloads do not impose a
  90. * statement size limit; nor does pantheios_logputs()
  91. */
  92. for(n = 0; n != (sizeof(bigBuff) / sizeof(0[bigBuff])) - 1; ++n)
  93. {
  94. bigBuff[n] = '-';
  95. }
  96. bigBuff[n] = '\0';
  97. pantheios_logprintf(PANTHEIOS_SEV_WARNING, PANTHEIOS_LITERAL_STRING("NOTE: pantheios_logprintf() output is limited to 4095 characters. The rest of this statement is a very long string of '-' characters, enclosed in quotations. As you can see, it will not be fully emitted: \"%s\""), bigBuff);
  98. /* Uninitialise the Pantheios libraries.
  99. *
  100. * Note: This is not required when your program contains 1 or more
  101. * C++ compilation units that #include <pantheios/pantheios.hpp>,
  102. * since that file contains mechanisms within it that cause the
  103. * initialisation/uninitialisation to occur automatically.
  104. */
  105. pantheios_uninit();
  106. }
  107. return retCode;
  108. }
  109. /* ///////////////////////////// end of file //////////////////////////// */