/**********************************************************************
 * find out what reading and writing is going on
 *
 * $Author: brlewis $
 * $Source: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/debug.c,v $
 * $Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/debug.c,v 0.1 92/03/12 15:27:10 brlewis Exp $
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/

#include <mit-copyright.h>

#ifndef lint
static char rcsid_debug_c[] = "$Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/debug.c,v 0.1 92/03/12 15:27:10 brlewis Exp $";
#endif /* lint */

#undef read
#undef write
#include <stdio.h>

/* global variables */
FILE *debug_log = NULL;

debug_init()
{
  char filename[64];
  sprintf(filename, "/tmp/debug%d", getpid());
  if (!(debug_log = fopen(filename, "w")))
    {
      perror(filename);
      exit(1);
    }
  return;
}

int
debug_write(d, buf, nbytes)
     int d;
     char *buf;
     int nbytes;
{
  char line[64];

  if (!debug_log) debug_init();
  sprintf(line, "\nWRITE %4d BYTES TO %d\n", nbytes, d);
  write(debug_log->_file, line, strlen(line));
  write(debug_log->_file, buf, nbytes);
  return(write(d, buf, nbytes));
}

int
debug_read(d, buf, nbytes)
     int d;
     char *buf;
     int nbytes;
{
  char line[64];
  int cc;

  if (!debug_log) debug_init();
  cc = read(d, buf, nbytes);
  sprintf(line, "\nREAD %d (MAX %d) BYTES FROM %d\n", cc, nbytes, d);
  write(debug_log->_file, line, strlen(line));
  if (cc > 0) write(debug_log->_file, buf, cc);
  return(cc);
}
