layout.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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(const log4c_layout_type_t* a_type)
  92. {
  93. sd_hash_iter_t* i = NULL;
  94. void* previous = NULL;
  95. if (!a_type)
  96. return NULL;
  97. if ( (i = sd_hash_lookadd(log4c_layout_types(), a_type->name)) == NULL)
  98. return NULL;
  99. previous = i->data;
  100. i->data = (void*) a_type;
  101. return (log4c_layout_type_t*)previous;
  102. }
  103. /*******************************************************************************/
  104. extern log4c_layout_t* log4c_layout_get(const char* a_name)
  105. {
  106. static const sd_factory_ops_t log4c_layout_factory_ops = {
  107. /*(void*) log4c_layout_new,
  108. (void*) log4c_layout_delete,
  109. (void*) log4c_layout_print,*/
  110. (void *( *)(const char *))log4c_layout_new,
  111. (void ( *)(void *))log4c_layout_delete,
  112. (void ( *)(void *,FILE *))log4c_layout_print
  113. };
  114. if (!log4c_layout_factory) {
  115. log4c_layout_factory = sd_factory_new("log4c_layout_factory", &log4c_layout_factory_ops);
  116. /* build default layouts */
  117. log4c_layout_set_type(log4c_layout_get("dated"), &log4c_layout_type_dated);
  118. log4c_layout_set_type(log4c_layout_get("basic"), &log4c_layout_type_basic);
  119. // 添加新的layouts [7/21/2010 jesse]
  120. log4c_layout_set_type(log4c_layout_get("dated_r"), &log4c_layout_type_dated_r);
  121. log4c_layout_set_type(log4c_layout_get("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 char* 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 char* 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 char* log4c_layout_format(const log4c_layout_t* ptrThis,
  182. const log4c_logging_event_t*a_event)
  183. {
  184. if (!ptrThis)
  185. return NULL;
  186. if (!ptrThis->lo_type)
  187. return NULL;
  188. if (!ptrThis->lo_type->format)
  189. return NULL;
  190. return ptrThis->lo_type->format(ptrThis, a_event);
  191. }
  192. /*******************************************************************************/
  193. extern void log4c_layout_print(const log4c_layout_t* ptrThis, FILE* a_stream)
  194. {
  195. if (!ptrThis)
  196. return;
  197. fprintf(a_stream, "{ name:'%s' type:'%s' udata:%p }",
  198. ptrThis->lo_name,
  199. ptrThis->lo_type ? ptrThis->lo_type->name : "(no set)",
  200. ptrThis->lo_udata);
  201. }