home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
TELECOM
/
xyz.lzh
/
ftdebug.c
< prev
next >
Wrap
Text File
|
1995-08-18
|
7KB
|
242 lines
/*
Printed form of this source is Copyright (C) 1995 Coriolis
Group, Inc. All rights reserved. Individual users may
make printed copies for their own personal use.
All other forms are Copyright (C) 1995 Tim Kientzle. All
rights reserved.
Redistribution in source or binary form is permitted only under
the following conditions:
1. If you own a copy of `The Working Programmer's Guide To Serial
Protocols,' then you may redistribute this code as part of
a complete application program under the conditions
described in that book. (See pages xiv, xv.) In any case,
you must abide by terms 4-7 below.
2. Otherwise, if you have received this code as a part of an
application program, it may only be redistributed with the
complete source of that program, under whatever conditions
apply to redistribution of that program as a whole.
3. If you have received this source code by some other means,
you may not redistribute it without explicit written
permission from Tim Kientzle.
4. All advertising materials mentioning features or use of this
software must prominently display the following acknowledgement:
This product is partially based on source code appearing in
`The Working Programmer's Guide to Serial Protocols,'
Copyright (C) 1995 Coriolis Group, Inc. and Tim Kientzle.
5. All programs using this source code must display the above
acknowledgement prominently in the program documentation
and user interface.
6. Neither the name of the Tim Kientzle nor the Coriolis Group, Inc.,
may be used to endorse or promote products derived from this
software without specific prior written permission.
7. Any redistribution in source form must retain the above copyright
notice, this list of conditions, and the disclaimer below.
THIS SOFTWARE IS PROVIDED ``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 TIM KIENTZLE OR THE CORIOLIS GROUP 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.
*/
#include "ftdebug.h"
#include <stddef.h>
#include <stdio.h>
#ifdef _UCC
#include <stdlib.h>
#include <string.h>
#else
int fprintf ();
int sprintf ();
FILE *fopen (const char *, const char *);
int fclose (FILE *);
int fflush (FILE *);
int putc (int, FILE *);
void *malloc (size_t);
void free (void *);
#endif
#include <time.h>
time_t time (time_t *);
struct tm *localtime (const time_t *);
#ifndef _UCC
#include <strings.h>
#endif
size_t strlen (const char *);
typedef struct {
unsigned int filter;
int thisSelect;
FILE *outputFile;
} DEBUG_PRIVATE;
static void DebugPrint
(DEBUG_PRIVATE *pD, const char *msg, int length) {
if ((pD) && (pD->thisSelect & pD->filter)) {
while (length-- > 0)
putc (*msg++, pD->outputFile);
}
}
void DebugInit
(DEBUG *pdPublic) {
DEBUG_PRIVATE *pD;
*pdPublic = NULL;
pD = (DEBUG_PRIVATE *) malloc (sizeof (*pD));
if (pD) {
pD->filter = 0xffff;
pD->thisSelect = 0;
pD->outputFile = stderr;
*pdPublic = (void *) pD;
}
}
void DebugDestroy
(DEBUG dPublic) {
DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
if (pD) {
if (pD->outputFile != stderr)
fclose (pD->outputFile);
free (pD);
}
}
void DebugFile
(DEBUG dPublic, const char *filename) {
DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
FILE *f;
if (pD) {
f = fopen (filename, "a");
if (f != NULL) {
time_t t = time (NULL);
pD->outputFile = f;
fprintf (f, "\n************************* %s\n", ctime (&t));
}
}
}
void DebugSetFilter
(DEBUG dPublic, int filter) {
DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
if (pD)
pD->filter = filter;
}
void DebugBeginInternal
(DEBUG dPublic, int select, const char *file, int line) {
DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
if (pD) {
time_t t = time (NULL);
struct tm *pNow = localtime (&t);
char tmpString[10];
pD->thisSelect = select;
sprintf (tmpString, "%02d:%02d:%02d ",
pNow->tm_hour, pNow->tm_min, pNow->tm_sec);
DebugPrint (pD, tmpString, 9);
DebugPrint (pD, file, strlen (file));
sprintf (tmpString, ":%d:", line);
DebugPrint (pD, tmpString, strlen (tmpString));
}
return;
}
void DebugEnd
(DEBUG dPublic) {
DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
if (pD) {
DebugPrint (pD, "\n", 1);
if (pD->outputFile == stderr)
fflush (pD->outputFile);
}
}
void DebugString
(DEBUG dPublic, const char *str) {
DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
DebugPrint (pD, str, strlen (str));
}
void DebugStringCount
(DEBUG dPublic, const char *str, unsigned long length) {
DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
char buff[10];
int i;
for (i = 0; i < length; i++) {
if ((str[i] >= 0x20) && (str[i] <= 0x7e))
DebugPrint (pD, str + i, 1);
else
switch (str[i]) {
case 0x0d:
DebugPrint (pD, "\\r", 2);
break;
case 0x0a:
DebugPrint (pD, "\\l", 2);
break;
case 0x08:
DebugPrint (pD, "\\b", 2);
break;
case 0x00:
DebugPrint (pD, "\\0", 2);
break;
default:
sprintf (buff, "\\x%02x", 255 & (int) (str[i]));
DebugPrint (pD, buff, 4);
break;
}
}
}
void DebugInt
(DEBUG dPublic, const long l) {
DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
char buff[20];
sprintf (buff, "%ld", l);
DebugString (pD, buff);
}
void DebugUInt
(DEBUG dPublic, const unsigned long l) {
DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
char buff[20];
sprintf (buff, "%lu", l);
DebugString (pD, buff);
}
void DebugIntHex
(DEBUG dPublic, const unsigned long l) {
DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
char buff[20];
sprintf (buff, "0x%lX", l);
DebugString (pD, buff);
}
void DebugPtr
(DEBUG dPublic, const void *p) {
DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
char buff[20];
sprintf (buff, "%p", p);
DebugString (pD, buff);
}
void DebugChar
(DEBUG dPublic, const char c) {
DebugString (dPublic, "`");
DebugStringCount (dPublic, &c, 1);
DebugString (dPublic, "'");
}