Mercurial > hg > audiostuff
comparison spandsp-0.0.6pre17/src/logging.c @ 4:26cd8f1ef0b1
import spandsp-0.0.6pre17
| author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
|---|---|
| date | Fri, 25 Jun 2010 15:50:58 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 3:c6c5a16ce2f2 | 4:26cd8f1ef0b1 |
|---|---|
| 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 Lesser General Public License version 2.1, | |
| 14 * as 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 Lesser General Public License for more details. | |
| 20 * | |
| 21 * You should have received a copy of the GNU Lesser General Public | |
| 22 * License 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.32 2009/02/10 17:44:18 steveu Exp $ | |
| 26 */ | |
| 27 | |
| 28 /*! \file */ | |
| 29 | |
| 30 #if defined(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 #include "spandsp/private/logging.h" | |
| 50 | |
| 51 static void default_message_handler(int level, const char *text); | |
| 52 | |
| 53 static message_handler_func_t __span_message = &default_message_handler; | |
| 54 static error_handler_func_t __span_error = NULL; | |
| 55 | |
| 56 /* Note that this list *must* match the enum definition in logging.h */ | |
| 57 static const char *severities[] = | |
| 58 { | |
| 59 "NONE", | |
| 60 "ERROR", | |
| 61 "WARNING", | |
| 62 "PROTOCOL_ERROR", | |
| 63 "PROTOCOL_WARNING", | |
| 64 "FLOW", | |
| 65 "FLOW 2", | |
| 66 "FLOW 3", | |
| 67 "DEBUG 1", | |
| 68 "DEBUG 2", | |
| 69 "DEBUG 3" | |
| 70 }; | |
| 71 | |
| 72 static void default_message_handler(int level, const char *text) | |
| 73 { | |
| 74 fprintf(stderr, "%s", text); | |
| 75 } | |
| 76 /*- End of function --------------------------------------------------------*/ | |
| 77 | |
| 78 SPAN_DECLARE(int) span_log_test(logging_state_t *s, int level) | |
| 79 { | |
| 80 if (s && (s->level & SPAN_LOG_SEVERITY_MASK) >= (level & SPAN_LOG_SEVERITY_MASK)) | |
| 81 return TRUE; | |
| 82 return FALSE; | |
| 83 } | |
| 84 /*- End of function --------------------------------------------------------*/ | |
| 85 | |
| 86 SPAN_DECLARE(int) span_log(logging_state_t *s, int level, const char *format, ...) | |
| 87 { | |
| 88 char msg[1024 + 1]; | |
| 89 va_list arg_ptr; | |
| 90 int len; | |
| 91 struct tm *tim; | |
| 92 struct timeval nowx; | |
| 93 time_t now; | |
| 94 | |
| 95 if (span_log_test(s, level)) | |
| 96 { | |
| 97 va_start(arg_ptr, format); | |
| 98 len = 0; | |
| 99 if ((level & SPAN_LOG_SUPPRESS_LABELLING) == 0) | |
| 100 { | |
| 101 if ((s->level & SPAN_LOG_SHOW_DATE)) | |
| 102 { | |
| 103 gettimeofday(&nowx, NULL); | |
| 104 now = nowx.tv_sec; | |
| 105 tim = gmtime(&now); | |
| 106 len += snprintf(msg + len, | |
| 107 1024 - len, | |
| 108 "%04d/%02d/%02d %02d:%02d:%02d.%03d ", | |
| 109 tim->tm_year + 1900, | |
| 110 tim->tm_mon + 1, | |
| 111 tim->tm_mday, | |
| 112 tim->tm_hour, | |
| 113 tim->tm_min, | |
| 114 tim->tm_sec, | |
| 115 (int) nowx.tv_usec/1000); | |
| 116 } | |
| 117 /*endif*/ | |
| 118 if ((s->level & SPAN_LOG_SHOW_SAMPLE_TIME)) | |
| 119 { | |
| 120 now = s->elapsed_samples/s->samples_per_second; | |
| 121 tim = gmtime(&now); | |
| 122 len += snprintf(msg + len, | |
| 123 1024 - len, | |
| 124 "%02d:%02d:%02d.%03d ", | |
| 125 tim->tm_hour, | |
| 126 tim->tm_min, | |
| 127 tim->tm_sec, | |
| 128 (int) (s->elapsed_samples%s->samples_per_second)*1000/s->samples_per_second); | |
| 129 } | |
| 130 /*endif*/ | |
| 131 if ((s->level & SPAN_LOG_SHOW_SEVERITY) && (level & SPAN_LOG_SEVERITY_MASK) <= SPAN_LOG_DEBUG_3) | |
| 132 len += snprintf(msg + len, 1024 - len, "%s ", severities[level & SPAN_LOG_SEVERITY_MASK]); | |
| 133 /*endif*/ | |
| 134 if ((s->level & SPAN_LOG_SHOW_PROTOCOL) && s->protocol) | |
| 135 len += snprintf(msg + len, 1024 - len, "%s ", s->protocol); | |
| 136 /*endif*/ | |
| 137 if ((s->level & SPAN_LOG_SHOW_TAG) && s->tag) | |
| 138 len += snprintf(msg + len, 1024 - len, "%s ", s->tag); | |
| 139 /*endif*/ | |
| 140 } | |
| 141 /*endif*/ | |
| 142 len += vsnprintf(msg + len, 1024 - len, format, arg_ptr); | |
| 143 if (s->span_error && level == SPAN_LOG_ERROR) | |
| 144 s->span_error(msg); | |
| 145 else if (__span_error && level == SPAN_LOG_ERROR) | |
| 146 __span_error(msg); | |
| 147 else if (s->span_message) | |
| 148 s->span_message(level, msg); | |
| 149 else if (__span_message) | |
| 150 __span_message(level, msg); | |
| 151 /*endif*/ | |
| 152 va_end(arg_ptr); | |
| 153 return 1; | |
| 154 } | |
| 155 /*endif*/ | |
| 156 return 0; | |
| 157 } | |
| 158 /*- End of function --------------------------------------------------------*/ | |
| 159 | |
| 160 SPAN_DECLARE(int) span_log_buf(logging_state_t *s, int level, const char *tag, const uint8_t *buf, int len) | |
| 161 { | |
| 162 char msg[1024]; | |
| 163 int i; | |
| 164 int msg_len; | |
| 165 | |
| 166 if (span_log_test(s, level)) | |
| 167 { | |
| 168 msg_len = 0; | |
| 169 if (tag) | |
| 170 msg_len += snprintf(msg + msg_len, 1024 - msg_len, "%s", tag); | |
| 171 for (i = 0; i < len && msg_len < 800; i++) | |
| 172 msg_len += snprintf(msg + msg_len, 1024 - msg_len, " %02x", buf[i]); | |
| 173 msg_len += snprintf(msg + msg_len, 1024 - msg_len, "\n"); | |
| 174 return span_log(s, level, msg); | |
| 175 } | |
| 176 return 0; | |
| 177 } | |
| 178 /*- End of function --------------------------------------------------------*/ | |
| 179 | |
| 180 SPAN_DECLARE(int) span_log_set_level(logging_state_t *s, int level) | |
| 181 { | |
| 182 s->level = level; | |
| 183 | |
| 184 return 0; | |
| 185 } | |
| 186 /*- End of function --------------------------------------------------------*/ | |
| 187 | |
| 188 SPAN_DECLARE(int) span_log_set_tag(logging_state_t *s, const char *tag) | |
| 189 { | |
| 190 s->tag = tag; | |
| 191 | |
| 192 return 0; | |
| 193 } | |
| 194 /*- End of function --------------------------------------------------------*/ | |
| 195 | |
| 196 SPAN_DECLARE(int) span_log_set_protocol(logging_state_t *s, const char *protocol) | |
| 197 { | |
| 198 s->protocol = protocol; | |
| 199 | |
| 200 return 0; | |
| 201 } | |
| 202 /*- End of function --------------------------------------------------------*/ | |
| 203 | |
| 204 SPAN_DECLARE(int) span_log_set_sample_rate(logging_state_t *s, int samples_per_second) | |
| 205 { | |
| 206 s->samples_per_second = samples_per_second; | |
| 207 | |
| 208 return 0; | |
| 209 } | |
| 210 /*- End of function --------------------------------------------------------*/ | |
| 211 | |
| 212 SPAN_DECLARE(int) span_log_bump_samples(logging_state_t *s, int samples) | |
| 213 { | |
| 214 s->elapsed_samples += samples; | |
| 215 | |
| 216 return 0; | |
| 217 } | |
| 218 /*- End of function --------------------------------------------------------*/ | |
| 219 | |
| 220 SPAN_DECLARE(void) span_log_set_message_handler(logging_state_t *s, message_handler_func_t func) | |
| 221 { | |
| 222 s->span_message = func; | |
| 223 } | |
| 224 /*- End of function --------------------------------------------------------*/ | |
| 225 | |
| 226 SPAN_DECLARE(void) span_log_set_error_handler(logging_state_t *s, error_handler_func_t func) | |
| 227 { | |
| 228 s->span_error = func; | |
| 229 } | |
| 230 /*- End of function --------------------------------------------------------*/ | |
| 231 | |
| 232 SPAN_DECLARE(void) span_set_message_handler(message_handler_func_t func) | |
| 233 { | |
| 234 __span_message = func; | |
| 235 } | |
| 236 /*- End of function --------------------------------------------------------*/ | |
| 237 | |
| 238 SPAN_DECLARE(void) span_set_error_handler(error_handler_func_t func) | |
| 239 { | |
| 240 __span_error = func; | |
| 241 } | |
| 242 /*- End of function --------------------------------------------------------*/ | |
| 243 | |
| 244 SPAN_DECLARE(logging_state_t *) span_log_init(logging_state_t *s, int level, const char *tag) | |
| 245 { | |
| 246 if (s == NULL) | |
| 247 { | |
| 248 if ((s = (logging_state_t *) malloc(sizeof(*s))) == NULL) | |
| 249 return NULL; | |
| 250 } | |
| 251 s->span_error = __span_error; | |
| 252 s->span_message = __span_message; | |
| 253 s->level = level; | |
| 254 s->tag = tag; | |
| 255 s->protocol = NULL; | |
| 256 s->samples_per_second = SAMPLE_RATE; | |
| 257 s->elapsed_samples = 0; | |
| 258 | |
| 259 return s; | |
| 260 } | |
| 261 /*- End of function --------------------------------------------------------*/ | |
| 262 | |
| 263 SPAN_DECLARE(int) span_log_release(logging_state_t *s) | |
| 264 { | |
| 265 return 0; | |
| 266 } | |
| 267 /*- End of function --------------------------------------------------------*/ | |
| 268 | |
| 269 SPAN_DECLARE(int) span_log_free(logging_state_t *s) | |
| 270 { | |
| 271 if (s) | |
| 272 free(s); | |
| 273 return 0; | |
| 274 } | |
| 275 /*- End of function --------------------------------------------------------*/ | |
| 276 /*- End of file ------------------------------------------------------------*/ |
