category_table.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * This file is part of the zlog Library.
  3. *
  4. * Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING in base directory.
  7. */
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <errno.h>
  11. #include "zc_defs.h"
  12. #include "category_table.h"
  13. void zlog_category_table_profile(zc_hashtable_t * categories, int flag)
  14. {
  15. zc_hashtable_entry_t *a_entry;
  16. zlog_category_t *a_category;
  17. zc_assert(categories,);
  18. zc_profile(flag, "-category_table[%p]-", categories);
  19. zc_hashtable_foreach(categories, a_entry) {
  20. a_category = (zlog_category_t *) a_entry->value;
  21. zlog_category_profile(a_category, flag);
  22. }
  23. return;
  24. }
  25. /*******************************************************************************/
  26. void zlog_category_table_del(zc_hashtable_t * categories)
  27. {
  28. zc_assert(categories,);
  29. zc_hashtable_del(categories);
  30. zc_debug("zlog_category_table_del[%p]", categories);
  31. return;
  32. }
  33. zc_hashtable_t *zlog_category_table_new(void)
  34. {
  35. zc_hashtable_t *categories;
  36. categories = zc_hashtable_new(20,
  37. (zc_hashtable_hash_fn) zc_hashtable_str_hash,
  38. (zc_hashtable_equal_fn) zc_hashtable_str_equal,
  39. NULL, (zc_hashtable_del_fn) zlog_category_del);
  40. if (!categories) {
  41. zc_error("zc_hashtable_new fail");
  42. return NULL;
  43. } else {
  44. zlog_category_table_profile(categories, ZC_DEBUG);
  45. return categories;
  46. }
  47. }
  48. /*******************************************************************************/
  49. int zlog_category_table_update_rules(zc_hashtable_t * categories, zc_arraylist_t * new_rules)
  50. {
  51. zc_hashtable_entry_t *a_entry;
  52. zlog_category_t *a_category;
  53. zc_assert(categories, -1);
  54. zc_hashtable_foreach(categories, a_entry) {
  55. a_category = (zlog_category_t *) a_entry->value;
  56. if (zlog_category_update_rules(a_category, new_rules)) {
  57. zc_error("zlog_category_update_rules fail, try rollback");
  58. return -1;
  59. }
  60. }
  61. return 0;
  62. }
  63. void zlog_category_table_commit_rules(zc_hashtable_t * categories)
  64. {
  65. zc_hashtable_entry_t *a_entry;
  66. zlog_category_t *a_category;
  67. zc_assert(categories,);
  68. zc_hashtable_foreach(categories, a_entry) {
  69. a_category = (zlog_category_t *) a_entry->value;
  70. zlog_category_commit_rules(a_category);
  71. }
  72. return;
  73. }
  74. void zlog_category_table_rollback_rules(zc_hashtable_t * categories)
  75. {
  76. zc_hashtable_entry_t *a_entry;
  77. zlog_category_t *a_category;
  78. zc_assert(categories,);
  79. zc_hashtable_foreach(categories, a_entry) {
  80. a_category = (zlog_category_t *) a_entry->value;
  81. zlog_category_rollback_rules(a_category);
  82. }
  83. return;
  84. }
  85. /*******************************************************************************/
  86. zlog_category_t *zlog_category_table_fetch_category(zc_hashtable_t * categories,
  87. const char *category_name, zc_arraylist_t * rules)
  88. {
  89. zlog_category_t *a_category;
  90. zc_assert(categories, NULL);
  91. /* 1st find category in global category map */
  92. a_category = zc_hashtable_get(categories, category_name);
  93. if (a_category) return a_category;
  94. /* else not found, create one */
  95. a_category = zlog_category_new(category_name, rules);
  96. if (!a_category) {
  97. zc_error("zc_category_new fail");
  98. return NULL;
  99. }
  100. if(zc_hashtable_put(categories, a_category->name, a_category)) {
  101. zc_error("zc_hashtable_put fail");
  102. goto err;
  103. }
  104. return a_category;
  105. err:
  106. zlog_category_del(a_category);
  107. return NULL;
  108. }
  109. /*******************************************************************************/