1 /*
2 $Id: mjpeg_logging.c,v 1.14 2005/05/18 17:21:47 sms00 Exp $
3
4 Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <string.h>
31
32 #include "mjpeg_logging.h"
33
34 #define MAX_DEFAULT_ID_SIZE 16
35 #define DEFAULT_DEFAULT_ID "???"
36
37 #ifdef HAVE___PROGNAME
38 extern const char *__progname;
39 #endif
40
41 static log_level_t mjpeg_log_verbosity = LOG_NONE;
42 static char default_handler_id[MAX_DEFAULT_ID_SIZE];
43 static char default_handler_id_is_set = 0;
44
default_mjpeg_log_filter(log_level_t level)45 static int default_mjpeg_log_filter( log_level_t level )
46 {
47 int verb_from_env;
48 if( mjpeg_log_verbosity == 0 )
49 {
50 char *mjpeg_verb_env = getenv("MJPEG_VERBOSITY");
51 if( mjpeg_verb_env != NULL )
52 {
53 verb_from_env = LOG_WARN-atoi(mjpeg_verb_env);
54 if( verb_from_env >= LOG_DEBUG && verb_from_env <= LOG_ERROR )
55 mjpeg_log_verbosity = (log_level_t)verb_from_env;
56 }
57 }
58 return (level < LOG_WARN && level < mjpeg_log_verbosity);
59 }
60
61 static mjpeg_log_filter_t _filter = default_mjpeg_log_filter;
62
63 static void
default_mjpeg_log_handler(log_level_t level,const char message[])64 default_mjpeg_log_handler(log_level_t level, const char message[])
65 {
66 const char *ids;
67
68 if( (*_filter)( level ) )
69 return;
70 if (default_handler_id_is_set) {
71 ids = default_handler_id;
72 } else {
73 #ifdef HAVE___PROGNAME
74 ids = __progname;
75 #else
76 ids = DEFAULT_DEFAULT_ID;
77 #endif
78 }
79 switch(level) {
80 case LOG_ERROR:
81 fprintf(stderr, "**ERROR: [%s] %s\n", ids, message);
82 break;
83 case LOG_DEBUG:
84 //fprintf(stderr, "--DEBUG: [%s] %s\n", ids, message);
85 break;
86 case LOG_WARN:
87 fprintf(stderr, "++ WARN: [%s] %s\n", ids, message);
88 break;
89 case LOG_INFO:
90 fprintf(stderr, " INFO: [%s] %s\n", ids, message);
91 break;
92 default:
93 assert(0);
94 }
95 }
96
97 static mjpeg_log_handler_t _handler = default_mjpeg_log_handler;
98
99
100 mjpeg_log_handler_t
mjpeg_log_set_handler(mjpeg_log_handler_t new_handler)101 mjpeg_log_set_handler(mjpeg_log_handler_t new_handler)
102 {
103 mjpeg_log_handler_t old_handler = _handler;
104
105 _handler = new_handler;
106
107 return old_handler;
108 }
109
110 /***************
111 *
112 * Set default log handlers degree of verboseity.
113 * 0 = quiet, 1 = info, 2 = debug
114 *
115 *************/
116
117 int
mjpeg_default_handler_verbosity(int verbosity)118 mjpeg_default_handler_verbosity(int verbosity)
119 {
120 int prev_verb = mjpeg_log_verbosity;
121 mjpeg_log_verbosity = (log_level_t)(LOG_WARN - verbosity);
122 return prev_verb;
123 }
124
125 /*
126 * Set identifier string used by default handler
127 *
128 */
129 int
mjpeg_default_handler_identifier(const char * new_id)130 mjpeg_default_handler_identifier(const char *new_id)
131 {
132 const char *s;
133 if (new_id == NULL) {
134 default_handler_id_is_set = 0;
135 return 0;
136 }
137 /* find basename of new_id (remove any directory prefix) */
138 if ((s = strrchr(new_id, '/')) == NULL)
139 s = new_id;
140 else
141 s = s + 1;
142 strncpy(default_handler_id, s, MAX_DEFAULT_ID_SIZE);
143 default_handler_id[MAX_DEFAULT_ID_SIZE-1] = '\0';
144 default_handler_id_is_set = 1;
145 return 0;
146 }
147
148
149 static void
mjpeg_logv(log_level_t level,const char format[],va_list args)150 mjpeg_logv(log_level_t level, const char format[], va_list args)
151 {
152 char buf[1024] = { 0, };
153
154 /* TODO: Original had a re-entrancy error trap to assist bug
155 finding. To make this work with multi-threaded applications a
156 lock is needed hence delete.
157 */
158
159
160 vsnprintf(buf, sizeof(buf)-1, format, args);
161
162 _handler(level, buf);
163 }
164
165 void
mjpeg_log(log_level_t level,const char format[],...)166 mjpeg_log(log_level_t level, const char format[], ...)
167 {
168 va_list args;
169 va_start (args, format);
170 mjpeg_logv(level, format, args);
171 va_end (args);
172 }
173
174 void
mjpeg_debug(const char format[],...)175 mjpeg_debug(const char format[], ...)
176 {
177 va_list args;
178 va_start (args, format);
179 mjpeg_logv(LOG_DEBUG, format, args);
180 va_end (args);
181 }
182
183 void
mjpeg_info(const char format[],...)184 mjpeg_info(const char format[], ...)
185 {
186 #if 0
187 va_list args;
188 va_start (args, format);
189 mjpeg_logv(LOG_INFO, format, args);
190 va_end (args);
191 #endif
192 }
193
194 void
mjpeg_warn(const char format[],...)195 mjpeg_warn(const char format[], ...)
196 {
197 va_list args;
198 va_start (args, format);
199 mjpeg_logv(LOG_WARN, format, args);
200 va_end (args);
201 }
202
203 void
mjpeg_error(const char format[],...)204 mjpeg_error(const char format[], ...)
205 {
206 va_list args;
207 va_start (args, format);
208 mjpeg_logv(LOG_ERROR, format, args);
209 va_end (args);
210 }
211
212 void
mjpeg_error_exit1(const char format[],...)213 mjpeg_error_exit1(const char format[], ...)
214 {
215 va_list args;
216 va_start( args, format );
217 mjpeg_logv( LOG_ERROR, format, args);
218 va_end(args);
219 exit(EXIT_FAILURE);
220 }
221
222
223 /*
224 * Local variables:
225 * c-file-style: "gnu"
226 * tab-width: 8
227 * indent-tabs-mode: nil
228 * End:
229 */
230