category.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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(log4c_category_t* ptrThis, log4c_appender_t* a_appender)
  135. {
  136. log4c_appender_t* previous;
  137. if (!ptrThis)
  138. return NULL;
  139. previous = ptrThis->cat_appender;
  140. ptrThis->cat_appender = a_appender;
  141. return previous;
  142. }
  143. /*******************************************************************************/
  144. extern int log4c_category_set_additivity(log4c_category_t* ptrThis, int a_additivity)
  145. {
  146. int previous;
  147. if (!ptrThis)
  148. return -1;
  149. previous = ptrThis->cat_additive;
  150. ptrThis->cat_additive = a_additivity;
  151. return previous;
  152. }
  153. /*******************************************************************************/
  154. extern void log4c_category_print(const log4c_category_t* ptrThis, FILE* a_stream)
  155. {
  156. if (!ptrThis)
  157. return;
  158. fprintf(a_stream, "{ name:'%s' priority:%s additive:%d appender:'%s' parent:'%s' }",
  159. ptrThis->cat_name,
  160. log4c_priority_to_string(ptrThis->cat_priority),
  161. ptrThis->cat_additive,
  162. log4c_appender_get_name(ptrThis->cat_appender),
  163. log4c_category_get_name(ptrThis->cat_parent)
  164. );
  165. }
  166. /*******************************************************************************/
  167. extern void __log4c_category_vlog(const log4c_category_t* ptrThis,
  168. const log4c_location_info_t* a_locinfo,
  169. int a_priority,
  170. const char* a_format,
  171. va_list a_args)
  172. {
  173. char* message;
  174. log4c_logging_event_t evt;
  175. const log4c_category_t* cat;
  176. int yes = 0;
  177. if (!ptrThis)
  178. return;
  179. /* check if an appender is defined in the category hierarchy */
  180. for (cat = ptrThis; cat; cat = cat->cat_parent)
  181. {
  182. if (cat->cat_appender)
  183. yes++;
  184. }
  185. if (!yes)
  186. return;
  187. log4c_reread();
  188. /* when there is no limit on the buffer size, we use malloc() to
  189. * give the user the possiblity to reallocate if necessary. When
  190. * the buffer is limited in size, we use alloca() for more
  191. * efficiency.
  192. */
  193. evt.evt_buffer.buf_maxsize = log4c_rc->config.bufsize;
  194. if (!evt.evt_buffer.buf_maxsize)
  195. {
  196. evt.evt_buffer.buf_size = LOG4C_BUFFER_SIZE_DEFAULT;
  197. evt.evt_buffer.buf_data = (char*)sd_malloc(evt.evt_buffer.buf_size);
  198. message = sd_vsprintf(a_format, a_args);
  199. }
  200. else
  201. {
  202. size_t n;
  203. evt.evt_buffer.buf_size = evt.evt_buffer.buf_maxsize;
  204. evt.evt_buffer.buf_data = (char*)alloca(evt.evt_buffer.buf_size);
  205. message = (char*)alloca(evt.evt_buffer.buf_size);
  206. if ( (n = (size_t)_vsnprintf_s(message, evt.evt_buffer.buf_size, _TRUNCATE, a_format, a_args))
  207. >= evt.evt_buffer.buf_size)
  208. sd_error("truncating message of %d bytes (bufsize = %d)", n,
  209. evt.evt_buffer.buf_size);
  210. }
  211. evt.evt_category = ptrThis->cat_name;
  212. evt.evt_priority = a_priority;
  213. evt.evt_msg = message;
  214. evt.evt_loc = a_locinfo;
  215. SD_GETTIMEOFDAY(&evt.evt_timestamp, NULL);
  216. for (cat = ptrThis; cat; cat = cat->cat_parent)
  217. {
  218. if (cat->cat_appender)
  219. log4c_appender_append(cat->cat_appender, &evt);
  220. if (!cat->cat_additive) break;
  221. }
  222. if (!evt.evt_buffer.buf_maxsize)
  223. {
  224. free(message);
  225. free(evt.evt_buffer.buf_data);
  226. }
  227. }
  228. /*******************************************************************************/
  229. extern void __log4c_category_vlog_no_file_num_no_layout(
  230. const log4c_category_t* ptrThis,
  231. const log4c_location_info_t* a_locinfo,
  232. int a_priority,
  233. const char* a_format,
  234. va_list a_args)
  235. {
  236. char* message;
  237. log4c_logging_event_t evt;
  238. const log4c_category_t* cat;
  239. int yes = 0;
  240. if (!ptrThis)
  241. return;
  242. /* check if an appender is defined in the category hierarchy */
  243. for (cat = ptrThis; cat; cat = cat->cat_parent) {
  244. if (cat->cat_appender)
  245. yes++;
  246. }
  247. if (!yes)
  248. return;
  249. log4c_reread();
  250. /* when there is no limit on the buffer size, we use malloc() to
  251. * give the user the possiblity to reallocate if necessary. When
  252. * the buffer is limited in size, we use alloca() for more
  253. * efficiency.
  254. */
  255. evt.evt_buffer.buf_maxsize = log4c_rc->config.bufsize;
  256. if (!evt.evt_buffer.buf_maxsize) {
  257. evt.evt_buffer.buf_size = LOG4C_BUFFER_SIZE_DEFAULT;
  258. evt.evt_buffer.buf_data = (char*)sd_malloc(evt.evt_buffer.buf_size);
  259. message = sd_vsprintf(a_format, a_args);
  260. }
  261. else {
  262. size_t n;
  263. evt.evt_buffer.buf_size = evt.evt_buffer.buf_maxsize;
  264. evt.evt_buffer.buf_data = (char*)alloca(evt.evt_buffer.buf_size);
  265. message = (char*)alloca(evt.evt_buffer.buf_size);
  266. if ( (n = (size_t)_vsnprintf_s(message, evt.evt_buffer.buf_size, _TRUNCATE, a_format, a_args))
  267. >= evt.evt_buffer.buf_size)
  268. sd_error("truncating message of %d bytes (bufsize = %d)", n,
  269. evt.evt_buffer.buf_size);
  270. }
  271. evt.evt_category = ptrThis->cat_name;
  272. evt.evt_priority = a_priority;
  273. evt.evt_msg = message;
  274. evt.evt_loc = a_locinfo;
  275. SD_GETTIMEOFDAY(&evt.evt_timestamp, NULL);
  276. for (cat = ptrThis; cat; cat = cat->cat_parent) {
  277. if (cat->cat_appender)
  278. log4c_appender_append_no_file_num_no_layout(cat->cat_appender, &evt);
  279. if (!cat->cat_additive) break;
  280. }
  281. if (!evt.evt_buffer.buf_maxsize) {
  282. free(message);
  283. free(evt.evt_buffer.buf_data);
  284. }
  285. }
  286. /*******************************************************************************/
  287. static const char* dot_dirname(char* a_string)
  288. {
  289. char* p;
  290. if (!a_string)
  291. return NULL;
  292. if ( (p = strrchr(a_string, '.')) == NULL)
  293. return LOG4C_CATEGORY_DEFAULT;
  294. *p = '\0';
  295. return a_string;
  296. }