appender.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. static const char version[] = "1.2.1";
  2. /*
  3. * appender.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. #ifdef HAVE_CONFIG_H
  10. #include "config.h"
  11. #endif
  12. #include <log4c/appender.h>
  13. #include <log4c/appender_type_stream.h>
  14. #include <string.h>
  15. #include <sd/error.h>
  16. #include <sd/malloc.h>
  17. #include <sd/factory.h>
  18. #include <sd/hash.h>
  19. #include <sd/sd_xplatform.h>
  20. struct __log4c_appender
  21. {
  22. char* app_name;
  23. const log4c_layout_t* app_layout;
  24. const log4c_appender_type_t* app_type;
  25. int app_isopen;
  26. void* app_udata;
  27. };
  28. sd_factory_t* log4c_appender_factory = NULL;
  29. //////////////////////////////////////////////////////////////////////////
  30. ///以下为jesse修改的代码,将原有代码用#if 0...#endif注释掉
  31. // 为解决log4c_layout_types中内存泄漏而将静态局部变量提升为全局局部变量 [9/8/2008 jesse]
  32. static sd_hash_t* gs_types = NULL;
  33. static sd_hash_t* log4c_appender_types(void)
  34. {
  35. ///static sd_hash_t* types = NULL;
  36. if (!gs_types)
  37. gs_types = sd_hash_new(20, NULL);
  38. return gs_types;
  39. }
  40. /** 删除本appender.c文件中定义的全局和静态的指针指向的内存,防止内存泄漏
  41. 如:
  42. 1.gs_types.
  43. 2.
  44. @return void.
  45. 作者:jesse 日期:2008.09.08
  46. */
  47. extern void log4c_appender_delete_global()
  48. {
  49. if (NULL != gs_types)
  50. {
  51. sd_hash_delete(gs_types);
  52. 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_appender_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_appender_types_print(FILE *fp)
  70. {
  71. sd_hash_iter_t* i;
  72. fprintf(fp, "appender types:");
  73. for (i = sd_hash_begin(log4c_appender_types());
  74. i != sd_hash_end(log4c_appender_types());
  75. i = sd_hash_iter_next(i) )
  76. {
  77. fprintf(fp, "'%s' ",((log4c_appender_type_t *)(i->data))->name );
  78. }
  79. fprintf(fp, "\n");
  80. }
  81. /*******************************************************************************/
  82. extern const log4c_appender_type_t* log4c_appender_type_get(const char* a_name)
  83. {
  84. sd_hash_iter_t* i;
  85. if (!a_name)
  86. return NULL;
  87. if ( (i = sd_hash_lookup(log4c_appender_types(), a_name)) != NULL)
  88. return (log4c_appender_type_t*)i->data;
  89. return NULL;
  90. }
  91. /*******************************************************************************/
  92. extern const log4c_appender_type_t* log4c_appender_type_set(
  93. const log4c_appender_type_t* a_type)
  94. {
  95. sd_hash_iter_t* i = NULL;
  96. void* previous = NULL;
  97. if (!a_type)
  98. return NULL;
  99. if ( (i = sd_hash_lookadd(log4c_appender_types(), a_type->name)) == NULL)
  100. return NULL;
  101. previous = i->data;
  102. i->data = (void*) a_type;
  103. return (log4c_appender_type_t*)previous;
  104. }
  105. /*******************************************************************************/
  106. extern log4c_appender_t* log4c_appender_get(const char* a_name)
  107. {
  108. static const sd_factory_ops_t log4c_appender_factory_ops = {
  109. /*(void*) log4c_appender_new,
  110. (void*) log4c_appender_delete,
  111. (void*) log4c_appender_print,*/
  112. (void *( *)(const char *))log4c_appender_new,
  113. (void ( *)(void *))log4c_appender_delete,
  114. (void ( *)(void *,FILE *))log4c_appender_print
  115. };
  116. if (!log4c_appender_factory) {
  117. log4c_appender_factory = sd_factory_new("log4c_appender_factory",
  118. &log4c_appender_factory_ops);
  119. /* build default appenders */
  120. log4c_appender_set_udata(log4c_appender_get("stderr"), stderr);
  121. log4c_appender_set_udata(log4c_appender_get("stdout"), stdout);
  122. }
  123. return (log4c_appender_t *)sd_factory_get(log4c_appender_factory, a_name);
  124. }
  125. /*******************************************************************************/
  126. extern log4c_appender_t* log4c_appender_new(const char* a_name)
  127. {
  128. log4c_appender_t* ptrThis;
  129. if (!a_name)
  130. return NULL;
  131. ptrThis = (log4c_appender_t*)sd_calloc(1, sizeof(log4c_appender_t));
  132. ptrThis->app_name = sd_strdup(a_name);
  133. ptrThis->app_type = &log4c_appender_type_stream;
  134. ptrThis->app_layout = log4c_layout_get("basic");
  135. ptrThis->app_isopen = 0;
  136. ptrThis->app_udata = NULL;
  137. return ptrThis;
  138. }
  139. /*******************************************************************************/
  140. extern void log4c_appender_delete(log4c_appender_t* ptrThis)
  141. {
  142. sd_debug("log4c_appender_delete['%s'",
  143. (ptrThis && ptrThis->app_name ? ptrThis->app_name: "(no name)"));
  144. if (!ptrThis){
  145. goto log4c_appender_delete_exit;
  146. }
  147. log4c_appender_close(ptrThis);
  148. if (ptrThis->app_name){
  149. free(ptrThis->app_name);
  150. }
  151. free(ptrThis);
  152. log4c_appender_delete_exit:
  153. sd_debug("]");
  154. }
  155. /*******************************************************************************/
  156. extern const char* log4c_appender_get_name(const log4c_appender_t* ptrThis)
  157. {
  158. return (ptrThis ? ptrThis->app_name : "(nil)");
  159. }
  160. /*******************************************************************************/
  161. extern const log4c_appender_type_t* log4c_appender_get_type(
  162. const log4c_appender_t* ptrThis)
  163. {
  164. return (ptrThis ? ptrThis->app_type : NULL);
  165. }
  166. /*******************************************************************************/
  167. extern const log4c_layout_t* log4c_appender_get_layout(const log4c_appender_t* ptrThis)
  168. {
  169. return (ptrThis ? ptrThis->app_layout : NULL);
  170. }
  171. /*******************************************************************************/
  172. extern void* log4c_appender_get_udata(const log4c_appender_t* ptrThis)
  173. {
  174. return (ptrThis ? ptrThis->app_udata : NULL);
  175. }
  176. /*******************************************************************************/
  177. extern const log4c_appender_type_t* log4c_appender_set_type(
  178. log4c_appender_t* ptrThis,
  179. const log4c_appender_type_t* a_type)
  180. {
  181. const log4c_appender_type_t* previous;
  182. if (!ptrThis)
  183. return NULL;
  184. previous = ptrThis->app_type;
  185. ptrThis->app_type = a_type;
  186. return previous;
  187. }
  188. /*******************************************************************************/
  189. extern const log4c_layout_t* log4c_appender_set_layout(
  190. log4c_appender_t* ptrThis,
  191. const log4c_layout_t* a_layout)
  192. {
  193. const log4c_layout_t* previous;
  194. if (!ptrThis)
  195. return NULL;
  196. previous = ptrThis->app_layout;
  197. ptrThis->app_layout = a_layout;
  198. return previous;
  199. }
  200. /*******************************************************************************/
  201. extern void* log4c_appender_set_udata(log4c_appender_t* ptrThis, void* a_udata)
  202. {
  203. void* previous;
  204. if (!ptrThis)
  205. return NULL;
  206. previous = ptrThis->app_udata;
  207. ptrThis->app_udata = a_udata;
  208. return previous;
  209. }
  210. /*******************************************************************************/
  211. extern int log4c_appender_open(log4c_appender_t* ptrThis)
  212. {
  213. int rc = 0;
  214. if (!ptrThis)
  215. return -1;
  216. if (ptrThis->app_isopen)
  217. return 0;
  218. if (!ptrThis->app_type)
  219. return 0;
  220. if (!ptrThis->app_type->open)
  221. return 0;
  222. if (ptrThis->app_type->open(ptrThis) == -1){
  223. rc = -1;
  224. }
  225. if (!rc) {
  226. ptrThis->app_isopen++;
  227. }
  228. return rc;
  229. }
  230. /**
  231. * @bug is this the right place to open an appender ?
  232. */
  233. /*******************************************************************************/
  234. extern int log4c_appender_append(
  235. log4c_appender_t* ptrThis,
  236. log4c_logging_event_t* a_event)
  237. {
  238. if (!ptrThis)
  239. return -1;
  240. if (!ptrThis->app_type)
  241. return 0;
  242. if (!ptrThis->app_type->append)
  243. return 0;
  244. if (!ptrThis->app_isopen)
  245. if (log4c_appender_open(ptrThis) == -1)
  246. return -1;
  247. if ( (a_event->evt_rendered_msg =
  248. log4c_layout_format(ptrThis->app_layout, a_event)) == NULL)
  249. a_event->evt_rendered_msg = a_event->evt_msg;
  250. return ptrThis->app_type->append(ptrThis, a_event);
  251. }
  252. /*******************************************************************************/
  253. extern int log4c_appender_append_no_file_num_no_layout(
  254. log4c_appender_t* ptrThis,
  255. log4c_logging_event_t* a_event)
  256. {
  257. if (!ptrThis)
  258. return -1;
  259. if (!ptrThis->app_type)
  260. return 0;
  261. if (!ptrThis->app_type->append)
  262. return 0;
  263. if (!ptrThis->app_isopen)
  264. if (log4c_appender_open(ptrThis) == -1)
  265. return -1;
  266. #if 0
  267. if ( (a_event->evt_rendered_msg =
  268. log4c_layout_format(ptrThis->app_layout, a_event)) == NULL)
  269. a_event->evt_rendered_msg = a_event->evt_msg;
  270. #else
  271. a_event->evt_rendered_msg = a_event->evt_msg;
  272. #endif
  273. return ptrThis->app_type->append(ptrThis, a_event);
  274. }
  275. /*******************************************************************************/
  276. extern int log4c_appender_close(log4c_appender_t* ptrThis)
  277. {
  278. if (!ptrThis)
  279. return -1;
  280. if (!ptrThis->app_isopen)
  281. return 0;
  282. if (!ptrThis->app_type)
  283. return 0;
  284. if (!ptrThis->app_type->close)
  285. return 0;
  286. if (ptrThis->app_type->close(ptrThis) == -1)
  287. return -1;
  288. ptrThis->app_isopen--;
  289. return 0;
  290. }
  291. /*******************************************************************************/
  292. extern void log4c_appender_print(const log4c_appender_t* ptrThis, FILE* a_stream)
  293. {
  294. if (!ptrThis)
  295. return;
  296. fprintf(a_stream, "{ name:'%s' type:'%s' layout:'%s' isopen:%d udata:%p}",
  297. ptrThis->app_name,
  298. ptrThis->app_type ? ptrThis->app_type->name : "(not set)",
  299. log4c_layout_get_name(ptrThis->app_layout),
  300. ptrThis->app_isopen,
  301. ptrThis->app_udata);
  302. }