rollingpolicy.c 9.0 KB

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