category.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. static const char version[] = "$Id$";
  2. /*
  3. * category.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 <stdlib.h>
  13. #include <string.h>
  14. #ifdef HAVE_ALLOCA_H
  15. #include <alloca.h>
  16. #endif
  17. #include <sd/sprintf.h>
  18. #include <sd/malloc.h>
  19. #include <sd/factory.h>
  20. #include <log4c/appender.h>
  21. #include <log4c/priority.h>
  22. #include <log4c/logging_event.h>
  23. #include <log4c/category.h>
  24. #include <log4c/rc.h>
  25. #include <sd/error.h>
  26. #include <sd/sd_xplatform.h>
  27. struct __log4c_category {
  28. char* cat_name;
  29. int cat_priority;
  30. int cat_additive;
  31. const log4c_category_t* cat_parent;
  32. log4c_appender_t* cat_appender;
  33. };
  34. sd_factory_t* log4c_category_factory = NULL;
  35. static const char LOG4C_CATEGORY_DEFAULT[] = "root";
  36. /**
  37. * @bug the root category name should be "" not "root". *
  38. */
  39. /*******************************************************************************/
  40. extern log4c_category_t* log4c_category_get(const char* a_name)
  41. {
  42. static const sd_factory_ops_t log4c_category_factory_ops = {
  43. /*(void*) log4c_category_new,
  44. (void*) log4c_category_delete,
  45. (void*) log4c_category_print,*/
  46. (void *( *)(const char *))log4c_category_new,
  47. (void ( *)(void *))log4c_category_delete,
  48. (void ( *)(void *,FILE *))log4c_category_print
  49. };
  50. if (!log4c_category_factory) {
  51. log4c_category_factory = sd_factory_new("log4c_category_factory",
  52. &log4c_category_factory_ops);
  53. }
  54. return (log4c_category_t *)sd_factory_get(log4c_category_factory, a_name);
  55. }
  56. /*******************************************************************************/
  57. extern int log4c_category_list(log4c_category_t** a_cats, int a_ncats)
  58. {
  59. return sd_factory_list(log4c_category_factory, (void**) a_cats, a_ncats);
  60. }
  61. /*******************************************************************************/
  62. static const char* dot_dirname(char* a_string);
  63. extern log4c_category_t* log4c_category_new(const char* a_name)
  64. {
  65. log4c_category_t* ptrThis;
  66. if (!a_name)
  67. return NULL;
  68. ptrThis = (log4c_category_t *)sd_calloc(1, sizeof(log4c_category_t));
  69. ptrThis->cat_name = sd_strdup(a_name);
  70. ptrThis->cat_priority = LOG4C_PRIORITY_NOTSET;
  71. ptrThis->cat_additive = 1;
  72. ptrThis->cat_appender = NULL;
  73. ptrThis->cat_parent = NULL;
  74. /* skip root category because it has a NULL parent */
  75. if (strcmp(LOG4C_CATEGORY_DEFAULT, a_name)) {
  76. char* tmp = sd_strdup(ptrThis->cat_name);
  77. ptrThis->cat_parent = log4c_category_get(dot_dirname(tmp));
  78. free(tmp);
  79. }
  80. return ptrThis;
  81. }
  82. /*******************************************************************************/
  83. extern void log4c_category_delete(log4c_category_t* ptrThis)
  84. {
  85. if (!ptrThis)
  86. return;
  87. free(ptrThis->cat_name);
  88. free(ptrThis);
  89. }
  90. /*******************************************************************************/
  91. extern const char* log4c_category_get_name(const log4c_category_t* ptrThis)
  92. {
  93. return (ptrThis ? ptrThis->cat_name : "(nil)");
  94. }
  95. /*******************************************************************************/
  96. extern int log4c_category_get_priority(const log4c_category_t* ptrThis)
  97. {
  98. return (ptrThis ? ptrThis->cat_priority : LOG4C_PRIORITY_UNKNOWN);
  99. }
  100. /*******************************************************************************/
  101. extern int log4c_category_get_chainedpriority(const log4c_category_t* ptrThis)
  102. {
  103. const log4c_category_t* cat = ptrThis;
  104. if (!ptrThis)
  105. return LOG4C_PRIORITY_UNKNOWN;
  106. while (cat->cat_priority == LOG4C_PRIORITY_NOTSET && cat->cat_parent)
  107. cat = cat->cat_parent;
  108. return cat->cat_priority;
  109. }
  110. /*******************************************************************************/
  111. extern const log4c_appender_t* log4c_category_get_appender(const log4c_category_t* ptrThis)
  112. {
  113. return (ptrThis ? ptrThis->cat_appender : NULL);
  114. }
  115. /*******************************************************************************/
  116. extern int log4c_category_get_additivity(const log4c_category_t* ptrThis)
  117. {
  118. return (ptrThis ? ptrThis->cat_additive : -1);
  119. }
  120. /*******************************************************************************/
  121. extern int log4c_category_set_priority(log4c_category_t* ptrThis, int a_priority)
  122. {
  123. int previous;
  124. if (!ptrThis)
  125. return LOG4C_PRIORITY_UNKNOWN;
  126. previous = ptrThis->cat_priority;
  127. ptrThis->cat_priority = a_priority;
  128. return previous;
  129. }
  130. /**
  131. * @todo need multiple appenders per category
  132. */
  133. /*******************************************************************************/
  134. extern const log4c_appender_t* log4c_category_set_appender(
  135. log4c_category_t* ptrThis,
  136. log4c_appender_t* a_appender)
  137. {
  138. log4c_appender_t* previous;
  139. if (!ptrThis)
  140. return NULL;
  141. previous = ptrThis->cat_appender;
  142. ptrThis->cat_appender = a_appender;
  143. return previous;
  144. }
  145. /*******************************************************************************/
  146. extern int log4c_category_set_additivity(log4c_category_t* ptrThis, int a_additivity)
  147. {
  148. int previous;
  149. if (!ptrThis)
  150. return -1;
  151. previous = ptrThis->cat_additive;
  152. ptrThis->cat_additive = a_additivity;
  153. return previous;
  154. }
  155. /*******************************************************************************/
  156. extern void log4c_category_print(const log4c_category_t* ptrThis, FILE* a_stream)
  157. {
  158. if (!ptrThis)
  159. return;
  160. fprintf(a_stream, "{ name:'%s' priority:%s additive:%d appender:'%s' parent:'%s' }",
  161. ptrThis->cat_name,
  162. log4c_priority_to_string(ptrThis->cat_priority),
  163. ptrThis->cat_additive,
  164. log4c_appender_get_name(ptrThis->cat_appender),
  165. log4c_category_get_name(ptrThis->cat_parent)
  166. );
  167. }
  168. /*******************************************************************************/
  169. extern void __log4c_category_vlog(const log4c_category_t* ptrThis,
  170. const log4c_location_info_t* a_locinfo,
  171. int a_priority,
  172. const char* a_format,
  173. va_list a_args)
  174. {
  175. char* message;
  176. log4c_logging_event_t evt;
  177. const log4c_category_t* cat;
  178. int yes = 0;
  179. if (!ptrThis)
  180. return;
  181. /* check if an appender is defined in the category hierarchy */
  182. for (cat = ptrThis; cat; cat = cat->cat_parent)
  183. {
  184. if (cat->cat_appender)
  185. yes++;
  186. }
  187. if (!yes)
  188. return;
  189. log4c_reread();
  190. /* when there is no limit on the buffer size, we use malloc() to
  191. * give the user the possiblity to reallocate if necessary. When
  192. * the buffer is limited in size, we use alloca() for more
  193. * efficiency.
  194. */
  195. evt.evt_buffer.buf_maxsize = log4c_rc->config.bufsize;
  196. if (!evt.evt_buffer.buf_maxsize)
  197. {
  198. evt.evt_buffer.buf_size = LOG4C_BUFFER_SIZE_DEFAULT;
  199. evt.evt_buffer.buf_data = (char*)sd_malloc(evt.evt_buffer.buf_size);
  200. message = sd_vsprintf(a_format, a_args);
  201. }
  202. else
  203. {
  204. size_t n;
  205. evt.evt_buffer.buf_size = evt.evt_buffer.buf_maxsize;
  206. evt.evt_buffer.buf_data = (char*)alloca(evt.evt_buffer.buf_size);
  207. message = (char*)alloca(evt.evt_buffer.buf_size);
  208. if ( (n = (size_t)vsnprintf(message, evt.evt_buffer.buf_size, a_format, a_args))
  209. >= evt.evt_buffer.buf_size)
  210. sd_error("truncating message of %d bytes (bufsize = %d)", n,
  211. evt.evt_buffer.buf_size);
  212. }
  213. evt.evt_category = ptrThis->cat_name;
  214. evt.evt_priority = a_priority;
  215. evt.evt_msg = message;
  216. evt.evt_loc = a_locinfo;
  217. SD_GETTIMEOFDAY(&evt.evt_timestamp, NULL);
  218. for (cat = ptrThis; cat; cat = cat->cat_parent)
  219. {
  220. if (cat->cat_appender)
  221. log4c_appender_append(cat->cat_appender, &evt);
  222. if (!cat->cat_additive) break;
  223. }
  224. if (!evt.evt_buffer.buf_maxsize)
  225. {
  226. free(message);
  227. free(evt.evt_buffer.buf_data);
  228. }
  229. }
  230. /*******************************************************************************/
  231. extern void __log4c_category_vlog_no_file_num_no_layout(
  232. const log4c_category_t* ptrThis,
  233. const log4c_location_info_t* a_locinfo,
  234. int a_priority,
  235. const char* a_format,
  236. va_list a_args)
  237. {
  238. char* message;
  239. log4c_logging_event_t evt;
  240. const log4c_category_t* cat;
  241. int yes = 0;
  242. if (!ptrThis)
  243. return;
  244. /* check if an appender is defined in the category hierarchy */
  245. for (cat = ptrThis; cat; cat = cat->cat_parent) {
  246. if (cat->cat_appender)
  247. yes++;
  248. }
  249. if (!yes)
  250. return;
  251. log4c_reread();
  252. /* when there is no limit on the buffer size, we use malloc() to
  253. * give the user the possiblity to reallocate if necessary. When
  254. * the buffer is limited in size, we use alloca() for more
  255. * efficiency.
  256. */
  257. evt.evt_buffer.buf_maxsize = log4c_rc->config.bufsize;
  258. if (!evt.evt_buffer.buf_maxsize) {
  259. evt.evt_buffer.buf_size = LOG4C_BUFFER_SIZE_DEFAULT;
  260. evt.evt_buffer.buf_data = (char*)sd_malloc(evt.evt_buffer.buf_size);
  261. message = sd_vsprintf(a_format, a_args);
  262. }
  263. else {
  264. size_t n;
  265. evt.evt_buffer.buf_size = evt.evt_buffer.buf_maxsize;
  266. evt.evt_buffer.buf_data = (char*)alloca(evt.evt_buffer.buf_size);
  267. message = (char*)alloca(evt.evt_buffer.buf_size);
  268. if ( (n = (size_t)vsnprintf(message, evt.evt_buffer.buf_size, a_format, a_args))
  269. >= evt.evt_buffer.buf_size)
  270. sd_error("truncating message of %d bytes (bufsize = %d)", n,
  271. evt.evt_buffer.buf_size);
  272. }
  273. evt.evt_category = ptrThis->cat_name;
  274. evt.evt_priority = a_priority;
  275. evt.evt_msg = message;
  276. evt.evt_loc = a_locinfo;
  277. SD_GETTIMEOFDAY(&evt.evt_timestamp, NULL);
  278. for (cat = ptrThis; cat; cat = cat->cat_parent) {
  279. if (cat->cat_appender)
  280. log4c_appender_append_no_file_num_no_layout(cat->cat_appender, &evt);
  281. if (!cat->cat_additive) break;
  282. }
  283. if (!evt.evt_buffer.buf_maxsize) {
  284. free(message);
  285. free(evt.evt_buffer.buf_data);
  286. }
  287. }
  288. /*******************************************************************************/
  289. static const char* dot_dirname(char* a_string)
  290. {
  291. char* p;
  292. if (!a_string)
  293. return NULL;
  294. if ( (p = strrchr(a_string, '.')) == NULL)
  295. return LOG4C_CATEGORY_DEFAULT;
  296. *p = '\0';
  297. return a_string;
  298. }