5
|
1 /*
|
|
2 * SpanDSP - a series of DSP components for telephony
|
|
3 *
|
|
4 * logging.c - error and debug logging.
|
|
5 *
|
|
6 * Written by Steve Underwood <steveu@coppice.org>
|
|
7 *
|
|
8 * Copyright (C) 2005 Steve Underwood
|
|
9 *
|
|
10 * All rights reserved.
|
|
11 *
|
|
12 * This program is free software; you can redistribute it and/or modify
|
|
13 * it under the terms of the GNU General Public License version 2, as
|
|
14 * published by the Free Software Foundation.
|
|
15 *
|
|
16 * This program is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with this program; if not, write to the Free Software
|
|
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
24 *
|
|
25 * $Id: logging.c,v 1.22 2006/11/30 15:41:47 steveu Exp $
|
|
26 */
|
|
27
|
|
28 /*! \file */
|
|
29
|
|
30 #ifdef HAVE_CONFIG_H
|
|
31 #include <config.h>
|
|
32 #endif
|
|
33
|
|
34 #include <limits.h>
|
|
35 #include <stdio.h>
|
|
36 #include <stdarg.h>
|
|
37 #include <fcntl.h>
|
|
38 #include <stdlib.h>
|
|
39 #include <inttypes.h>
|
|
40 #include <string.h>
|
|
41 #include <errno.h>
|
|
42 #include <signal.h>
|
|
43 #include <sys/time.h>
|
|
44 #include <time.h>
|
|
45
|
|
46 #include "spandsp/telephony.h"
|
|
47 #include "spandsp/logging.h"
|
|
48
|
|
49 static void default_message_handler(int level, const char *text);
|
|
50
|
|
51 static message_handler_func_t __span_message = *default_message_handler;
|
|
52 static error_handler_func_t __span_error = NULL;
|
|
53
|
|
54 static const char *severities[] =
|
|
55 {
|
|
56 "NONE",
|
|
57 "ERROR",
|
|
58 "WARNING",
|
|
59 "PROTOCOL_ERROR",
|
|
60 "PROTOCOL_WARNING",
|
|
61 "FLOW",
|
|
62 "FLOW 2",
|
|
63 "FLOW 3",
|
|
64 "DEBUG 1",
|
|
65 "DEBUG 2",
|
|
66 "DEBUG 3"
|
|
67 };
|
|
68
|
|
69 static void default_message_handler(int level, const char *text)
|
|
70 {
|
|
71 fprintf(stderr, "%s", text);
|
|
72 }
|
|
73 /*- End of function --------------------------------------------------------*/
|
|
74
|
|
75 int span_log_test(logging_state_t *s, int level)
|
|
76 {
|
|
77 if (s && (s->level & SPAN_LOG_SEVERITY_MASK) >= (level & SPAN_LOG_SEVERITY_MASK))
|
|
78 return TRUE;
|
|
79 return FALSE;
|
|
80 }
|
|
81 /*- End of function --------------------------------------------------------*/
|
|
82
|
|
83 int span_log(logging_state_t *s, int level, const char *format, ...)
|
|
84 {
|
|
85 char msg[1024 + 1];
|
|
86 va_list arg_ptr;
|
|
87 int len;
|
|
88 struct tm *tim;
|
|
89 struct timeval nowx;
|
|
90 time_t now;
|
|
91
|
|
92 if (span_log_test(s, level))
|
|
93 {
|
|
94 va_start(arg_ptr, format);
|
|
95 len = 0;
|
|
96 if ((level & SPAN_LOG_SUPPRESS_LABELLING) == 0)
|
|
97 {
|
|
98 if ((s->level & SPAN_LOG_SHOW_DATE))
|
|
99 {
|
|
100 gettimeofday(&nowx, NULL);
|
|
101 now = nowx.tv_sec;
|
|
102 tim = gmtime(&now);
|
|
103 snprintf(msg + len,
|
|
104 1024 - len,
|
|
105 "%04d/%02d/%02d %02d:%02d:%02d.%03d ",
|
|
106 tim->tm_year + 1900,
|
|
107 tim->tm_mon + 1,
|
|
108 tim->tm_mday,
|
|
109 tim->tm_hour,
|
|
110 tim->tm_min,
|
|
111 tim->tm_sec,
|
|
112 (int) nowx.tv_usec/1000);
|
|
113 len += (int) strlen(msg + len);
|
|
114 }
|
|
115 /*endif*/
|
|
116 if ((s->level & SPAN_LOG_SHOW_SAMPLE_TIME))
|
|
117 {
|
|
118 now = s->elapsed_samples/s->samples_per_second;
|
|
119 tim = gmtime(&now);
|
|
120 snprintf(msg + len,
|
|
121 1024 - len,
|
|
122 "%02d:%02d:%02d.%03d ",
|
|
123 tim->tm_hour,
|
|
124 tim->tm_min,
|
|
125 tim->tm_sec,
|
|
126 (int) (s->elapsed_samples%s->samples_per_second)*1000/s->samples_per_second);
|
|
127 len += (int) strlen(msg + len);
|
|
128 }
|
|
129 /*endif*/
|
|
130 if ((s->level & SPAN_LOG_SHOW_SEVERITY) && (level & SPAN_LOG_SEVERITY_MASK) <= SPAN_LOG_DEBUG_3)
|
|
131 len += snprintf(msg + len, 1024 - len, "%s ", severities[level & SPAN_LOG_SEVERITY_MASK]);
|
|
132 /*endif*/
|
|
133 if ((s->level & SPAN_LOG_SHOW_PROTOCOL) && s->protocol)
|
|
134 len += snprintf(msg + len, 1024 - len, "%s ", s->protocol);
|
|
135 /*endif*/
|
|
136 if ((s->level & SPAN_LOG_SHOW_TAG) && s->tag)
|
|
137 len += snprintf(msg + len, 1024 - len, "%s ", s->tag);
|
|
138 /*endif*/
|
|
139 }
|
|
140 /*endif*/
|
|
141 len += vsnprintf(msg + len, 1024 - len, format, arg_ptr);
|
|
142 if (s->span_error && level == SPAN_LOG_ERROR)
|
|
143 s->span_error(msg);
|
|
144 else if (__span_error && level == SPAN_LOG_ERROR)
|
|
145 __span_error(msg);
|
|
146 else if (s->span_message)
|
|
147 s->span_message(level, msg);
|
|
148 else if (__span_message)
|
|
149 __span_message(level, msg);
|
|
150 /*endif*/
|
|
151 va_end(arg_ptr);
|
|
152 return 1;
|
|
153 }
|
|
154 /*endif*/
|
|
155 return 0;
|
|
156 }
|
|
157 /*- End of function --------------------------------------------------------*/
|
|
158
|
|
159 int span_log_buf(logging_state_t *s, int level, const char *tag, const uint8_t *buf, int len)
|
|
160 {
|
|
161 char msg[1024];
|
|
162 int i;
|
|
163 int msg_len;
|
|
164
|
|
165 if (span_log_test(s, level))
|
|
166 {
|
|
167 msg_len = 0;
|
|
168 if (tag)
|
|
169 msg_len += snprintf(msg + msg_len, 1024 - msg_len, "%s", tag);
|
|
170 for (i = 0; i < len && msg_len < 800; i++)
|
|
171 msg_len += snprintf(msg + msg_len, 1024 - msg_len, " %02x", buf[i]);
|
|
172 msg_len += snprintf(msg + msg_len, 1024 - msg_len, "\n");
|
|
173 return span_log(s, level, msg);
|
|
174 }
|
|
175 return 0;
|
|
176 }
|
|
177 /*- End of function --------------------------------------------------------*/
|
|
178
|
|
179 int span_log_init(logging_state_t *s, int level, const char *tag)
|
|
180 {
|
|
181 s->span_error = __span_error;
|
|
182 s->span_message = __span_message;
|
|
183 s->level = level;
|
|
184 s->tag = tag;
|
|
185 s->protocol = NULL;
|
|
186 s->samples_per_second = SAMPLE_RATE;
|
|
187 s->elapsed_samples = 0;
|
|
188
|
|
189 return 0;
|
|
190 }
|
|
191 /*- End of function --------------------------------------------------------*/
|
|
192
|
|
193 int span_log_set_level(logging_state_t *s, int level)
|
|
194 {
|
|
195 s->level = level;
|
|
196
|
|
197 return 0;
|
|
198 }
|
|
199 /*- End of function --------------------------------------------------------*/
|
|
200
|
|
201 int span_log_set_tag(logging_state_t *s, const char *tag)
|
|
202 {
|
|
203 s->tag = tag;
|
|
204
|
|
205 return 0;
|
|
206 }
|
|
207 /*- End of function --------------------------------------------------------*/
|
|
208
|
|
209 int span_log_set_protocol(logging_state_t *s, const char *protocol)
|
|
210 {
|
|
211 s->protocol = protocol;
|
|
212
|
|
213 return 0;
|
|
214 }
|
|
215 /*- End of function --------------------------------------------------------*/
|
|
216
|
|
217 int span_log_set_sample_rate(logging_state_t *s, int samples_per_second)
|
|
218 {
|
|
219 s->samples_per_second = samples_per_second;
|
|
220
|
|
221 return 0;
|
|
222 }
|
|
223 /*- End of function --------------------------------------------------------*/
|
|
224
|
|
225 int span_log_bump_samples(logging_state_t *s, int samples)
|
|
226 {
|
|
227 s->elapsed_samples += samples;
|
|
228
|
|
229 return 0;
|
|
230 }
|
|
231 /*- End of function --------------------------------------------------------*/
|
|
232
|
|
233 void span_log_set_message_handler(logging_state_t *s, message_handler_func_t func)
|
|
234 {
|
|
235 s->span_message = func;
|
|
236 }
|
|
237 /*- End of function --------------------------------------------------------*/
|
|
238
|
|
239 void span_log_set_error_handler(logging_state_t *s, error_handler_func_t func)
|
|
240 {
|
|
241 s->span_error = func;
|
|
242 }
|
|
243 /*- End of function --------------------------------------------------------*/
|
|
244
|
|
245 void span_set_message_handler(message_handler_func_t func)
|
|
246 {
|
|
247 __span_message = func;
|
|
248 }
|
|
249 /*- End of function --------------------------------------------------------*/
|
|
250
|
|
251 void span_set_error_handler(error_handler_func_t func)
|
|
252 {
|
|
253 __span_error = func;
|
|
254 }
|
|
255 /*- End of function --------------------------------------------------------*/
|
|
256 /*- End of file ------------------------------------------------------------*/
|