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 
9 #include <string.h>
10 #include <stdlib.h>
11 #include <errno.h>
12 
13 #include "zc_defs.h"
14 #include "category_table.h"
15 
zlog_category_table_profile(zc_hashtable_t * categories,int flag)16 void zlog_category_table_profile(zc_hashtable_t * categories, int flag)
17 {
18 	zc_hashtable_entry_t *a_entry;
19 	zlog_category_t *a_category;
20 
21 	zc_assert(categories,);
22 	zc_profile(flag, "-category_table[%p]-", categories);
23 	zc_hashtable_foreach(categories, a_entry) {
24 		a_category = (zlog_category_t *) a_entry->value;
25 		zlog_category_profile(a_category, flag);
26 	}
27 	return;
28 }
29 
30 /*******************************************************************************/
31 
zlog_category_table_del(zc_hashtable_t * categories)32 void zlog_category_table_del(zc_hashtable_t * categories)
33 {
34 	zc_assert(categories,);
35 	zc_hashtable_del(categories);
36 	zc_debug("zlog_category_table_del[%p]", categories);
37 	return;
38 }
39 
zlog_category_table_new(void)40 zc_hashtable_t *zlog_category_table_new(void)
41 {
42 	zc_hashtable_t *categories;
43 
44 	categories = zc_hashtable_new(20,
45 			 (zc_hashtable_hash_fn) zc_hashtable_str_hash,
46 			 (zc_hashtable_equal_fn) zc_hashtable_str_equal,
47 			 NULL, (zc_hashtable_del_fn) zlog_category_del);
48 	if (!categories) {
49 		zc_error("zc_hashtable_new fail");
50 		return NULL;
51 	} else {
52 		zlog_category_table_profile(categories, ZC_DEBUG);
53 		return categories;
54 	}
55 }
56 /*******************************************************************************/
zlog_category_table_update_rules(zc_hashtable_t * categories,zc_arraylist_t * new_rules)57 int zlog_category_table_update_rules(zc_hashtable_t * categories, zc_arraylist_t * new_rules)
58 {
59 	zc_hashtable_entry_t *a_entry;
60 	zlog_category_t *a_category;
61 
62 	zc_assert(categories, -1);
63 	zc_hashtable_foreach(categories, a_entry) {
64 		a_category = (zlog_category_t *) a_entry->value;
65 		if (zlog_category_update_rules(a_category, new_rules)) {
66 			zc_error("zlog_category_update_rules fail, try rollback");
67 			return -1;
68 		}
69 	}
70 	return 0;
71 }
72 
zlog_category_table_commit_rules(zc_hashtable_t * categories)73 void zlog_category_table_commit_rules(zc_hashtable_t * categories)
74 {
75 	zc_hashtable_entry_t *a_entry;
76 	zlog_category_t *a_category;
77 
78 	zc_assert(categories,);
79 	zc_hashtable_foreach(categories, a_entry) {
80 		a_category = (zlog_category_t *) a_entry->value;
81 		zlog_category_commit_rules(a_category);
82 	}
83 	return;
84 }
85 
zlog_category_table_rollback_rules(zc_hashtable_t * categories)86 void zlog_category_table_rollback_rules(zc_hashtable_t * categories)
87 {
88 	zc_hashtable_entry_t *a_entry;
89 	zlog_category_t *a_category;
90 
91 	zc_assert(categories,);
92 	zc_hashtable_foreach(categories, a_entry) {
93 		a_category = (zlog_category_t *) a_entry->value;
94 		zlog_category_rollback_rules(a_category);
95 	}
96 	return;
97 }
98 
99 /*******************************************************************************/
zlog_category_table_fetch_category(zc_hashtable_t * categories,const char * category_name,zc_arraylist_t * rules)100 zlog_category_t *zlog_category_table_fetch_category(zc_hashtable_t * categories,
101 			const char *category_name, zc_arraylist_t * rules)
102 {
103 	zlog_category_t *a_category;
104 
105 	zc_assert(categories, NULL);
106 
107 	/* 1st find category in global category map */
108 	a_category = zc_hashtable_get(categories, category_name);
109 	if (a_category) return a_category;
110 
111 	/* else not fount, create one */
112 	a_category = zlog_category_new(category_name, rules);
113 	if (!a_category) {
114 		zc_error("zc_category_new fail");
115 		return NULL;
116 	}
117 
118 	if(zc_hashtable_put(categories, a_category->name, a_category)) {
119 		zc_error("zc_hashtable_put fail");
120 		goto err;
121 	}
122 
123 	return a_category;
124 err:
125 	zlog_category_del(a_category);
126 	return NULL;
127 }
128 
129 /*******************************************************************************/
130