/* * Copyright (c) 2002 Pedro Bastos * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the name of his contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY PEDRO BASTOS AND HIS CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * */ /* * Really compile only when requested. */ #ifdef _E_DEBUG #include #include #include #include #include "global.h" #define MAXLOGBUFSIZ 256 int init_debug(void) { if ((debug_fil = (FILE *) fopen("machmon-debug.txt", "a")) == NULL) return (1); if ((setvbuf(debug_fil, NULL, _IONBF, 0))) return (1); return (0); } /* * debug_inf_w() is a debug information wrapper, nicely redirecting data to the * appropriate debug output media, shall it be a file or a socket. */ void debug_inf_w(const char *data, ...) { char buff[MAXLOGBUFSIZ], tbuf[36]; time_t t; va_list debug_args; struct tm tm; if (data == NULL) { (void) snprintf(buff, MAXLOGBUFSIZ, "debug_inf_w(): some function called me without data to be written.\n"); exit(3); } (void) memset(&buff, 0x0, sizeof(buff)); va_start(debug_args, data); (void) vsnprintf(buff, MAXLOGBUFSIZ, data, debug_args); va_end(debug_args); if (strlen(buff) > MAXLOGBUFSIZ) return; (void) time(&t); tm = *localtime(&t); (void) strftime(tbuf, sizeof(tbuf) - 1, "%H:%M:%S ", &tm); if (1 != (int) fwrite(tbuf, strlen(tbuf), 1, debug_fil) || 1 != (int) fwrite(buff, strlen(buff), 1, debug_fil)) { (void) fprintf(stderr, "Warning: debug wrapper failed trying to output data\n"); } return; } #endif