layout.c 6.6 KB

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