| 1 |
/*
|
| 2 |
debug.c: log (or not) messages
|
| 3 |
Copyright (C) 2003-2005 Ludovic Rousseau
|
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or
|
| 6 |
modify it under the terms of the GNU Lesser General Public
|
| 7 |
License as published by the Free Software Foundation; either
|
| 8 |
version 2.1 of the License, or (at your option) any later version.
|
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful,
|
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 13 |
Lesser General Public License for more details.
|
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public
|
| 16 |
License along with this library; if not, write to the Free Software
|
| 17 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 18 |
*/
|
| 19 |
|
| 20 |
/*
|
| 21 |
* $Id$
|
| 22 |
*/
|
| 23 |
|
| 24 |
|
| 25 |
#include "config.h"
|
| 26 |
#include "debug.h"
|
| 27 |
|
| 28 |
#include <stdarg.h>
|
| 29 |
#include <stdio.h>
|
| 30 |
#include <string.h>
|
| 31 |
|
| 32 |
#define DEBUG_BUF_SIZE ((256+20)*3+10)
|
| 33 |
|
| 34 |
static char DebugBuffer[DEBUG_BUF_SIZE];
|
| 35 |
|
| 36 |
#define LOG_TO_STDERR
|
| 37 |
|
| 38 |
void debug_msg(char *fmt, ...)
|
| 39 |
{
|
| 40 |
va_list argptr;
|
| 41 |
|
| 42 |
va_start(argptr, fmt);
|
| 43 |
vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
|
| 44 |
va_end(argptr);
|
| 45 |
|
| 46 |
#ifdef LOG_TO_STDERR
|
| 47 |
fprintf(stderr, "%s\n", DebugBuffer);
|
| 48 |
#endif
|
| 49 |
} /* debug_msg */
|
| 50 |
|
| 51 |
void debug_xxd(const char *msg, const unsigned char *buffer, const int len)
|
| 52 |
{
|
| 53 |
int i;
|
| 54 |
char *c, *debug_buf_end;
|
| 55 |
|
| 56 |
debug_buf_end = DebugBuffer + DEBUG_BUF_SIZE - 5;
|
| 57 |
|
| 58 |
strncpy(DebugBuffer, msg, sizeof(DebugBuffer)-1);
|
| 59 |
c = DebugBuffer + strlen(DebugBuffer);
|
| 60 |
|
| 61 |
for (i = 0; (i < len) && (c < debug_buf_end); ++i)
|
| 62 |
{
|
| 63 |
sprintf(c, "%02X ", (unsigned char)buffer[i]);
|
| 64 |
c += strlen(c);
|
| 65 |
}
|
| 66 |
|
| 67 |
#ifdef LOG_TO_STDERR
|
| 68 |
if (c >= debug_buf_end)
|
| 69 |
fprintf(stderr, "Debug buffer too short\n");
|
| 70 |
|
| 71 |
fprintf(stderr, "%s\n", DebugBuffer);
|
| 72 |
#endif
|
| 73 |
} /* debug_xxd */
|
| 74 |
|