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