1 /*******************************************************************
2 uLan Utilities Library - C library of basic reusable constructions
4 ul_logbase.c - base of standard logging facility
6 (C) Copyright 2003 by Pavel Pisa - Originator
8 The uLan utilities library can be used, copied and modified under
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.
20 See files COPYING and README for details.
22 *******************************************************************/
29 #include <sys/types.h>
38 #include <posix/unistd.h>
42 #include "ul_utdefs.h"
43 #include "ul_logbase.h"
47 int ul_log_cutoff_level;
49 #ifdef UL_LOG_NOINLINE
51 int ul_log_cond(ul_log_domain_t *domain, int level)
53 if(!domain || ((level&UL_LOGL_MASK) > UL_LOGL_MAX))
55 return (level&UL_LOGL_MASK) <= domain->level;
58 void ul_vlog(ul_log_domain_t *domain, int level,
59 const char *format, va_list ap)
61 if(!ul_log_cond(domain,level))
63 ul_vlog1(domain, level, format, ap);
66 void ul_log(ul_log_domain_t *domain, int level,
67 const char *format, ...)
71 if(!ul_log_cond(domain,level))
73 va_start (ap, format);
74 ul_vlog1(domain, level, format, ap);
78 #endif /*UL_LOG_NOINLINE*/
82 ul_log_fnc_default(ul_log_domain_t *domain, int level,
83 const char *format, va_list ap) UL_ATTR_REENTRANT;
85 ul_log_fnc_t *ul_log_output;
86 #if !defined(__RTL__) && !defined(SDCC)
87 FILE *ul_log_default_file;
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
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.
102 * Note: There is a global variable %ul_log_cutoff_level.
103 * Only the messages with %level <= %ul_log_cutoff_level will be logged.
107 ul_log1(ul_log_domain_t *domain, int level,
108 const char *format, ...)
111 va_start(ap, format);
112 ul_vlog1(domain,level,format,ap);
117 ul_vlog1(ul_log_domain_t *domain, int level,
118 const char *format, va_list ap)
120 if(ul_log_cutoff_level) {
121 if((level & UL_LOGL_MASK) > ul_log_cutoff_level) return;
123 if(ul_log_output==NULL) {
124 ul_log_check_default_output();
127 (*ul_log_output)(domain,level,format,ap);
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
138 ul_log_redir(ul_log_fnc_t *log_fnc, int add_flags)
140 if(log_fnc==NULL) log_fnc=ul_log_fnc_default;
141 ul_log_output=log_fnc;
144 #if !defined(__RTL__) && !defined(SDCC)
146 ul_log_fnc_default(ul_log_domain_t *domain, int level,
147 const char *format, va_list ap)
149 if(!(level&UL_LOGL_CONT)) {
152 fprintf(ul_log_default_file,"<%d>",level);
153 if(domain && domain->name)
154 fprintf(ul_log_default_file,"%s: ",domain->name);
156 vfprintf(ul_log_default_file, format, ap);
157 fflush(ul_log_default_file);
161 #elif defined(__RTL__)
163 ul_log_fnc_default(ul_log_domain_t *domain, int level,
164 const char *format, va_list ap)
166 if(!(level&UL_LOGL_CONT)) {
169 rtl_printf("<%d>",level);
170 if(domain && domain->name)
171 rtl_printf("%s: ",domain->name);
173 rtl_vprintf(format, ap);
179 ul_log_check_default_output(void)
181 #if !defined(__RTL__) && !defined(SDCC)
186 if(ul_log_output!=NULL)
189 ul_log_output=ul_log_fnc_default;
190 #if !defined(__RTL__) && !defined(SDCC)
191 if((log_fname=getenv("UL_LOG_FILENAME"))!=NULL){
192 ul_log_default_file=fopen(log_fname,"a");
194 if(ul_log_default_file==NULL)
195 ul_log_default_file=stderr;
196 if(!ul_debug_flg&&((s=getenv("UL_DEBUG_FLG"))!=NULL)){
197 ul_debug_flg=atoi(s);
199 if((s = getenv("UL_LOG_CUTTOFF")) != NULL) {
200 ul_log_cutoff_level = atoi(s);