/*
 *
 * $Id: serialportlogger.h 2 2008-07-30 09:28:03Z mschneider $
 * $URL: svn://localhost/Bioloid/trunk/serialportlogger.h $
 
 * \version   $Rev: 2 $
 * \author    Markus Schneider
 * \date      17.07.08
 *
 */

#ifndef SERIALPORTLOGGER_H
#define SERIALPORTLOGGER_H

#include <stdio.h>
#include "serialport.h"

/**
 * This class log all read/write traffic from a SerialPort
 * 
 * USAGE:
 *   SerialPort *port= new SerialPort_FTDI();
 *   SerialPort *logger = new SerialPortLogger(port, stdout);
 *   Connection conn(logger);
 *   ...
 *   delete port;
 *   delete logger;
 */

class SerialPortLogger : public SerialPort
{
protected:
  FILE *fp;
  SerialPort *stream;
  bool toclose;
  bool was_write;  
  
public: 
 /**
  * This class log all read/write traffic from a SerialPort
  * @param stream The Serial port to monitor
  * @param filename The file to log
  * @param append Append to the file or start at the beginning
  */
  SerialPortLogger(SerialPort *stream, const char *filename, bool append=false);
  
 /**
  * This class log all read/write traffic from a SerialPort
  * @param stream The Serial port to monitor
  * @param fp The file to log in form of a filepointer
  */  
  SerialPortLogger(SerialPort *stream, FILE *fp);
  
 /*
  * All these methods are just wrapper around the original SerialPort
  * Read/write operations will be logged and then the read/write method
  * of the original SerialPort will be called.
  */
  virtual int setTimeout(unsigned int msec);
  virtual int readByte(unsigned char * byte);
  virtual int writeByte(unsigned char byte);
  virtual int writeData(unsigned char TxBuff[], unsigned int TxBytes);
  virtual int readData(unsigned char RxBuff[], unsigned int RxBytes);
  virtual ~SerialPortLogger();  
};

#endif
