]> rtime.felk.cvut.cz Git - ulut.git/blob - ulut/ul_logreg.c
Provided generic code to maintain and register log domains.
[ulut.git] / ulut / ul_logreg.c
1 /*******************************************************************
2   uLan Utilities Library - C library of basic reusable constructions
3
4   ul_logreg.c   - registration of logging domains
5
6   (C) Copyright 2006 by Pavel Pisa - Originator
7
8   The uLan utilities library can be used, copied and modified under
9   next licenses
10     - GPL - GNU General Public License
11     - LGPL - GNU Lesser General Public License
12     - MPL - Mozilla Public License
13     - and other licenses added by project originators
14   Code can be modified and re-distributed under any combination
15   of the above listed licenses. If contributor does not agree with
16   some of the licenses, he/she can delete appropriate line.
17   Warning, if you delete all lines, you are not allowed to
18   distribute source code and/or binaries utilizing code.
19   
20   See files COPYING and README for details.
21
22  *******************************************************************/
23
24 #include <string.h>
25 #include <ul_logbase.h>
26 #include <ul_logreg.h>
27 #include <ul_gsacust.h>
28
29
30 typedef struct ul_log_domains_t {
31  #ifdef UL_LOG_DOMAINS_STATIC
32   /*gsa_static_array_field_t domains;*/
33   struct {
34     ul_log_domain_t * const* items;
35     int count;
36   } domains;
37  #else /*UL_LOG_DOMAINS_STATIC*/
38   gsa_array_field_t domains;
39  #endif /*UL_LOG_DOMAINS_STATIC*/
40 } ul_log_domains_t;
41
42 typedef const char *ul_log_domains_key_t;
43
44 inline int
45 ul_log_domains_cmp_fnc(const ul_log_domains_key_t *a, const ul_log_domains_key_t *b)
46 {
47   return strcmp(*a,*b);
48 }
49
50 /* Custom array declarations */
51 #ifdef UL_LOG_DOMAINS_STATIC
52
53 GSA_STATIC_CUST_DEC(ul_log_domains, ul_log_domains_t, ul_log_domain_t, ul_log_domains_key_t,
54         domains, name, ul_log_domains_cmp_fnc)
55
56 GSA_STATIC_CUST_IMP(ul_log_domains, ul_log_domains_t, ul_log_domain_t, ul_log_domains_key_t,
57         domains, name, ul_log_domains_cmp_fnc, 0)
58
59 #else /*UL_LOG_DOMAINS_STATIC*/
60
61 GSA_CUST_DEC(ul_log_domains, ul_log_domains_t, ul_log_domain_t, ul_log_domains_key_t,
62         domains, name, ul_log_domains_cmp_fnc)
63
64 GSA_CUST_IMP(ul_log_domains, ul_log_domains_t, ul_log_domain_t, ul_log_domains_key_t,
65         domains, name, ul_log_domains_cmp_fnc, 0)
66
67 #endif /*UL_LOG_DOMAINS_STATIC*/
68
69 ul_log_domains_t ul_log_domains;
70
71 /*This is not ideal*/
72 extern int ul_log_cutoff_level;
73
74 int ul_log_domain_setlevel(const char *name, int setlevel)
75 {
76   int all_fl=0;
77
78   ul_log_domains_it_t it;
79   ul_log_domain_t *domain=NULL;
80
81   if((setlevel<0) || (setlevel>UL_LOGL_MAX))
82     return -1;
83
84   if(!name)
85     all_fl=1;
86   else
87     all_fl=!strcmp(name,"all");
88
89   if(!all_fl){
90     domain=ul_log_domains_find(&ul_log_domains,&name);
91     if(!domain){
92       return 1;
93     }
94     domain->level=setlevel;
95   }else{
96     ul_for_each_it(ul_log_domains, &ul_log_domains, it){
97       domain=ul_log_domains_it2item(&it);
98       domain->level=setlevel;
99     }
100   }
101
102   return 0;
103 }
104
105 int ul_log_domain_getlevel(const char *name)
106 {
107   ul_log_domain_t *domain=NULL;
108
109   if(!name)
110     return -1;
111
112   domain=ul_log_domains_find(&ul_log_domains,&name);
113   if(!domain){
114     return -1;
115   }
116   
117   return domain->level;
118 }
119
120 int ul_logreg_domain(ul_log_domain_t *domain)
121 {
122   ul_log_check_default_output();
123   if(!domain->level)
124     domain->level=ul_log_cutoff_level;
125   return ul_log_domains_insert(&ul_log_domains,domain);
126 }
127
128 int ul_logreg_domains_static(ul_log_domain_t *const *domains, int count)
129 {
130   if(!ul_log_domains.domains.items && !ul_log_domains.domains.alloc_count){
131     ul_log_domains.domains.items=(void**)domains;
132     ul_log_domains.domains.count=count;
133     ul_log_check_default_output();
134     while(count-->0){
135       if(!(*domains)->level)
136          (*domains)->level=ul_log_cutoff_level;
137       domains++;
138     }
139     return 0;
140   }
141   while(count-->0){
142     if(ul_logreg_domain(*(domains++))<0)
143       return -1;
144   }
145   return 0;
146 }