test_rollingfile_appender.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * test_rollingfile_appender.c
  3. *
  4. * This file demonstrates how to programatically use the rolling
  5. * file appender. See also the API documentation for the
  6. * appender_type_rollingfile.h file.
  7. *
  8. * It is possible to be more terse here if you accept the default values,
  9. * but we spell it out explicitly.
  10. *
  11. * See the COPYING file for the terms of usage and distribution.
  12. */
  13. #ifdef HAVE_CONFIG_H
  14. #include "config.h"
  15. #endif
  16. #include <stdio.h>
  17. #include <log4c.h>
  18. #include <log4c/appender_type_rollingfile.h>
  19. #include <log4c/rollingpolicy.h>
  20. #include <log4c/rollingpolicy_type_sizewin.h>
  21. /*********************** Parameters **********************************
  22. *
  23. * params could be taken from the command line to facillitate testing
  24. *
  25. */
  26. /*
  27. * rolling file specific params
  28. */
  29. #ifdef _WIN32
  30. #define FILE_SEP "\\"
  31. #else
  32. #define FILE_SEP "/"
  33. #endif
  34. char *param_log_dir = ROLLINGFILE_DEFAULT_LOG_DIR;
  35. char* param_log_prefix = ROLLINGFILE_DEFAULT_LOG_PREFIX"rf";
  36. long param_max_file_size = 1666;
  37. long param_max_num_files = 6;
  38. /*
  39. * Other Logging params
  40. * xxx Problem with dated layout on windows: assert failure
  41. * in gmtime call.
  42. */
  43. char *param_layout_to_use = "dated"; /* could also be "basic" */
  44. /*******************************************************************
  45. *
  46. * Globals
  47. *
  48. */
  49. log4c_category_t* root = NULL;
  50. log4c_appender_t* file_appender = NULL;
  51. /******************************************************************************/
  52. /*
  53. * Init log4c and configure a rolling file appender
  54. *
  55. */
  56. static void init_log4c_with_rollingfile_appender(){
  57. int rc = 2;
  58. rollingfile_udata_t *rfup = NULL;
  59. log4c_rollingpolicy_t *policyp = NULL;
  60. rollingpolicy_sizewin_udata_t *sizewin_udatap = NULL;
  61. printf("using the rolling file appender "
  62. "to write test log files\n"
  63. "to log directory '%s', log file prefix is '%s'"
  64. ", max file size is '%ld'\n"
  65. "max num files is '%ld'\n",
  66. param_log_dir, param_log_prefix, param_max_file_size,
  67. param_max_num_files);
  68. if ( (rc = log4c_init()) == 0 ) {
  69. printf("log4c init success\n");
  70. } else {
  71. printf("log4c init failed--error %d\n", rc);
  72. return;
  73. }
  74. /*
  75. * Get a reference to the root category
  76. */
  77. root = log4c_category_get("root");
  78. log4c_category_set_priority(root,
  79. LOG4C_PRIORITY_WARN);
  80. /*
  81. * Get a file appender and set the type to rollingfile
  82. */
  83. file_appender = log4c_appender_get("aname");
  84. log4c_appender_set_type(file_appender,
  85. log4c_appender_type_get("rollingfile"));
  86. /*
  87. * Make a rolling file udata object and set the basic parameters
  88. */
  89. rfup = rollingfile_make_udata();
  90. rollingfile_udata_set_logdir(rfup, param_log_dir);
  91. rollingfile_udata_set_files_prefix(rfup, param_log_prefix);
  92. /*
  93. * Get a new rollingpolicy
  94. * type defaults to "sizewin" but set the type explicitly here
  95. * to show how to do it.
  96. */
  97. policyp = log4c_rollingpolicy_get("a_policy_name");
  98. log4c_rollingpolicy_set_type(policyp,
  99. log4c_rollingpolicy_type_get("sizewin"));
  100. /*
  101. * Get a new sizewin policy type and configure it.
  102. * Then attach it to the policy object.
  103. */
  104. sizewin_udatap = sizewin_make_udata();
  105. sizewin_udata_set_file_maxsize(sizewin_udatap, param_max_file_size);
  106. sizewin_udata_set_max_num_files(sizewin_udatap, param_max_num_files);
  107. log4c_rollingpolicy_set_udata(policyp,sizewin_udatap);
  108. /*
  109. * Now set that policy in our rolling file appender udata.
  110. */
  111. rollingfile_udata_set_policy(rfup, policyp);
  112. log4c_appender_set_udata(file_appender, rfup);
  113. /*
  114. * Allow the rolling policy to initialize itself:
  115. * it needs to know the rollingfile udata it is associated with--it
  116. * picks up some parameters in there
  117. */
  118. log4c_rollingpolicy_init(policyp, rfup);
  119. /*
  120. * Configure a layout for the rolling file appender
  121. */
  122. log4c_appender_set_layout(file_appender,
  123. log4c_layout_get(param_layout_to_use) );
  124. /*
  125. * Configure the root category with our rolling file appender...
  126. * and we can then start logging to it.
  127. *
  128. */
  129. log4c_category_set_appender(root,file_appender );
  130. log4c_dump_all_instances(stderr);
  131. }
  132. static int test0(int argc, char* argv[])
  133. {
  134. int i = 0;
  135. init_log4c_with_rollingfile_appender();
  136. for (i = 0; i<200; i++){
  137. log4c_category_fatal(root,
  138. "%d%d%d%d%d%d%d%d%d%d%d%d%d\n",i,i,i,i,i,i,i,i,i,i,i,i,i);
  139. }
  140. /* Explicitly call the log4c cleanup routine */
  141. if ( log4c_fini()){
  142. printf("log4c_fini() failed");
  143. }
  144. return 1;
  145. }
  146. /******************************************************************************/
  147. int main(int argc, char* argv[])
  148. {
  149. test0(argc,argv);
  150. return(0);
  151. }