#include <sys/file.h>

#include <rccl.h>
#include <stdio.h>
#include <signal.h>
#include "logger.h"

int logfd;
DATABUF *dataBuf;
extern logJ6Values();

logHandler()                                                    /*1*/
{
        writeLogFile (logfd, dataBuf->bufnum^1);
}

writeLogFile (fd, num)                                          /*2*/
FILE *fd;
int num;
{
        if (write (fd, dataBuf->buf[num], dataBuf->count[num]) < 0)
         { perror ("can't write to log file");
           exit (-1);
         }
}

main()
{
        MANIP *mnp;
        int fxnId;

        rcclSetOptions (RCCL_ERROR_EXIT);
        mnp = rcclCreate (getDefaultRobot(), 0);
        rcclStart();

        signal (SIGUSR1, logHandler);                           /*3*/
        if ((logfd = open ("log", O_RDWR|O_CREAT, 0666)) < 0)   /*4*/
         { perror ("can't open log file");
           exit (-1);
         }
        dataBuf = ALLOC_MEM (NULL, DATABUF, UNDEF);             /*5*/
        dataBuf->bufnum = UNDEF;
        fxnId = runMonitorFxn (mnp, logJ6Values, (int)dataBuf); /*6*/

        delay (10000.0);

        deleteMonitorFxn (mnp, fxnId);                          /*7*/
        rcclBlock();
        writeLogFile (logfd, dataBuf->bufnum);                  /*8*/
        close (logfd);
        rcclRelease(YES);
}
