]> rtime.felk.cvut.cz Git - ulut.git/blob - ulut/ul_logreg.c
6f63d95eb8586ed0f69beba222690b4d4ae9064d
[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 <ctype.h>
25 #include <string.h>
26 #ifndef __RTL__
27 #include <stdlib.h>
28 #endif
29 #include <ul_logbase.h>
30 #include <ul_logreg.h>
31 #include <ul_gsacust.h>
32
33
34 typedef struct ul_log_domains_t {
35  #ifdef UL_LOG_DOMAINS_STATIC
36   /*gsa_static_array_field_t domains;*/
37   struct {
38     ul_log_domain_t * const* items;
39     int count;
40   } domains;
41  #else /*UL_LOG_DOMAINS_STATIC*/
42   gsa_array_field_t domains;
43  #endif /*UL_LOG_DOMAINS_STATIC*/
44 } ul_log_domains_t;
45
46 typedef const char *ul_log_domains_key_t;
47
48 inline int
49 ul_log_domains_cmp_fnc(const ul_log_domains_key_t *a, const ul_log_domains_key_t *b)
50 {
51   return strcmp(*a,*b);
52 }
53
54 /* Custom array declarations */
55 #ifdef UL_LOG_DOMAINS_STATIC
56
57 GSA_STATIC_CUST_DEC(ul_log_domains, ul_log_domains_t, ul_log_domain_t, ul_log_domains_key_t,
58         domains, name, ul_log_domains_cmp_fnc)
59
60 GSA_STATIC_CUST_IMP(ul_log_domains, ul_log_domains_t, ul_log_domain_t, ul_log_domains_key_t,
61         domains, name, ul_log_domains_cmp_fnc, 0)
62
63 #else /*UL_LOG_DOMAINS_STATIC*/
64
65 GSA_CUST_DEC(ul_log_domains, ul_log_domains_t, ul_log_domain_t, ul_log_domains_key_t,
66         domains, name, ul_log_domains_cmp_fnc)
67
68 GSA_CUST_IMP(ul_log_domains, ul_log_domains_t, ul_log_domain_t, ul_log_domains_key_t,
69         domains, name, ul_log_domains_cmp_fnc, 0)
70
71 #endif /*UL_LOG_DOMAINS_STATIC*/
72
73 ul_log_domains_t ul_log_domains;
74
75 /*This is not ideal*/
76 extern int ul_log_cutoff_level;
77
78 int ul_log_domain_setlevel(const char *name, int setlevel)
79 {
80   int all_fl=0;
81
82   ul_log_domains_it_t it;
83   ul_log_domain_t *domain=NULL;
84
85   if(setlevel<0)
86     return -1;
87
88   if(setlevel>UL_LOGL_MAX)
89     setlevel=UL_LOGL_MAX;
90
91   if(!name)
92     all_fl=1;
93   else
94     all_fl=!strcmp(name,"all") || !strcmp(name,"ALL");
95
96   if(!all_fl){
97     domain=ul_log_domains_find(&ul_log_domains,&name);
98     if(!domain){
99       return 1;
100     }
101     domain->level=setlevel;
102   }else{
103     ul_for_each_it(ul_log_domains, &ul_log_domains, it){
104       domain=ul_log_domains_it2item(&it);
105       domain->level=setlevel;
106     }
107   }
108
109   return 0;
110 }
111
112 int ul_log_domain_getlevel(const char *name)
113 {
114   ul_log_domain_t *domain=NULL;
115
116   if(!name)
117     return -1;
118
119   domain=ul_log_domains_find(&ul_log_domains,&name);
120   if(!domain){
121     return -1;
122   }
123   
124   return domain->level;
125 }
126
127 #ifndef UL_MAX_DOMAIN_NAME
128 #define UL_MAX_DOMAIN_NAME 20
129 #endif /*UL_MAX_DOMAIN_NAME*/
130
131 int ul_log_domain_arg2levels(const char *arg)
132 {
133   const char *p=arg;
134   const char *r;
135   int l;
136   char name[UL_MAX_DOMAIN_NAME+1];
137
138   if(!arg)
139     return -1;
140
141   while(*p){
142     if(isdigit(*p)){
143       strcpy(name,"all");
144     }else{
145       r=p;
146       while(isalnum(*p)||(*p=='_')) p++;
147       l=p-r;
148       if(l>UL_MAX_DOMAIN_NAME)
149         l=UL_MAX_DOMAIN_NAME;
150       memcpy(name,r,l);
151       name[l]=0;
152       if(*p&&(*p!='.')&&(*p!='='))
153         return p-arg;
154       p++;
155     }
156     r=p;
157     l=strtol(r,(char**)&p,0);
158     if(!p||(p==r)||(*p&&(*p!=':')&&(*p!=',')))
159       return p-arg;
160     if(ul_log_domain_setlevel(name, l)<0)
161       return p-arg;
162     if(*p)
163       p++;
164   }
165
166   return 0;
167 }
168
169 int ul_logreg_domain(ul_log_domain_t *domain)
170 {
171   ul_log_check_default_output();
172   if(!domain->level)
173     domain->level=ul_log_cutoff_level;
174   return ul_log_domains_insert(&ul_log_domains,domain);
175 }
176
177 int ul_logreg_domains_static(ul_log_domain_t *const *domains, int count)
178 {
179   if(!ul_log_domains.domains.items && !ul_log_domains.domains.alloc_count){
180     ul_log_domains.domains.items=(void**)domains;
181     ul_log_domains.domains.count=count;
182     ul_log_check_default_output();
183     while(count-->0){
184       if(!(*domains)->level)
185          (*domains)->level=ul_log_cutoff_level;
186       domains++;
187     }
188     return 0;
189   }
190   while(count-->0){
191     if(ul_logreg_domain(*(domains++))<0)
192       return -1;
193   }
194   return 0;
195 }
196
197 void ul_logreg_for_each_domain(ul_logreg_domain_cb_t *callback, void *context)
198 {
199   ul_log_domains_it_t it;
200   ul_log_domain_t *domain;
201   ul_for_each_it(ul_log_domains, &ul_log_domains, it) {
202     domain=ul_log_domains_it2item(&it);
203     if (callback) {
204       int ret;
205       ret = callback(domain, context);
206       if (ret)
207         break;
208     }
209   }
210 }