rollingpolicy.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * rollingpolicy.c
  3. *
  4. *
  5. * See the COPYING file for the terms of usage and distribution.
  6. */
  7. #include <log4c/appender.h>
  8. #include <log4c/appender_type_rollingfile.h>
  9. #include <log4c/rollingpolicy.h>
  10. #include <log4c/rollingpolicy_type_sizewin.h>
  11. #include <string.h>
  12. #include <sd/malloc.h>
  13. #include <sd/factory.h>
  14. #include <sd/hash.h>
  15. #include <sd/factory.h>
  16. #include <sd/error.h>
  17. /* Internal struct that defines the conf and the state info
  18. * for an instance of the appender_type_fileroller type.
  19. */
  20. struct __log4c_rollingpolicy
  21. {
  22. char* policy_name;
  23. const log4c_rollingpolicy_type_t* policy_type;
  24. void * policy_udata;
  25. rollingfile_udata_t* policy_rfudatap;
  26. #define PFLAGS_IS_INITIALIZED 0x0001
  27. int policy_flags;
  28. };
  29. sd_factory_t* log4c_rollingpolicy_factory = NULL;
  30. //////////////////////////////////////////////////////////////////////////
  31. ///以下为jesse修改的代码,将原有代码用#if 0...#endif注释掉
  32. // 为解决log4c_layout_types中内存泄漏而将静态局部变量提升为全局局部变量 [9/8/2008 jesse]
  33. static sd_hash_t* rollingpolicy_c_gs_types = NULL;
  34. static sd_hash_t* log4c_rollingpolicy_types(void)
  35. {
  36. ///static sd_hash_t* types = NULL;
  37. if (!rollingpolicy_c_gs_types)
  38. rollingpolicy_c_gs_types = sd_hash_new(20, NULL);
  39. return rollingpolicy_c_gs_types;
  40. }
  41. /** 删除本rollingpolicy.c文件中定义的全局和静态的指针指向的内存,防止内存泄漏
  42. 如:
  43. 1.rollingpolicy_c_gs_types.
  44. 2.
  45. @return void.
  46. 作者:jesse 日期:2008.09.08
  47. */
  48. extern void log4c_rollingpolicy_delete_global()
  49. {
  50. if (NULL != rollingpolicy_c_gs_types)
  51. {
  52. sd_hash_delete(rollingpolicy_c_gs_types);
  53. rollingpolicy_c_gs_types = NULL;
  54. }
  55. }
  56. //////////////////////////////////////////////////////////////////////////
  57. #if 0
  58. /*******************************************************************************/
  59. static sd_hash_t* log4c_rollingpolicy_types(void){
  60. static sd_hash_t* types = NULL;
  61. if (!types)
  62. types = sd_hash_new(20, NULL);
  63. return types;
  64. }
  65. #endif
  66. extern void log4c_rollingpolicy_types_print(FILE *fp)
  67. {
  68. sd_hash_iter_t* i;
  69. fprintf(fp, "rollingpolicy types:");
  70. for (i = sd_hash_begin(log4c_rollingpolicy_types());
  71. i != sd_hash_end(log4c_rollingpolicy_types());
  72. i = sd_hash_iter_next(i) )
  73. {
  74. fprintf(fp, "'%s' ",((log4c_rollingpolicy_type_t *)(i->data))->name );
  75. }
  76. fprintf(fp, "\n");
  77. }
  78. /*******************************************************************************/
  79. LOG4C_API log4c_rollingpolicy_t* log4c_rollingpolicy_get(const char* a_name)
  80. {
  81. static const sd_factory_ops_t log4c_rollingpolicy_factory_ops = {
  82. /*(void*) log4c_rollingpolicy_new,
  83. (void*) log4c_rollingpolicy_delete,
  84. (void*) log4c_rollingpolicy_print,*/
  85. (void *( *)(const char *))log4c_rollingpolicy_new,
  86. (void ( *)(void *))log4c_rollingpolicy_delete,
  87. (void ( *)(void *,FILE *))log4c_rollingpolicy_print
  88. };
  89. if (!log4c_rollingpolicy_factory) {
  90. log4c_rollingpolicy_factory =
  91. sd_factory_new("log4c_rollingpolicy_factory",
  92. &log4c_rollingpolicy_factory_ops);
  93. /* build default rollingpolicy
  94. log4c_rollingpolicy_set_udata(log4c_appender_get("stderr"), stderr);
  95. log4c_appender_set_udata(log4c_appender_get("stdout"), stdout);*/
  96. }
  97. return (log4c_rollingpolicy_t*)sd_factory_get(log4c_rollingpolicy_factory, a_name);
  98. }
  99. /****************************************************************************/
  100. LOG4C_API log4c_rollingpolicy_t* log4c_rollingpolicy_new(const char* a_name){
  101. log4c_rollingpolicy_t* ptrThis;
  102. sd_debug("log4c_rollingpolicy_new[ ");
  103. if (!a_name)
  104. return NULL;
  105. sd_debug("new policy name='%s'", a_name);
  106. ptrThis = (log4c_rollingpolicy_t*)sd_calloc(1, sizeof(log4c_rollingpolicy_t));
  107. ptrThis->policy_name = sd_strdup(a_name);
  108. ptrThis->policy_type = &log4c_rollingpolicy_type_sizewin;
  109. ptrThis->policy_udata = NULL;
  110. ptrThis->policy_rfudatap = NULL;
  111. ptrThis->policy_flags = 0;
  112. sd_debug("]");
  113. return ptrThis;
  114. }
  115. /*******************************************************************************/
  116. LOG4C_API void log4c_rollingpolicy_delete(log4c_rollingpolicy_t* ptrThis)
  117. {
  118. sd_debug("log4c_rollingpolicy_delete['%s'",
  119. (ptrThis && ptrThis->policy_name ? ptrThis->policy_name: "(no name)"));
  120. if (!ptrThis){
  121. goto log4c_rollingpolicy_delete_exit;
  122. }
  123. if (log4c_rollingpolicy_fini(ptrThis)){
  124. sd_error("failed to fini rollingpolicy");
  125. goto log4c_rollingpolicy_delete_exit;
  126. }
  127. if (ptrThis->policy_name){
  128. sd_debug("freeing policy name");
  129. free(ptrThis->policy_name);
  130. ptrThis->policy_name = NULL;
  131. };
  132. sd_debug("freeing ptrThis rolling policy instance");
  133. free(ptrThis);
  134. log4c_rollingpolicy_delete_exit:
  135. sd_debug("]");
  136. }
  137. /*******************************************************************************/
  138. LOG4C_API int log4c_rollingpolicy_init(log4c_rollingpolicy_t *ptrThis, rollingfile_udata_t* rfup){
  139. int rc = 0;
  140. if (!ptrThis)
  141. return -1;
  142. ptrThis->policy_rfudatap = rfup;
  143. if (!ptrThis->policy_type)
  144. return 0;
  145. if (!ptrThis->policy_type->init)
  146. return 0;
  147. rc = ptrThis->policy_type->init(ptrThis, rfup);
  148. ptrThis->policy_flags |= PFLAGS_IS_INITIALIZED;
  149. return rc;
  150. }
  151. LOG4C_API int log4c_rollingpolicy_fini(log4c_rollingpolicy_t *ptrThis){
  152. int rc = 0;
  153. sd_debug("log4c_rollingpolicy_fini['%s'",
  154. (ptrThis && ptrThis->policy_name ? ptrThis->policy_name: "(no name")) ;
  155. if (!ptrThis){
  156. rc = 0;
  157. } else {
  158. if (ptrThis->policy_flags & PFLAGS_IS_INITIALIZED){
  159. if (ptrThis->policy_type){
  160. rc = ptrThis->policy_type->fini(ptrThis);
  161. }
  162. }
  163. if (!rc){
  164. ptrThis->policy_flags &= ~PFLAGS_IS_INITIALIZED;
  165. }else{
  166. sd_debug("Call to rollingpolicy fini failed");
  167. }
  168. }
  169. sd_debug("]");
  170. return rc;
  171. }
  172. /*******************************************************************************/
  173. LOG4C_API int log4c_rollingpolicy_is_triggering_event(log4c_rollingpolicy_t* ptrThis, const log4c_logging_event_t* a_event, long current_fs){
  174. if (!ptrThis)
  175. return -1;
  176. if (!ptrThis->policy_type)
  177. return 0;
  178. if (!ptrThis->policy_type->is_triggering_event)
  179. return 0;
  180. return ptrThis->policy_type->is_triggering_event(ptrThis, a_event, current_fs);
  181. }
  182. /*******************************************************************************/
  183. LOG4C_API int log4c_rollingpolicy_rollover(log4c_rollingpolicy_t* ptrThis, FILE **fpp){
  184. if (!ptrThis)
  185. return -1;
  186. if (!ptrThis->policy_type)
  187. return 0;
  188. if (!ptrThis->policy_type->rollover)
  189. return 0;
  190. return ptrThis->policy_type->rollover(ptrThis, fpp);
  191. }
  192. /*******************************************************************************/
  193. LOG4C_API void* log4c_rollingpolicy_get_udata(const log4c_rollingpolicy_t* ptrThis){
  194. return (ptrThis ? ptrThis->policy_udata : NULL);
  195. }
  196. /*******************************************************************************/
  197. LOG4C_API void* log4c_rollingpolicy_get_name(const log4c_rollingpolicy_t* ptrThis){
  198. return (ptrThis ? ptrThis->policy_name : NULL);
  199. }
  200. /*******************************************************************************/
  201. LOG4C_API rollingfile_udata_t* log4c_rollingpolicy_get_rfudata(const log4c_rollingpolicy_t* ptrThis){
  202. return (ptrThis ? ptrThis->policy_rfudatap : NULL);
  203. }
  204. /*******************************************************************************/
  205. LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_type_set( const log4c_rollingpolicy_type_t* a_type){
  206. sd_hash_iter_t* i = NULL;
  207. void* previous = NULL;
  208. if (!a_type)
  209. return NULL;
  210. if ( (i = sd_hash_lookadd(log4c_rollingpolicy_types(), a_type->name)) == NULL)
  211. return NULL;
  212. previous = i->data;
  213. i->data = (void*) a_type;
  214. return (log4c_rollingpolicy_type_t*)previous;
  215. }
  216. /*****************************************************************************/
  217. LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_type_get( const char* a_name){
  218. sd_hash_iter_t* i;
  219. if (!a_name)
  220. return NULL;
  221. if ( (i = sd_hash_lookup(log4c_rollingpolicy_types(), a_name)) != NULL)
  222. return (log4c_rollingpolicy_type_t*)i->data;
  223. return NULL;
  224. }
  225. /*******************************************************************************/
  226. LOG4C_API void log4c_rollingpolicy_set_udata(log4c_rollingpolicy_t* ptrThis, void *udatap){
  227. if ( ptrThis) {
  228. ptrThis->policy_udata = udatap;
  229. }
  230. }
  231. /*******************************************************************************/
  232. LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_set_type( log4c_rollingpolicy_t* a_rollingpolicy, const log4c_rollingpolicy_type_t* a_type){
  233. const log4c_rollingpolicy_type_t* previous;
  234. if (!a_rollingpolicy)
  235. return NULL;
  236. previous = a_rollingpolicy->policy_type;
  237. a_rollingpolicy->policy_type = a_type;
  238. return previous;
  239. }
  240. /*******************************************************************************/
  241. LOG4C_API void log4c_rollingpolicy_print(const log4c_rollingpolicy_t* ptrThis, FILE* a_stream)
  242. {
  243. if (!ptrThis)
  244. return;
  245. fprintf(a_stream, "{ name:'%s' udata:%p}",
  246. ptrThis->policy_name,
  247. ptrThis->policy_udata);
  248. }
  249. LOG4C_API int log4c_rollingpolicy_is_initialized(log4c_rollingpolicy_t* ptrThis){
  250. if (!ptrThis)
  251. return(0);
  252. return( ptrThis->policy_flags & PFLAGS_IS_INITIALIZED);
  253. }