// Filename:	errlog.C
// Contents:	the error logger interface object (for bbs interface)
// Author:		Greg Shaw
// Created:		7/11/93

/*
This file is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

In addition to the permissions in the GNU General Public License, the
Free Software Foundation gives you unlimited permission to link the
compiled version of this file with other programs, and to distribute
those programs without any restriction coming from the use of this
file.  (The General Public License restrictions do apply in other
respects; for example, they cover modification of the file, and
distribution when not linked into another program.)

This file is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifndef _ERRLOG_C_
#define _ERRLOG_C_

#include "bbshdr.h"			// the world

// Method:		ap_log
// Purpose:		send an error message to the error logger daemon
// Input:		msg - the message to append to the log file
// Output:		the message will be sent to the logger daemon, or, error
//				error will be returned
// Author:		Greg Shaw
// Created:		7/11/93

int errlog::ap_log(char *msg)
{
	char ttystr[50];
	char *u;
	char sendstr[255];
	char hostname[50];

	if (ipcobj.open_sock(loghost(),ERRLOG_PORT) == 0) // open socket successful? 
	{
		if (isatty(fileno(stdout)))	// check for valid tty
		{
			strcpy(ttystr,ttyname(fileno(stdout))); // get ttyname
			if (u = strchr(ttystr,'y'), u != NULL)
			{                                               // chop off /dev/tty
				strcpy(sendstr,++u);     // chop after y
				strcpy(ttystr,sendstr);  // copy back
			}
#ifdef MULTIPLE_MACHINE_BBS
			gethostname(hostname,49);
			sprintf(sendstr,"(%s %s) %s",hostname,ttystr,msg);
#else
			sprintf(sendstr,"(%s) %s",ttystr,msg);
#endif
		}
		else
			strcpy(sendstr,msg);
		if (ipcobj.send(sendstr) == 0)		// send message to error logger			
		{
			ipcobj.close_sock(0);
			return(0);
		}
		else
			ipcobj.close_sock(0);		// close socket
	}
	return(-1);
};

// Method:		er_log
// Purpose:		send an error message to the error logger daemon then exit
// Input:		msg - the message to append to the log file
// Output:		the message will be sent to the logger daemon and, the program
//				will abort
// Author:		Greg Shaw
// Created:		7/11/93

int errlog::er_log(char *msg)
{
	char ttystr[50];
	char *u;
	char sendstr[255];

	if (ipcobj.open_sock(loghost(),ERRLOG_PORT) == 0) // open socket successful? 
	{
		if (isatty(fileno(stdout)))	// check for valid tty
		{
			strcpy(ttystr,ttyname(fileno(stdout))); // get ttyname
			if (u = strchr(ttystr,'y'), u != NULL)
			{                                               // chop off /dev/tty
				strcpy(sendstr,++u);     // chop after y
				strcpy(ttystr,sendstr);  // copy back
			}
			sprintf(sendstr,"(%s) %s",ttystr,msg);
		}
		else
			strcpy(sendstr,msg);
		if (ipcobj.send(sendstr) == 0)		// send message to error logger			
			ipcobj.close_sock(0);
	}
	exit(0);	// abort program
};

#endif // _ERRLOG_C_
