]> rtime.felk.cvut.cz Git - ulut.git/blob - ulut/ul_logreg.c
uLUt: do not include malloc.h, use stdlib.h through ul_utmalloc.h.
[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 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 /* Argument syntax:
132  *
133  * <arg> := <assignment> | <arg> (':' | ',') <assignment>
134  * <assignment> := [ <domain> ('.' | '=') ] NUMBER
135  * <domain> := ( IDENTIFIER | "all" | "ALL" )
136  */
137 int ul_log_domain_arg2levels(const char *arg)
138 {
139   const char *p=arg;
140   const char *r;
141   int l;
142   char name[UL_MAX_DOMAIN_NAME+1];
143
144   if(!arg)
145     return -1;
146
147   while(*p){
148     if(isdigit(*p)){
149       strcpy(name,"all");
150     }else{
151       r=p;
152       while(isalnum(*p)||(*p=='_')) p++;
153       l=p-r;
154       if(l>UL_MAX_DOMAIN_NAME)
155         l=UL_MAX_DOMAIN_NAME;
156       memcpy(name,r,l);
157       name[l]=0;
158       if(*p&&(*p!='.')&&(*p!='='))
159         return p-arg;
160       p++;
161     }
162     r=p;
163     l=strtol(r,(char**)&p,0);
164     if(!p||(p==r)||(*p&&(*p!=':')&&(*p!=',')))
165       return p-arg;
166     if(ul_log_domain_setlevel(name, l)<0)
167       return p-arg;
168     if(*p)
169       p++;
170   }
171
172   return 0;
173 }
174
175 int ul_logreg_domain(ul_log_domain_t *domain)
176 {
177   ul_log_check_default_output();
178   if(!domain->level)
179     domain->level=ul_log_cutoff_level;
180   return ul_log_domains_insert(&ul_log_domains,domain);
181 }
182
183 int ul_logreg_domains_static(ul_log_domain_t *const *domains, int count)
184 {
185   if(!ul_log_domains.domains.items && !ul_log_domains.domains.alloc_count){
186     ul_log_domains.domains.items=(void**)domains;
187     ul_log_domains.domains.count=count;
188     ul_log_check_default_output();
189     while(count-->0){
190       if(!(*domains)->level)
191          (*domains)->level=ul_log_cutoff_level;
192       domains++;
193     }
194     return 0;
195   }
196   while(count-->0){
197     if(ul_logreg_domain(*(domains++))<0)
198       return -1;
199   }
200   return 0;
201 }
202
203 void ul_logreg_for_each_domain(ul_logreg_domain_cb_t *callback, void *context)
204 {
205   ul_log_domains_it_t it;
206   ul_log_domain_t *domain;
207   ul_for_each_it(ul_log_domains, &ul_log_domains, it) {
208     domain=ul_log_domains_it2item(&it);
209     if (callback) {
210       int ret;
211       ret = callback(domain, context);
212       if (ret)
213         break;
214     }
215   }
216 }