/* * scamper_debug.c * * $Id: scamper_debug.c,v 1.17 2007/05/09 03:20:33 mjl Exp $ * * routines to reduce the impact of debugging cruft in scamper's code. * * Copyright (C) 2003-2007 The University of Waikato * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 2. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include #include #include #include #include #include #include #include #if defined(__APPLE__) #include #endif #include "scamper.h" #include "scamper_debug.h" #include "utils.h" static char *timestamp_str(char *buf, const size_t len) { struct timeval tv; struct tm *tm; int ms; time_t t; buf[0] = '\0'; if(gettimeofday_wrap(&tv) == -1) return buf; t = tv.tv_sec; if((tm = localtime(&t)) == NULL) return buf; ms = tv.tv_usec / 1000; snprintf(buf, len, "[%02d:%02d:%02d:%03d] ", tm->tm_hour, tm->tm_min, tm->tm_sec, ms); return buf; } static char *error_str(const int e, char *(*error_itoa)(int), char *buf, const size_t len) { char *str; if(error_itoa == NULL || (str = error_itoa(e)) == NULL) { buf[0] = '\0'; return buf; } snprintf(buf, len, ": %s", str); return buf; } /* * printerror * * format a nice and consistent error string using the errno to string * conversion utilities and the arguments supplied */ void printerror(const int ecode, char *(*error_itoa)(int), const char *func, const char *format, ...) { char message[512]; char err[128]; char ts[16]; char fs[64]; va_list ap; va_start(ap, format); vsnprintf(message, sizeof(message), format, ap); va_end(ap); error_str(ecode, error_itoa, err, sizeof(err)); timestamp_str(ts, sizeof(ts)); if(func != NULL) snprintf(fs, sizeof(fs), "%s: ", func); else fs[0] = '\0'; fprintf(stderr, "%s%s%s%s\n", ts, fs, message, err); fflush(stderr); return; } #ifndef NDEBUG void scamper_debug(const char *func, const char *format, ...) { char message[512]; va_list ap; char ts[16]; char fs[64]; assert(format != NULL); va_start(ap, format); vsnprintf(message, sizeof(message), format, ap); va_end(ap); timestamp_str(ts, sizeof(ts)); if(func != NULL) snprintf(fs, sizeof(fs), "%s: ", func); else fs[0] = '\0'; fprintf(stderr, "%s%s%s\n", ts, fs, message); fflush(stderr); return; } #endif