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 >
Text File  |  1995-08-18  |  7KB  |  242 lines

  1. /*
  2.    Printed form of this source is Copyright (C) 1995 Coriolis
  3.    Group, Inc.  All rights reserved.  Individual users may
  4.    make printed copies for their own personal use.
  5.  
  6.    All other forms are Copyright (C) 1995 Tim Kientzle. All
  7.    rights reserved.
  8.  
  9. Redistribution in source or binary form is permitted only under
  10. the following conditions:
  11. 1. If you own a copy of `The Working Programmer's Guide To Serial
  12.    Protocols,' then you may redistribute this code as part of
  13.    a complete application program under the conditions
  14.    described in that book.  (See pages xiv, xv.)  In any case,
  15.    you must abide by terms 4-7 below.
  16. 2. Otherwise, if you have received this code as a part of an
  17.    application program, it may only be redistributed with the
  18.    complete source of that program, under whatever conditions
  19.    apply to redistribution of that program as a whole.
  20. 3. If you have received this source code by some other means,
  21.    you may not redistribute it without explicit written
  22.    permission from Tim Kientzle.
  23. 4. All advertising materials mentioning features or use of this
  24.    software must prominently display the following acknowledgement:
  25.       This product is partially based on source code appearing in
  26.       `The Working Programmer's Guide to Serial Protocols,'
  27.       Copyright (C) 1995 Coriolis Group, Inc. and Tim Kientzle.
  28. 5. All programs using this source code must display the above
  29.    acknowledgement prominently in the program documentation
  30.    and user interface.
  31. 6. Neither the name of the Tim Kientzle nor the Coriolis Group, Inc.,
  32.    may be used to endorse or promote products derived from this
  33.    software without specific prior written permission.
  34. 7. Any redistribution in source form must retain the above copyright
  35.    notice, this list of conditions, and the disclaimer below.
  36.  
  37. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  38. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  39. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  40. IN NO EVENT SHALL TIM KIENTZLE OR THE CORIOLIS GROUP BE LIABLE FOR
  41. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  43. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  44. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  45. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  46. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  47. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48.  
  49. */
  50.  
  51. #include "ftdebug.h"
  52. #include <stddef.h>
  53. #include <stdio.h>
  54. #ifdef _UCC
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #else
  58. int     fprintf ();
  59. int     sprintf ();
  60. FILE   *fopen (const char *, const char *);
  61. int     fclose (FILE *);
  62. int     fflush (FILE *);
  63. int     putc (int, FILE *);
  64. void   *malloc (size_t);
  65. void    free (void *);
  66. #endif
  67.  
  68. #include <time.h>
  69. time_t  time (time_t *);
  70. struct tm *localtime (const time_t *);
  71.  
  72. #ifndef _UCC
  73. #include <strings.h>
  74. #endif
  75.  
  76. size_t  strlen (const char *);
  77. typedef struct {
  78.    unsigned int filter;
  79.    int     thisSelect;
  80.    FILE   *outputFile;
  81. } DEBUG_PRIVATE;
  82. static void DebugPrint
  83.         (DEBUG_PRIVATE *pD, const char *msg, int length) {
  84.    if ((pD) && (pD->thisSelect & pD->filter)) {
  85.       while (length-- > 0)
  86.          putc (*msg++, pD->outputFile);
  87.    }
  88. }
  89. void    DebugInit
  90.         (DEBUG *pdPublic) {
  91.    DEBUG_PRIVATE *pD;
  92.  
  93.    *pdPublic = NULL;
  94.    pD = (DEBUG_PRIVATE *) malloc (sizeof (*pD));
  95.    if (pD) {
  96.       pD->filter = 0xffff;
  97.       pD->thisSelect = 0;
  98.       pD->outputFile = stderr;
  99.       *pdPublic = (void *) pD;
  100.    }
  101. }
  102. void    DebugDestroy
  103.         (DEBUG dPublic) {
  104.    DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
  105.  
  106.    if (pD) {
  107.       if (pD->outputFile != stderr)
  108.          fclose (pD->outputFile);
  109.       free (pD);
  110.    }
  111. }
  112. void    DebugFile
  113.         (DEBUG dPublic, const char *filename) {
  114.    DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
  115.    FILE   *f;
  116.  
  117.    if (pD) {
  118.       f = fopen (filename, "a");
  119.       if (f != NULL) {
  120.          time_t  t = time (NULL);
  121.  
  122.          pD->outputFile = f;
  123.          fprintf (f, "\n************************* %s\n", ctime (&t));
  124.       }
  125.    }
  126. }
  127. void    DebugSetFilter
  128.         (DEBUG dPublic, int filter) {
  129.    DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
  130.  
  131.    if (pD)
  132.       pD->filter = filter;
  133. }
  134. void    DebugBeginInternal
  135.         (DEBUG dPublic, int select, const char *file, int line) {
  136.    DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
  137.  
  138.    if (pD) {
  139.       time_t  t = time (NULL);
  140.       struct tm *pNow = localtime (&t);
  141.       char    tmpString[10];
  142.  
  143.       pD->thisSelect = select;
  144.       sprintf (tmpString, "%02d:%02d:%02d ",
  145.                pNow->tm_hour, pNow->tm_min, pNow->tm_sec);
  146.       DebugPrint (pD, tmpString, 9);
  147.       DebugPrint (pD, file, strlen (file));
  148.       sprintf (tmpString, ":%d:", line);
  149.       DebugPrint (pD, tmpString, strlen (tmpString));
  150.    }
  151.    return;
  152. }
  153. void    DebugEnd
  154.         (DEBUG dPublic) {
  155.    DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
  156.  
  157.    if (pD) {
  158.       DebugPrint (pD, "\n", 1);
  159.       if (pD->outputFile == stderr)
  160.          fflush (pD->outputFile);
  161.    }
  162. }
  163. void    DebugString
  164.         (DEBUG dPublic, const char *str) {
  165.    DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
  166.  
  167.    DebugPrint (pD, str, strlen (str));
  168. }
  169.  
  170. void    DebugStringCount
  171.         (DEBUG dPublic, const char *str, unsigned long length) {
  172.    DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
  173.    char    buff[10];
  174.    int     i;
  175.  
  176.    for (i = 0; i < length; i++) {
  177.       if ((str[i] >= 0x20) && (str[i] <= 0x7e))
  178.          DebugPrint (pD, str + i, 1);
  179.       else
  180.          switch (str[i]) {
  181.          case 0x0d:
  182.             DebugPrint (pD, "\\r", 2);
  183.             break;
  184.          case 0x0a:
  185.             DebugPrint (pD, "\\l", 2);
  186.             break;
  187.          case 0x08:
  188.             DebugPrint (pD, "\\b", 2);
  189.             break;
  190.          case 0x00:
  191.             DebugPrint (pD, "\\0", 2);
  192.             break;
  193.          default:
  194.             sprintf (buff, "\\x%02x", 255 & (int) (str[i]));
  195.             DebugPrint (pD, buff, 4);
  196.             break;
  197.          }
  198.    }
  199. }
  200. void    DebugInt
  201.         (DEBUG dPublic, const long l) {
  202.    DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
  203.    char    buff[20];
  204.  
  205.    sprintf (buff, "%ld", l);
  206.    DebugString (pD, buff);
  207. }
  208.  
  209. void    DebugUInt
  210.         (DEBUG dPublic, const unsigned long l) {
  211.    DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
  212.    char    buff[20];
  213.  
  214.    sprintf (buff, "%lu", l);
  215.    DebugString (pD, buff);
  216. }
  217.  
  218. void    DebugIntHex
  219.         (DEBUG dPublic, const unsigned long l) {
  220.    DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
  221.    char    buff[20];
  222.  
  223.    sprintf (buff, "0x%lX", l);
  224.    DebugString (pD, buff);
  225. }
  226.  
  227. void    DebugPtr
  228.         (DEBUG dPublic, const void *p) {
  229.    DEBUG_PRIVATE *pD = (DEBUG_PRIVATE *) dPublic;
  230.    char    buff[20];
  231.  
  232.    sprintf (buff, "%p", p);
  233.    DebugString (pD, buff);
  234. }
  235.  
  236. void    DebugChar
  237.         (DEBUG dPublic, const char c) {
  238.    DebugString (dPublic, "`");
  239.    DebugStringCount (dPublic, &c, 1);
  240.    DebugString (dPublic, "'");
  241. }
  242.