]> rtime.felk.cvut.cz Git - ulut.git/blob - ulut/ul_logbase.c
Conversion to new logging support finished.
[ulut.git] / ulut / ul_logbase.c
1 /*******************************************************************
2   uLan Utilities Library - C library of basic reusable constructions
3
4   ul_logbase.c  - base of standard logging facility
5
6   (C) Copyright 2003 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 #ifndef __RTL__
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <stdarg.h>
30
31 #else /*__RTL__*/
32
33 #include <rtl.h>
34 #include <string.h>
35 #include <signal.h>
36 #include <posix/unistd.h>
37
38 #endif /*__RTL__*/
39
40 #include "ul_utdefs.h"
41 #include "ul_logbase.h"
42
43
44 int ul_debug_flg;
45 int ul_log_cutoff_level;   
46
47 #ifdef UL_LOG_NOINLINE
48
49 int ul_log_cond(ul_log_domain_t *domain, int level)
50 {
51   if(!domain || ((level&UL_LOGL_MASK) > UL_LOGL_MAX))
52     return 0;
53   return (level&UL_LOGL_MASK) <= domain->level;
54 }
55
56 void ul_vlog(ul_log_domain_t *domain, int level,
57         const char *format, va_list ap)
58 {
59   if(!ul_log_cond(domain,level))
60     return;
61   ul_vlog1(domain, level, format, ap);
62 }
63
64 void ul_log(ul_log_domain_t *domain, int level,
65         const char *format, ...)
66 {
67   va_list ap;
68
69   if(!ul_log_cond(domain,level))
70     return;
71   va_start (ap, format);
72   ul_vlog1(domain, level, format, ap);
73   va_end (ap);
74 }
75
76 #endif /*UL_LOG_NOINLINE*/
77
78
79 void
80 ul_log_fnc_default(ul_log_domain_t *domain, int level,
81         const char *format, va_list ap);
82
83 ul_log_fnc_t *ul_log_output;
84 #ifndef __RTL__
85 FILE *ul_log_default_file;
86 #endif /*__RTL__*/
87
88 /**
89  * ul_log - generic logging facility for ULUT library
90  * @domain: pointer to domain of debugging messages
91  * @level:  severity level
92  * @format: printf style format followed by arguments
93  *
94  * This functions is used for logging of various events.
95  * If not overridden by application, logged messages goes to the stderr.
96  * Environment variable %UL_LOG_FILENAME can be used to redirect 
97  * output to file. Environment variable %UL_DEBUG_FLG can be used
98  * to select different set of logged events through ul_debug_flg.
99  *
100  * Note: There is a global variable %ul_log_cutoff_level. 
101  * Only the messages with %level <= %ul_log_cutoff_level will be logged.
102  */           
103
104 void
105 ul_log1(ul_log_domain_t *domain, int level,
106        const char *format, ...)
107 {
108   va_list ap;
109   va_start(ap, format);
110   ul_vlog1(domain,level,format,ap);
111   va_end(ap); 
112 }
113
114 void
115 ul_vlog1(ul_log_domain_t *domain, int level,
116        const char *format, va_list ap)
117 {
118   if(ul_log_cutoff_level) {
119       if((level & UL_LOGL_MASK) > ul_log_cutoff_level) return;
120   }
121   if(ul_log_output==NULL) {
122     ul_log_check_default_output();
123   }
124   if(ul_log_output)
125     (*ul_log_output)(domain,level,format,ap);
126 }
127
128 /**
129  * ul_log_redir - redirects default log output function
130  * @log_fnc: new log output function. Value NULL resets
131  *           to default function
132  * @add_flags: some more flags
133  */
134
135 void
136 ul_log_redir(ul_log_fnc_t *log_fnc, int add_flags)
137 {
138   if(log_fnc==NULL) log_fnc=ul_log_fnc_default;
139   ul_log_output=log_fnc;
140 }
141
142 #ifndef __RTL__
143 void
144 ul_log_fnc_default(ul_log_domain_t *domain, int level,
145         const char *format, va_list ap)
146 {
147   if(!(level&UL_LOGL_CONT)) {
148     level&=UL_LOGL_MASK;
149     if(level)
150       fprintf(ul_log_default_file,"<%d>",level);
151     if(domain && domain->name)
152       fprintf(ul_log_default_file,"%s: ",domain->name);
153   }
154   vfprintf(ul_log_default_file, format, ap);
155   fflush(ul_log_default_file);
156 }
157
158
159 #else /*__RTL__*/
160 void
161 ul_log_fnc_default(ul_log_domain_t *domain, int level,
162         const char *format, va_list ap)
163 {
164   if(!(level&UL_LOGL_CONT)) {
165     level&=UL_LOGL_MASK;
166     if(level)
167       rtl_printf("<%d>",level);
168     if(domain && domain->name)
169       rtl_printf("%s: ",domain->name);
170   }
171   rtl_vprintf(format, ap);
172 }
173
174 #endif /*__RTL__*/
175
176 int
177 ul_log_check_default_output(void)
178 {
179  #ifndef __RTL__
180   char *s;
181   char *log_fname;
182  #endif /*__RTL__*/
183
184   if(ul_log_output!=NULL)
185     return 0;
186
187   ul_log_output=ul_log_fnc_default;
188  #ifndef __RTL__
189   if((log_fname=getenv("UL_LOG_FILENAME"))!=NULL){
190     ul_log_default_file=fopen(log_fname,"a");
191   }
192   if(ul_log_default_file==NULL)
193     ul_log_default_file=stderr;
194   if(!ul_debug_flg&&((s=getenv("UL_DEBUG_FLG"))!=NULL)){
195     ul_debug_flg=atoi(s);
196   }
197   if((s = getenv("UL_LOG_CUTTOFF")) != NULL) {
198       ul_log_cutoff_level = atoi(s);
199   }
200  #endif /*__RTL__*/
201   return 0;
202 }
203