test_layout_r.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. static const char version[] = "$Id$";
  2. /*
  3. * test_layout.c
  4. *
  5. * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
  6. *
  7. * See the COPYING file for the terms of usage and distribution.
  8. *
  9. *
  10. * Note: there's a known issue with this program double closing the
  11. * test file pointer. This is because the test shares the file with
  12. * the log4c stream appender and they both close it on exiting. In
  13. * the context here this error is benign. The stream2 appender
  14. * does not have this issue as it will not close a passed-in
  15. * file pointer on exit. In general you would not share a file pointer like
  16. * this with an appender so that behaviour of the stream appender is not a
  17. * serious issue.
  18. *
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <log4c/layout_type_basic_r.h>
  24. #include <log4c/appender.h>
  25. #include <log4c/category.h>
  26. #include <log4c/init.h>
  27. #include <log4c/rc.h>
  28. #include <sd/test.h>
  29. #include <sd/factory.h>
  30. #include <stdio.h>
  31. typedef struct {
  32. FILE *out;
  33. } test_layout_r_udata_t;
  34. static log4c_category_t* root = NULL;
  35. static log4c_category_t* sub1 = NULL;
  36. static test_layout_r_udata_t test_udata;
  37. /******************************************************************************/
  38. static void log4c_print(FILE* a_fp)
  39. {
  40. extern sd_factory_t* log4c_category_factory;
  41. extern sd_factory_t* log4c_appender_factory;
  42. extern sd_factory_t* log4c_layout_factory;
  43. sd_factory_print(log4c_category_factory, a_fp); fprintf(a_fp, "\n");
  44. sd_factory_print(log4c_appender_factory, a_fp); fprintf(a_fp, "\n");
  45. sd_factory_print(log4c_layout_factory, a_fp); fprintf(a_fp, "\n");
  46. }
  47. /******************************************************************************/
  48. static int test0(sd_test_t* a_test, int argc, char* argv[])
  49. {
  50. log4c_layout_t* layout1 = log4c_layout_get("layout1");
  51. log4c_appender_t* appender1 = log4c_appender_get("appender1");
  52. test_layout_r_udata_t *test_udata = sd_test_get_udata(a_test);
  53. log4c_layout_set_type(layout1, &log4c_layout_type_basic_r);
  54. log4c_appender_set_layout(appender1, layout1);
  55. test_udata->out = log4c_appender_get_udata(appender1);
  56. log4c_appender_set_udata(appender1, sd_test_out(a_test));
  57. log4c_category_set_appender(sub1, appender1);
  58. log4c_category_set_priority(sub1, LOG4C_PRIORITY_ERROR);
  59. log4c_print(sd_test_out(a_test));
  60. return 1;
  61. }
  62. /******************************************************************************/
  63. static int test1(sd_test_t* a_test, int argc, char* argv[])
  64. {
  65. log4c_category_error(sub1, "let's log");
  66. return 1;
  67. }
  68. /******************************************************************************/
  69. static int test2(sd_test_t* a_test, int argc, char* argv[])
  70. {
  71. log4c_rc->config.bufsize = 32;
  72. log4c_category_error(sub1, "let's log a very long log that whill surely get trimmed because it is way to long");
  73. return 1;
  74. }
  75. /******************************************************************************/
  76. static int test_restore(sd_test_t* a_test, int argc, char* argv[])
  77. {
  78. log4c_appender_t* appender1 = log4c_appender_get("appender1");
  79. test_layout_r_udata_t *test_udata = sd_test_get_udata(a_test);
  80. log4c_appender_set_udata(appender1, test_udata->out);
  81. return 1;
  82. }
  83. /******************************************************************************/
  84. int main(int argc, char* argv[])
  85. {
  86. int ret;
  87. sd_test_t* t = sd_test_new(argc, argv);
  88. /* If we're not using GNU C then initialize our test categories
  89. explicitly
  90. */
  91. root = log4c_category_get("root");
  92. sub1 = log4c_category_get("sub1");
  93. log4c_init();
  94. sd_test_set_udata(t, &test_udata);
  95. sd_test_add(t, test0);
  96. sd_test_add(t, test1);
  97. sd_test_add(t, test2);
  98. /* The test shares the file with the log4c stream appender and they would
  99. * be both closed on exiting. This will restore original file descriptors
  100. * in appenders to prevent double closing. */
  101. sd_test_add(t, test_restore);
  102. ret = sd_test_run(t, argc, argv);
  103. sd_test_delete(t);
  104. log4c_fini();
  105. return ! ret;
  106. }