layout.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * layout.c
  3. *
  4. * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
  5. *
  6. * See the COPYING file for the terms of usage and distribution.
  7. */
  8. #include <tchar.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "log4c/layout.h"
  13. #include "log4c/layout_type_basic.h"
  14. #include "log4c/layout_type_dated.h"
  15. #include "log4c/layout_type_dated_r.h"
  16. #include "log4c/layout_type_dated_threadid.h"
  17. #include "log4c/priority.h"
  18. #include "sd/hash.h"
  19. #include "sd/malloc.h"
  20. #include "sd/factory.h"
  21. static const TCHAR version[] = _T("$Id$");
  22. struct __log4c_layout
  23. {
  24. TCHAR* lo_name;
  25. const log4c_layout_type_t* lo_type;
  26. void* lo_udata;
  27. };
  28. sd_factory_t* log4c_layout_factory = NULL;
  29. //////////////////////////////////////////////////////////////////////////
  30. ///以下为jesse修改的代码,将原有代码用#if 0...#endif注释掉
  31. // 为解决log4c_layout_types中内存泄漏而将静态局部变量提升为全局局部变量 [9/8/2008 jesse]
  32. static sd_hash_t* layout_c_gs_types = NULL;
  33. static sd_hash_t* log4c_layout_types(void)
  34. {
  35. ///static sd_hash_t* types = NULL;
  36. if (!layout_c_gs_types)
  37. layout_c_gs_types = sd_hash_new(20, NULL);
  38. return layout_c_gs_types;
  39. }
  40. /** 删除本layout.c文件中定义的全局和静态的指针指向的内存,防止内存泄漏
  41. 如:
  42. 1.layout_c_gs_types.
  43. 2.
  44. @return void.
  45. 作者:jesse 日期:2008.09.08
  46. */
  47. extern void log4c_layout_delete_global()
  48. {
  49. if (NULL != layout_c_gs_types)
  50. {
  51. sd_hash_delete(layout_c_gs_types);
  52. layout_c_gs_types = NULL;
  53. }
  54. }
  55. //////////////////////////////////////////////////////////////////////////
  56. #if 0
  57. /**
  58. * @bug log4c_appender_type hash is not freed in destructor
  59. */
  60. /*******************************************************************************/
  61. static sd_hash_t* log4c_layout_types(void)
  62. {
  63. static sd_hash_t* types = NULL;
  64. if (!types)
  65. types = sd_hash_new(20, NULL);
  66. return types;
  67. }
  68. #endif
  69. extern void log4c_layout_types_print(FILE *fp)
  70. {
  71. sd_hash_iter_t* i;
  72. _ftprintf(fp, _T("layout types:"));
  73. for (i = sd_hash_begin(log4c_layout_types());i != sd_hash_end(log4c_layout_types()); i = sd_hash_iter_next(i) )
  74. {
  75. _ftprintf(fp, _T("'%s' "),((log4c_layout_type_t *)(i->data))->name );
  76. }
  77. _ftprintf(fp, _T("\n"));
  78. }
  79. /*******************************************************************************/
  80. extern const log4c_layout_type_t* log4c_layout_type_get(const TCHAR* a_name)
  81. {
  82. sd_hash_iter_t* i;
  83. if (!a_name)
  84. return NULL;
  85. if ( (i = (sd_hash_iter_t*)sd_hash_lookup(log4c_layout_types(), a_name)) != NULL)
  86. return (log4c_layout_type_t*)i->data;
  87. return NULL;
  88. }
  89. /*******************************************************************************/
  90. extern const log4c_layout_type_t* log4c_layout_type_set(const log4c_layout_type_t* a_type)
  91. {
  92. sd_hash_iter_t* i = NULL;
  93. void* previous = NULL;
  94. if (!a_type)
  95. return NULL;
  96. if ( (i = sd_hash_lookadd(log4c_layout_types(), a_type->name)) == NULL)
  97. return NULL;
  98. previous = i->data;
  99. i->data = (void*) a_type;
  100. return (log4c_layout_type_t*)previous;
  101. }
  102. /*******************************************************************************/
  103. extern log4c_layout_t* log4c_layout_get(const TCHAR* a_name)
  104. {
  105. static const sd_factory_ops_t log4c_layout_factory_ops = {
  106. /*(void*) log4c_layout_new,
  107. (void*) log4c_layout_delete,
  108. (void*) log4c_layout_print,*/
  109. (void *( *)(const TCHAR *))log4c_layout_new,
  110. (void ( *)(void *))log4c_layout_delete,
  111. (void ( *)(void *,FILE *))log4c_layout_print
  112. };
  113. if (!log4c_layout_factory)
  114. {
  115. log4c_layout_factory = sd_factory_new(_T("log4c_layout_factory"),&log4c_layout_factory_ops);
  116. /* build default layouts */
  117. log4c_layout_set_type(log4c_layout_get(_T("dated")), &log4c_layout_type_dated);
  118. log4c_layout_set_type(log4c_layout_get(_T("basic")), &log4c_layout_type_basic);
  119. // 添加新的layouts [7/21/2010 jesse]
  120. log4c_layout_set_type(log4c_layout_get(_T("dated_r")), &log4c_layout_type_dated_r);
  121. log4c_layout_set_type(log4c_layout_get(_T("dated_threadid")), &log4c_layout_type_dated_threadid);
  122. }
  123. return (log4c_layout_t *)sd_factory_get(log4c_layout_factory, a_name);
  124. }
  125. /*******************************************************************************/
  126. extern log4c_layout_t* log4c_layout_new(const TCHAR* a_name)
  127. {
  128. log4c_layout_t* ptrThis;
  129. if (!a_name)
  130. return NULL;
  131. ptrThis = (log4c_layout_t*)sd_calloc(1, sizeof(log4c_layout_t));
  132. ptrThis->lo_name = sd_strdup(a_name);
  133. ptrThis->lo_type = &log4c_layout_type_basic;
  134. ptrThis->lo_udata = NULL;
  135. return ptrThis;
  136. }
  137. /*******************************************************************************/
  138. extern void log4c_layout_delete(log4c_layout_t* ptrThis)
  139. {
  140. if (!ptrThis)
  141. return;
  142. free(ptrThis->lo_name);
  143. free(ptrThis);
  144. }
  145. /*******************************************************************************/
  146. extern const TCHAR* log4c_layout_get_name(const log4c_layout_t* ptrThis)
  147. {
  148. return (ptrThis ? ptrThis->lo_name : NULL);
  149. }
  150. /*******************************************************************************/
  151. extern const log4c_layout_type_t* log4c_layout_get_type(const log4c_layout_t* ptrThis)
  152. {
  153. return (ptrThis ? ptrThis->lo_type : NULL);
  154. }
  155. /*******************************************************************************/
  156. extern const log4c_layout_type_t* log4c_layout_set_type(log4c_layout_t* ptrThis, const log4c_layout_type_t* a_type)
  157. {
  158. const log4c_layout_type_t* previous;
  159. if (!ptrThis)
  160. return NULL;
  161. previous = ptrThis->lo_type;
  162. ptrThis->lo_type = a_type;
  163. return previous;
  164. }
  165. /*******************************************************************************/
  166. extern void* log4c_layout_get_udata(const log4c_layout_t* ptrThis)
  167. {
  168. return (ptrThis ? ptrThis->lo_udata : NULL);
  169. }
  170. /*******************************************************************************/
  171. extern void* log4c_layout_set_udata(log4c_layout_t* ptrThis, void* a_udata)
  172. {
  173. void* previous;
  174. if (!ptrThis)
  175. return NULL;
  176. previous = ptrThis->lo_udata;
  177. ptrThis->lo_udata = a_udata;
  178. return previous;
  179. }
  180. /*******************************************************************************/
  181. extern const TCHAR* log4c_layout_format(
  182. const log4c_layout_t* ptrThis,
  183. const log4c_logging_event_t*a_event)
  184. {
  185. if (!ptrThis)
  186. return NULL;
  187. if (!ptrThis->lo_type)
  188. return NULL;
  189. if (!ptrThis->lo_type->format)
  190. return NULL;
  191. return ptrThis->lo_type->format(ptrThis, a_event);
  192. }
  193. /*******************************************************************************/
  194. extern void log4c_layout_print(const log4c_layout_t* ptrThis, FILE* a_stream)
  195. {
  196. if (!ptrThis)
  197. return;
  198. _ftprintf(a_stream, _T("{ name:'%s' type:'%s' udata:%p }"),
  199. ptrThis->lo_name,
  200. ptrThis->lo_type ? ptrThis->lo_type->name : _T("(no set)"),
  201. ptrThis->lo_udata);
  202. }