/*
 * syslog.library Spy example
 *
 * This file is public domain.
 *
 * Author: Petri Nordlund <petrin@megabaud.fi>
 *
 * $Id: Spy.c 1.3 1995/11/01 19:07:32 petrin Exp petrin $
 *
 *
 * This program demostrates how to add a spy to syslog.library.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <proto/exec.h>
#include <dos/dos.h>
#include <proto/syslog.h>
#include <libraries/syslog.h>



struct Library *SysLogBase = NULL;
struct SysLogSpy *spy = NULL;


int
main(int argc, char **argv)
{
	ULONG spysig;
	BOOL quitting = FALSE;

	/*
	 * First open the syslog.library
	 */
	if(!(SysLogBase = OpenLibrary(SYSLOGNAME, SYSLOGVERSION)))
	{
		puts("Can't open \"syslog.library\".");
		exit(RETURN_FAIL);
	}

	/*
	 * Tell syslog.library we want to see the log messages too
	 */
	if(!(spy=AddSysLogSpy()))
	{
		puts("Can't connect with syslog.library.");
		exit(RETURN_FAIL);
	}

	/*
	 * Get the signal from message port
	 */
	spysig = 1L << spy->Spy_port->mp_SigBit;

	/*
	 * And now just wait for messages or CTRL_C
	 */
	while(!quitting)
	{
		ULONG signals;

		signals = Wait(spysig | SIGBREAKF_CTRL_C);

		if(signals & spysig)
		{
			struct SysLogMessage *msg;

			/*
			 * Get all messages
			 */
			while(msg=(struct SysLogMessage *) GetMsg(spy->Spy_port))
			{
				/*
				 * Ask syslog.library to put a time stamp to the message
				 */
				GetSysLogMsgTime(msg);

				/*
				 * Print the message. There's always \n at the end of the string.
				 * You'll find the message priority and facility in msg->pri.
				 */
				printf(msg->msg);

				/*
				 * Ask syslog.library to delete the message
				 */
				DeleteSysLogMessage(msg);
			}
		}

		if(signals & SIGBREAKF_CTRL_C)
			quitting = TRUE;
	}

	/*
	 * We are done. Syslog.library takes care of messages that might still be
	 * in the message port.
	 */
	if(spy)
		RemSysLogSpy(spy);

	if(SysLogBase)
		CloseLibrary(SysLogBase);

	return(RETURN_OK);
}
