/* 
 * File:   launch_logger.c
 * Author: blackpanther
 *
 * Created on October 28, 2010, 11:27 AM
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <string.h>

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#include "shared_data.h"

#define MY_ID "LOGGER"

#define handle(a,mess,code_error)   \
    if(a){                          \
        perror(mess);               \
        return code_error;          \
    }

int main(void) {

    int msg_id;
    int control;

    MessageBuffer theMsg;

    say(MY_ID,"");

    msg_id = msgget(UNIQUE_MSG_KEY, IPC_CREAT);
        handle(msg_id < 0,"LOGGER -> msgget: ",-1);

    theMsg.mtype	= 1;
    strcpy(&(theMsg.mtext[0]), "");

    do
    {
	control	= msgrcv(
                msg_id,                     // message queue
                (MessageBuffer *)&theMsg,   // message structure
                MAX_INPUT,                  // message size
                (long)0,                    // read the first message
                0);                         // wait

            handle(control < 0,"LOGGER -> msgrcv: ",-2);
            
            printf("\tLOGGER : control <%d>\n", control);

	printf("LOGGER -> MSG: <%s>\n", &(theMsg.mtext[0]));
    }while( strcmp(theMsg.mtext,STOP_KEYWORD) );

    say(MY_ID,"successfully terminated");
    
    return (EXIT_SUCCESS);
}

