rc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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'",
  134. (name->value ? name->value :"(not set)"));
  135. app = log4c_appender_get(name->value);
  136. if (type){
  137. sd_debug("appender type is '%s'",
  138. (type->value ? type->value: "(not set)"));
  139. log4c_appender_set_type(app, log4c_appender_type_get(type->value));
  140. #ifdef WITH_ROLLINGFILE
  141. if ( !strcasecmp(type->value, "rollingfile")) {
  142. rollingfile_udata_t *rfup = NULL;
  143. log4c_rollingpolicy_t *rollingpolicyp = NULL;
  144. sd_domnode_t* logdir = sd_domnode_attrs_get(anode,
  145. "logdir");
  146. sd_domnode_t* logprefix = sd_domnode_attrs_get(anode,
  147. "prefix");
  148. sd_domnode_t* rollingpolicy_name = sd_domnode_attrs_get(anode,
  149. "rollingpolicy");
  150. sd_debug("logdir='%s', prefix='%s', rollingpolicy='%s'",
  151. (logdir && logdir->value ? name->value :"(not set)"),
  152. (logprefix && logprefix->value ? logprefix->value :"(not set)"),
  153. (rollingpolicy_name && rollingpolicy_name->value ?
  154. rollingpolicy_name->value :"(not set)"));
  155. rfup = rollingfile_make_udata();
  156. rollingfile_udata_set_logdir(rfup, (char *)logdir->value);
  157. rollingfile_udata_set_files_prefix(rfup, (char *)logprefix->value);
  158. if (rollingpolicy_name){
  159. /* recover a rollingpolicy instance with this name */
  160. rollingpolicyp = log4c_rollingpolicy_get(rollingpolicy_name->value);
  161. /* connect that policy to this rollingfile appender conf */
  162. rollingfile_udata_set_policy(rfup, rollingpolicyp);
  163. log4c_appender_set_udata(app, rfup);
  164. /* allow the policy to initialize itself */
  165. log4c_rollingpolicy_init(rollingpolicyp, rfup);
  166. } else {
  167. /* no rollingpolicy specified, default to default sizewin */
  168. sd_debug("no rollingpolicy name specified--will default");
  169. }
  170. }
  171. #endif
  172. }
  173. if (layout)
  174. log4c_appender_set_layout(app, log4c_layout_get(layout->value));
  175. sd_debug("]");
  176. return 0;
  177. }
  178. /******************************************************************************/
  179. static int layout_load(log4c_rc_t* this, sd_domnode_t* anode)
  180. {
  181. sd_domnode_t* name = sd_domnode_attrs_get(anode, "name");
  182. sd_domnode_t* type = sd_domnode_attrs_get(anode, "type");
  183. log4c_layout_t* layout = NULL;
  184. if (!name) {
  185. sd_error("attribute \"name\" is missing");
  186. return -1;
  187. }
  188. layout = log4c_layout_get(name->value);
  189. if (type)
  190. log4c_layout_set_type(layout, log4c_layout_type_get(type->value));
  191. return 0;
  192. }
  193. #ifdef WITH_ROLLINGFILE
  194. /******************************************************************************/
  195. static int rollingpolicy_load(log4c_rc_t* this, sd_domnode_t* anode)
  196. {
  197. sd_domnode_t* name = sd_domnode_attrs_get(anode, "name");
  198. sd_domnode_t* type = sd_domnode_attrs_get(anode, "type");
  199. log4c_rollingpolicy_t* rpolicyp = NULL;
  200. long a_maxsize;
  201. sd_debug("rollingpolicy_load[");
  202. if (!name) {
  203. sd_error("attribute \"name\" is missing");
  204. return -1;
  205. }
  206. rpolicyp = log4c_rollingpolicy_get(name->value);
  207. if (type){
  208. log4c_rollingpolicy_set_type(rpolicyp,
  209. log4c_rollingpolicy_type_get(type->value));
  210. if (!strcasecmp(type->value, "sizewin")){
  211. sd_domnode_t* maxsize = sd_domnode_attrs_get(anode, "maxsize");
  212. sd_domnode_t* maxnum = sd_domnode_attrs_get(anode, "maxnum");
  213. rollingpolicy_sizewin_udata_t *sizewin_udatap = NULL;
  214. sd_debug("type='sizewin', maxsize='%s', maxnum='%s', "
  215. "rpolicyname='%s'",
  216. (maxsize && maxsize->value ? maxsize->value :"(not set)"),
  217. (maxnum && maxnum->value ? maxnum->value :"(not set)"),
  218. (name && name->value ? name->value :"(not set)"));
  219. /*
  220. * Get a new sizewin policy type and configure it.
  221. * Then attach it to the policy object.
  222. * Check to see if this policy already has a
  223. sw udata object. If so, leave as is except update
  224. the params
  225. */
  226. if ( !(sizewin_udatap = log4c_rollingpolicy_get_udata(rpolicyp))){
  227. sd_debug("creating new sizewin udata for this policy");
  228. sizewin_udatap = sizewin_make_udata();
  229. log4c_rollingpolicy_set_udata(rpolicyp,sizewin_udatap);
  230. a_maxsize = parse_byte_size(maxsize->value);
  231. if (a_maxsize)
  232. sizewin_udata_set_file_maxsize(sizewin_udatap, a_maxsize);
  233. else{
  234. sd_debug("When parsing %s a size of 0 was returned. Default size %d will be used",
  235. maxsize->value, ROLLINGPOLICY_SIZE_DEFAULT_MAX_FILE_SIZE);
  236. sizewin_udata_set_file_maxsize(sizewin_udatap, ROLLINGPOLICY_SIZE_DEFAULT_MAX_FILE_SIZE);
  237. }
  238. sizewin_udata_set_max_num_files(sizewin_udatap, atoi(maxnum->value));
  239. }else{
  240. sd_debug("policy already has a sizewin udata--just updating params");
  241. sizewin_udata_set_file_maxsize(sizewin_udatap, parse_byte_size(maxsize->value));
  242. sizewin_udata_set_max_num_files(sizewin_udatap, atoi(maxnum->value));
  243. /* allow the policy to initialize itself */
  244. log4c_rollingpolicy_init(rpolicyp,
  245. log4c_rollingpolicy_get_rfudata(rpolicyp));
  246. }
  247. }
  248. }
  249. sd_debug("]");
  250. return 0;
  251. }
  252. #endif
  253. /******************************************************************************/
  254. extern int log4c_rc_load(log4c_rc_t* this, const char* a_filename)
  255. {
  256. sd_list_iter_t* i = NULL;
  257. sd_domnode_t* node = NULL;
  258. sd_domnode_t* root_node = NULL;
  259. sd_debug("parsing file '%s'\n", a_filename);
  260. if (!this)
  261. return -1;
  262. root_node = sd_domnode_new(NULL, NULL);
  263. if (sd_domnode_load(root_node, a_filename) == -1) {
  264. sd_domnode_delete(root_node);
  265. return -1;
  266. }
  267. /* Check configuration file root node */
  268. if (strcmp(root_node->name, "log4c")) {
  269. sd_error("invalid root name %s", root_node->name);
  270. sd_domnode_delete(root_node);
  271. return -1;
  272. }
  273. /* Check configuration file revision */
  274. if ( (node = sd_domnode_attrs_get(root_node, "version")) != NULL)
  275. if (strcmp(version, node->value)) {
  276. sd_error("version mismatch: %s != %s", version, node->value);
  277. sd_domnode_delete(root_node);
  278. return -1;
  279. }
  280. /* backward compatibility. */
  281. if ( (node = sd_domnode_attrs_get(root_node, "cleanup")) != NULL) {
  282. sd_debug("attribute \"cleanup\" is deprecated");
  283. this->config.nocleanup = !atoi(node->value);
  284. }
  285. /* load configuration elements */
  286. for (i = sd_list_begin(root_node->children);
  287. i != sd_list_end(root_node->children);
  288. i = sd_list_iter_next(i))
  289. {
  290. sd_domnode_t* node = i->data;
  291. if (!strcmp(node->name, "category")) category_load(this, node);
  292. if (!strcmp(node->name, "appender")) appender_load(this, node);
  293. #ifdef WITH_ROLLINGFILE
  294. if (!strcmp(node->name, "rollingpolicy"))rollingpolicy_load(this, node);
  295. #endif
  296. if (!strcmp(node->name, "layout")) layout_load(this, node);
  297. if (!strcmp(node->name, "config")) config_load(this, node);
  298. }
  299. sd_domnode_delete(root_node);
  300. return 0;
  301. }
  302. /******************************************************************************/
  303. extern int log4c_load(const char* a_filename)
  304. {
  305. return log4c_rc_load(&__log4c_rc, a_filename);
  306. }