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