rollingpolicy.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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_free( void ) {
  67. sd_hash_t * types = log4c_rollingpolicy_types();
  68. if ( types != NULL ) {
  69. sd_hash_delete( types );
  70. }
  71. }
  72. extern void log4c_rollingpolicy_types_print(FILE *fp)
  73. {
  74. sd_hash_iter_t* i;
  75. fprintf(fp, "rollingpolicy types:");
  76. for (i = sd_hash_begin(log4c_rollingpolicy_types());
  77. i != sd_hash_end(log4c_rollingpolicy_types());
  78. i = sd_hash_iter_next(i) )
  79. {
  80. fprintf(fp, "'%s' ",((log4c_rollingpolicy_type_t *)(i->data))->name );
  81. }
  82. fprintf(fp, "\n");
  83. }
  84. /*******************************************************************************/
  85. LOG4C_API log4c_rollingpolicy_t* log4c_rollingpolicy_get(const char* a_name)
  86. {
  87. static const sd_factory_ops_t log4c_rollingpolicy_factory_ops = {
  88. /*(void*) log4c_rollingpolicy_new,
  89. (void*) log4c_rollingpolicy_delete,
  90. (void*) log4c_rollingpolicy_print,*/
  91. (void *( *)(const char *))log4c_rollingpolicy_new,
  92. (void ( *)(void *))log4c_rollingpolicy_delete,
  93. (void ( *)(void *,FILE *))log4c_rollingpolicy_print
  94. };
  95. if (!log4c_rollingpolicy_factory) {
  96. log4c_rollingpolicy_factory =
  97. sd_factory_new("log4c_rollingpolicy_factory",
  98. &log4c_rollingpolicy_factory_ops);
  99. /* build default rollingpolicy
  100. log4c_rollingpolicy_set_udata(log4c_appender_get("stderr"), stderr);
  101. log4c_appender_set_udata(log4c_appender_get("stdout"), stdout);*/
  102. }
  103. return (log4c_rollingpolicy_t*)sd_factory_get(log4c_rollingpolicy_factory, a_name);
  104. }
  105. /****************************************************************************/
  106. LOG4C_API log4c_rollingpolicy_t* log4c_rollingpolicy_new(const char* a_name){
  107. log4c_rollingpolicy_t* this;
  108. sd_debug("log4c_rollingpolicy_new[ ");
  109. if (!a_name)
  110. return NULL;
  111. sd_debug("new policy name='%s'", a_name);
  112. this = sd_calloc(1, sizeof(log4c_rollingpolicy_t));
  113. this->policy_name = sd_strdup(a_name);
  114. this->policy_type = &log4c_rollingpolicy_type_sizewin;
  115. this->policy_udata = NULL;
  116. this->policy_rfudatap = NULL;
  117. this->policy_flags = 0;
  118. sd_debug("]");
  119. return this;
  120. }
  121. /*******************************************************************************/
  122. LOG4C_API void log4c_rollingpolicy_delete(log4c_rollingpolicy_t* this)
  123. {
  124. sd_debug("log4c_rollingpolicy_delete['%s'",
  125. (this && this->policy_name ? this->policy_name: "(no name)"));
  126. if (!this){
  127. goto log4c_rollingpolicy_delete_exit;
  128. }
  129. if (log4c_rollingpolicy_fini(this)){
  130. sd_error("failed to fini rollingpolicy");
  131. goto log4c_rollingpolicy_delete_exit;
  132. }
  133. if (this->policy_name){
  134. sd_debug("freeing policy name");
  135. free(this->policy_name);
  136. this->policy_name = NULL;
  137. };
  138. sd_debug("freeing this rolling policy instance");
  139. free(this);
  140. log4c_rollingpolicy_delete_exit:
  141. sd_debug("]");
  142. }
  143. /*******************************************************************************/
  144. LOG4C_API int log4c_rollingpolicy_init(log4c_rollingpolicy_t *this, rollingfile_udata_t* rfup){
  145. int rc = 0;
  146. if (!this)
  147. return -1;
  148. this->policy_rfudatap = rfup;
  149. if (!this->policy_type)
  150. return 0;
  151. if (!this->policy_type->init)
  152. return 0;
  153. rc = this->policy_type->init(this, rfup);
  154. this->policy_flags |= PFLAGS_IS_INITIALIZED;
  155. return rc;
  156. }
  157. LOG4C_API int log4c_rollingpolicy_fini(log4c_rollingpolicy_t *this){
  158. int rc = 0;
  159. sd_debug("log4c_rollingpolicy_fini['%s'",
  160. (this && this->policy_name ? this->policy_name: "(no name")) ;
  161. if (!this){
  162. rc = 0;
  163. } else {
  164. if (this->policy_flags & PFLAGS_IS_INITIALIZED){
  165. if (this->policy_type){
  166. rc = this->policy_type->fini(this);
  167. }
  168. }
  169. if (!rc){
  170. this->policy_flags &= ~PFLAGS_IS_INITIALIZED;
  171. }else{
  172. sd_debug("Call to rollingpolicy fini failed");
  173. }
  174. }
  175. sd_debug("]");
  176. return rc;
  177. }
  178. /*******************************************************************************/
  179. LOG4C_API int log4c_rollingpolicy_is_triggering_event(log4c_rollingpolicy_t* this, const log4c_logging_event_t* a_event, long current_fs){
  180. if (!this)
  181. return -1;
  182. if (!this->policy_type)
  183. return 0;
  184. if (!this->policy_type->is_triggering_event)
  185. return 0;
  186. return this->policy_type->is_triggering_event(this, a_event, current_fs);
  187. }
  188. /*******************************************************************************/
  189. LOG4C_API int log4c_rollingpolicy_rollover(log4c_rollingpolicy_t* this, FILE **fpp){
  190. if (!this)
  191. return -1;
  192. if (!this->policy_type)
  193. return 0;
  194. if (!this->policy_type->rollover)
  195. return 0;
  196. return this->policy_type->rollover(this, fpp);
  197. }
  198. /*******************************************************************************/
  199. LOG4C_API void* log4c_rollingpolicy_get_udata(const log4c_rollingpolicy_t* this){
  200. return (this ? this->policy_udata : NULL);
  201. }
  202. /*******************************************************************************/
  203. LOG4C_API void* log4c_rollingpolicy_get_name(const log4c_rollingpolicy_t* this){
  204. return (this ? this->policy_name : NULL);
  205. }
  206. /*******************************************************************************/
  207. LOG4C_API rollingfile_udata_t* log4c_rollingpolicy_get_rfudata(const log4c_rollingpolicy_t* this){
  208. return (this ? this->policy_rfudatap : NULL);
  209. }
  210. /*******************************************************************************/
  211. LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_type_set( const log4c_rollingpolicy_type_t* a_type){
  212. sd_hash_iter_t* i = NULL;
  213. void* previous = NULL;
  214. if (!a_type)
  215. return NULL;
  216. if ( (i = sd_hash_lookadd(log4c_rollingpolicy_types(), a_type->name)) == NULL)
  217. return NULL;
  218. previous = i->data;
  219. i->data = (void*) a_type;
  220. return (log4c_rollingpolicy_type_t*)previous;
  221. }
  222. /*****************************************************************************/
  223. LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_type_get( const char* a_name){
  224. sd_hash_iter_t* i;
  225. if (!a_name)
  226. return NULL;
  227. if ( (i = sd_hash_lookup(log4c_rollingpolicy_types(), a_name)) != NULL)
  228. return (log4c_rollingpolicy_type_t*)i->data;
  229. return NULL;
  230. }
  231. /*******************************************************************************/
  232. LOG4C_API void log4c_rollingpolicy_set_udata(log4c_rollingpolicy_t* this, void *udatap){
  233. if ( this) {
  234. this->policy_udata = udatap;
  235. }
  236. }
  237. /*******************************************************************************/
  238. LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_set_type( log4c_rollingpolicy_t* a_rollingpolicy, const log4c_rollingpolicy_type_t* a_type){
  239. const log4c_rollingpolicy_type_t* previous;
  240. if (!a_rollingpolicy)
  241. return NULL;
  242. previous = a_rollingpolicy->policy_type;
  243. a_rollingpolicy->policy_type = a_type;
  244. return previous;
  245. }
  246. /*******************************************************************************/
  247. LOG4C_API void log4c_rollingpolicy_print(const log4c_rollingpolicy_t* this, FILE* a_stream)
  248. {
  249. if (!this)
  250. return;
  251. fprintf(a_stream, "{ name:'%s' udata:%p}",
  252. this->policy_name,
  253. this->policy_udata);
  254. }
  255. LOG4C_API int log4c_rollingpolicy_is_initialized(log4c_rollingpolicy_t* this){
  256. if (!this)
  257. return(0);
  258. return( this->policy_flags & PFLAGS_IS_INITIALIZED);
  259. }