rc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. static const char version[] = "1.2.1";
  2. /*
  3. * rc.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/rc.h>
  13. #include <log4c/category.h>
  14. #include <log4c/appender.h>
  15. #include <log4c/layout.h>
  16. #include <log4c/appender_type_rollingfile.h>
  17. #include <log4c/rollingpolicy.h>
  18. #include <log4c/rollingpolicy_type_sizewin.h>
  19. #include <sd/error.h>
  20. #include <sd/domnode.h>
  21. #include <sd/malloc.h>
  22. #include <sd/sd_xplatform.h>
  23. #include <sd/factory.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. static log4c_rc_t __log4c_rc = { { 0, 0, 0, 0 } };
  27. log4c_rc_t* const log4c_rc = &__log4c_rc;
  28. /******************************************************************************/
  29. static long parse_byte_size (const char *astring)
  30. {
  31. /* Parse size in bytes depending on the suffix. Valid suffixes are KB, MB and GB */
  32. size_t sz = strlen (astring);
  33. long res = strtol(astring, (char **) NULL, 10);
  34. if (res <= 0)
  35. return 0;
  36. if (astring[ sz - 1 ] == 'B') {
  37. switch (astring[ sz - 2 ]) {
  38. case 'K':
  39. res *= 1024;
  40. break;
  41. case 'M':
  42. res *= 1024 * 1024;
  43. break;
  44. case 'G':
  45. res *= 1024 * 1024 * 1024;
  46. break;
  47. default:
  48. sd_debug("Wrong suffix parsing size in bytes for string %s, ignoring suffix",
  49. astring);
  50. }
  51. }
  52. sd_debug("Parsed size parameter %s to value %ld",astring, res);
  53. return (res);
  54. }
  55. /******************************************************************************/
  56. static int config_load(log4c_rc_t* this, sd_domnode_t* anode)
  57. {
  58. sd_list_iter_t* i = NULL;
  59. for (i = sd_list_begin(anode->children); i != sd_list_end(anode->children);
  60. i = sd_list_iter_next(i))
  61. {
  62. sd_domnode_t* node = i->data;
  63. if (!strcmp(node->name, "nocleanup")) {
  64. this->config.nocleanup = atoi(node->value);
  65. if (this->config.nocleanup)
  66. sd_debug("deactivating log4c cleanup");
  67. }
  68. if (!strcmp(node->name, "bufsize")) {
  69. this->config.bufsize = parse_byte_size(node->value);
  70. if (this->config.bufsize)
  71. sd_debug("using fixed buffer size of %d bytes",
  72. this->config.bufsize);
  73. else
  74. sd_debug("using dynamic allocated buffer");
  75. }
  76. if (!strcmp(node->name, "debug")) {
  77. sd_domnode_t* level = sd_domnode_attrs_get(node, "level");
  78. if (level) {
  79. this->config.debug = atoi(level->value);
  80. sd_debug("activating log4c debugging. level = %d", this->config.debug);
  81. }
  82. }
  83. if (!strcmp(node->name, "reread")) {
  84. this->config.reread = atoi(node->value);
  85. sd_debug("log4crc reread is %d",this->config.reread);
  86. if (0 == this->config.reread)
  87. sd_debug("deactivating log4crc reread");
  88. }
  89. }
  90. return 0;
  91. }
  92. /******************************************************************************/
  93. static int category_load(log4c_rc_t* this, sd_domnode_t* anode)
  94. {
  95. sd_domnode_t* name = sd_domnode_attrs_get(anode, "name");
  96. sd_domnode_t* priority = sd_domnode_attrs_get(anode, "priority");
  97. sd_domnode_t* additivity = sd_domnode_attrs_get(anode, "additivity");
  98. sd_domnode_t* appender = sd_domnode_attrs_get(anode, "appender");
  99. log4c_category_t* cat = NULL;
  100. if (!name) {
  101. sd_error("attribute \"name\" is missing");
  102. return -1;
  103. }
  104. cat = log4c_category_get(name->value);
  105. if (priority)
  106. log4c_category_set_priority(
  107. cat, log4c_priority_to_int(priority->value));
  108. if (additivity) {
  109. if (!strcasecmp(additivity->value, "false")) {
  110. log4c_category_set_additivity(cat, 0);
  111. } else if (!strcasecmp(additivity->value, "true")) {
  112. log4c_category_set_additivity(cat, 1);
  113. } else {
  114. sd_error("additivity value is invalid : %s", additivity->value);
  115. }
  116. }
  117. if (appender)
  118. log4c_category_set_appender(
  119. cat, log4c_appender_get(appender->value));
  120. return 0;
  121. }
  122. /******************************************************************************/
  123. static int appender_load(log4c_rc_t* this, sd_domnode_t* anode)
  124. {
  125. sd_domnode_t* name = sd_domnode_attrs_get(anode, "name");
  126. sd_domnode_t* type = sd_domnode_attrs_get(anode, "type");
  127. sd_domnode_t* layout = sd_domnode_attrs_get(anode, "layout");
  128. log4c_appender_t* app = NULL;
  129. if (!name) {
  130. sd_error("attribute \"name\" is missing");
  131. return -1;
  132. }
  133. sd_debug("appender_load[name='%s'", (name->value ? name->value :"(not set)"));
  134. app = log4c_appender_get(name->value);
  135. if (type){
  136. sd_debug("appender type is '%s'",(type->value ? type->value: "(not set)"));
  137. log4c_appender_set_type(app, log4c_appender_type_get(type->value));
  138. #ifdef WITH_ROLLINGFILE
  139. if ( !strcasecmp(type->value, "rollingfile")) {
  140. rollingfile_udata_t *rfup = NULL;
  141. log4c_rollingpolicy_t *rollingpolicyp = NULL;
  142. sd_domnode_t* logdir = sd_domnode_attrs_get(anode,"logdir");
  143. sd_domnode_t* logprefix = sd_domnode_attrs_get(anode,"prefix");
  144. sd_domnode_t* logext = sd_domnode_attrs_get(anode,"ext");
  145. sd_domnode_t* rollingpolicy_name = sd_domnode_attrs_get(anode,"rollingpolicy");
  146. sd_debug("logdir='%s', prefix='%s', rollingpolicy='%s'",
  147. (logdir && logdir->value ? name->value :"(not set)"),
  148. (logprefix && logprefix->value ? logprefix->value :"(not set)"),
  149. (logext && logext->value ? logext->value :"txt"),
  150. (rollingpolicy_name && rollingpolicy_name->value ? rollingpolicy_name->value :"(not set)"));
  151. rfup = rollingfile_make_udata();
  152. rollingfile_udata_set_logdir(rfup, (char *)logdir->value);
  153. rollingfile_udata_set_files_prefix(rfup, (char *)logprefix->value);
  154. rollingfile_udata_set_files_ext(rfup, (char *)logext->value);
  155. if (rollingpolicy_name){
  156. /* recover a rollingpolicy instance with this name */
  157. rollingpolicyp = log4c_rollingpolicy_get(rollingpolicy_name->value);
  158. /* connect that policy to this rollingfile appender conf */
  159. rollingfile_udata_set_policy(rfup, rollingpolicyp);
  160. log4c_appender_set_udata(app, rfup);
  161. /* allow the policy to initialize itself */
  162. log4c_rollingpolicy_init(rollingpolicyp, rfup);
  163. } else {
  164. /* no rollingpolicy specified, default to default sizewin */
  165. sd_debug("no rollingpolicy name specified--will default");
  166. }
  167. }
  168. #endif
  169. }
  170. if (layout)
  171. log4c_appender_set_layout(app, log4c_layout_get(layout->value));
  172. sd_debug("]");
  173. return 0;
  174. }
  175. /******************************************************************************/
  176. static int layout_load(log4c_rc_t* this, sd_domnode_t* anode)
  177. {
  178. sd_domnode_t* name = sd_domnode_attrs_get(anode, "name");
  179. sd_domnode_t* type = sd_domnode_attrs_get(anode, "type");
  180. log4c_layout_t* layout = NULL;
  181. if (!name) {
  182. sd_error("attribute \"name\" is missing");
  183. return -1;
  184. }
  185. layout = log4c_layout_get(name->value);
  186. if (type)
  187. log4c_layout_set_type(layout, log4c_layout_type_get(type->value));
  188. return 0;
  189. }
  190. #ifdef WITH_ROLLINGFILE
  191. /******************************************************************************/
  192. static int rollingpolicy_load(log4c_rc_t* this, sd_domnode_t* anode)
  193. {
  194. sd_domnode_t* name = sd_domnode_attrs_get(anode, "name");
  195. sd_domnode_t* type = sd_domnode_attrs_get(anode, "type");
  196. log4c_rollingpolicy_t* rpolicyp = NULL;
  197. long a_maxsize;
  198. sd_debug("rollingpolicy_load[");
  199. if (!name) {
  200. sd_error("attribute \"name\" is missing");
  201. return -1;
  202. }
  203. rpolicyp = log4c_rollingpolicy_get(name->value);
  204. if (type){
  205. log4c_rollingpolicy_set_type(rpolicyp,
  206. log4c_rollingpolicy_type_get(type->value));
  207. if (!strcasecmp(type->value, "sizewin")){
  208. sd_domnode_t* maxsize = sd_domnode_attrs_get(anode, "maxsize");
  209. sd_domnode_t* maxnum = sd_domnode_attrs_get(anode, "maxnum");
  210. rollingpolicy_sizewin_udata_t *sizewin_udatap = NULL;
  211. sd_debug("type='sizewin', maxsize='%s', maxnum='%s', "
  212. "rpolicyname='%s'",
  213. (maxsize && maxsize->value ? maxsize->value :"(not set)"),
  214. (maxnum && maxnum->value ? maxnum->value :"(not set)"),
  215. (name && name->value ? name->value :"(not set)"));
  216. /*
  217. * Get a new sizewin policy type and configure it.
  218. * Then attach it to the policy object.
  219. * Check to see if this policy already has a
  220. sw udata object. If so, leave as is except update
  221. the params
  222. */
  223. if ( !(sizewin_udatap = log4c_rollingpolicy_get_udata(rpolicyp))){
  224. sd_debug("creating new sizewin udata for this policy");
  225. sizewin_udatap = sizewin_make_udata();
  226. log4c_rollingpolicy_set_udata(rpolicyp,sizewin_udatap);
  227. a_maxsize = parse_byte_size(maxsize->value);
  228. if (a_maxsize)
  229. sizewin_udata_set_file_maxsize(sizewin_udatap, a_maxsize);
  230. else{
  231. sd_debug("When parsing %s a size of 0 was returned. Default size %d will be used",
  232. maxsize->value, ROLLINGPOLICY_SIZE_DEFAULT_MAX_FILE_SIZE);
  233. sizewin_udata_set_file_maxsize(sizewin_udatap, ROLLINGPOLICY_SIZE_DEFAULT_MAX_FILE_SIZE);
  234. }
  235. sizewin_udata_set_max_num_files(sizewin_udatap, atoi(maxnum->value));
  236. }else{
  237. sd_debug("policy already has a sizewin udata--just updating params");
  238. sizewin_udata_set_file_maxsize(sizewin_udatap, parse_byte_size(maxsize->value));
  239. sizewin_udata_set_max_num_files(sizewin_udatap, atoi(maxnum->value));
  240. /* allow the policy to initialize itself */
  241. log4c_rollingpolicy_init(rpolicyp,
  242. log4c_rollingpolicy_get_rfudata(rpolicyp));
  243. }
  244. }
  245. }
  246. sd_debug("]");
  247. return 0;
  248. }
  249. #endif
  250. /******************************************************************************/
  251. extern int log4c_rc_load(log4c_rc_t* this, const char* a_filename)
  252. {
  253. sd_list_iter_t* i = NULL;
  254. sd_domnode_t* node = NULL;
  255. sd_domnode_t* root_node = NULL;
  256. sd_debug("parsing file '%s'\n", a_filename);
  257. if (!this)
  258. return -1;
  259. root_node = sd_domnode_new(NULL, NULL);
  260. if (sd_domnode_load(root_node, a_filename) == -1) {
  261. sd_domnode_delete(root_node);
  262. return -1;
  263. }
  264. /* Check configuration file root node */
  265. if (strcmp(root_node->name, "log4c")) {
  266. sd_error("invalid root name %s", root_node->name);
  267. sd_domnode_delete(root_node);
  268. return -1;
  269. }
  270. /* Check configuration file revision */
  271. if ( (node = sd_domnode_attrs_get(root_node, "version")) != NULL)
  272. {
  273. if (strcmp(version, node->value))
  274. {
  275. sd_error("version mismatch: %s != %s", version, node->value);
  276. sd_domnode_delete(root_node);
  277. return -1;
  278. }
  279. }
  280. /* backward compatibility. */
  281. if ( (node = sd_domnode_attrs_get(root_node, "cleanup")) != NULL)
  282. {
  283. sd_debug("attribute \"cleanup\" is deprecated");
  284. this->config.nocleanup = !atoi(node->value);
  285. }
  286. /* load configuration elements */
  287. for (i = sd_list_begin(root_node->children); i != sd_list_end(root_node->children); i = sd_list_iter_next(i))
  288. {
  289. sd_domnode_t* node = i->data;
  290. if (!strcmp(node->name, "category")) category_load(this, node);
  291. if (!strcmp(node->name, "appender")) appender_load(this, node);
  292. #ifdef WITH_ROLLINGFILE
  293. if (!strcmp(node->name, "rollingpolicy"))rollingpolicy_load(this, node);
  294. #endif
  295. if (!strcmp(node->name, "layout")) layout_load(this, node);
  296. if (!strcmp(node->name, "config")) config_load(this, node);
  297. }
  298. sd_domnode_delete(root_node);
  299. return 0;
  300. }
  301. /******************************************************************************/
  302. extern int log4c_load(const char* a_filename)
  303. {
  304. return log4c_rc_load(&__log4c_rc, a_filename);
  305. }