/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

#ifdef LEVEL6
static char sccsid_STDlogging6[] = "@(#)STDlogging.c	1.14 11/13/92 10:51:59  LLNL ";

#   define FILENAME log6filename
#   define FILEVAR log6file
#   define FLAGVAR log6on
#   define STARTROUTINE __ROr_log6start
#   define ENDROUTINE __ROr_log6end
#   define STARTROUTINEI __ROr_log6starti
#   define ENDROUTINEI __ROr_log6endi
#   define LOGLEVEL 6
#   define STARTLOG Start_log6
#   define STARTLOGI Start_log6i
#   define STOPLOG Stop_log6
#   define STOPLOGI Stop_log6i

#else

#ifdef LEVEL7
static char sccsid_STDlogging7[] = "@(#)STDlogging.c	1.14 11/13/92 10:51:59  LLNL ";

#   define FILENAME log7filename
#   define FILEVAR log7file
#   define FLAGVAR log7on
#   define STARTROUTINE __ROr_log7start
#   define ENDROUTINE __ROr_log7end
#   define STARTROUTINEI __ROr_log7starti
#   define ENDROUTINEI __ROr_log7endi
#   define LOGLEVEL 7
#   define STARTLOG Start_log7
#   define STARTLOGI Start_log7i
#   define STOPLOG Stop_log7
#   define STOPLOGI Stop_log7i
#else
   ***Error
#endif
#endif

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	11/30/90

PURPOSE:
	These are the routines, et. al. which implement the start and stop
	of logging


*/

#include <elros/instance.h>
#include <malloc.h>
#include <string.h>
extern char *getenv();

extern __RO_Debugstate __ROv_global_debug_state;

int STARTLOG(filename)
  char *filename;
{ 
  return STARTROUTINE(filename);
}

int STARTLOGI(instance,filename)
  __RO_Instance_Record *instance;
  char *filename;
{
  return STARTROUTINEI(instance,filename);
}

int STOPLOG()
{ 
  return ENDROUTINE();
}

int STOPLOGI(instance)
  __RO_Instance_Record *instance;
{
  return ENDROUTINEI(instance);
}


int STARTROUTINE(filename)

	char *filename;

{
   char *filemode = "w";
   char default_name[32];

   if (__ROv_global_debug_state.FILEVAR)
      if (fclose(__ROv_global_debug_state.FILEVAR))
         return(__ROx_LOG_FCLOSE);
   if (filename)
   {
      if (__ROv_global_debug_state.FILENAME) 
        free(__ROv_global_debug_state.FILENAME);
      strcpy(__ROv_global_debug_state.FILENAME=malloc(strlen(filename)+1),filename);
   }
   else if (__ROv_global_debug_state.FILENAME)
      filemode = "a";
   else 
   {
      sprintf(default_name,"ELROS.log%d",LOGLEVEL);
      __ROv_global_debug_state.FILENAME = 
		strcpy(malloc(strlen(default_name)+1),default_name);
   }
   if (!(__ROv_global_debug_state.FILEVAR =
	fopen(__ROv_global_debug_state.FILENAME,filemode)))
      return(__ROx_LOG_FOPEN);
   __ROv_global_debug_state.FLAGVAR = 1;

   return(SUCCESS);
}

int STARTROUTINEI(instance,filename)

	__RO_Instance_Record *instance;
	char *filename;

{
   if (!__ROv_global_debug_state.FILEVAR)
   {
      STARTROUTINE(filename);
      __ROv_global_debug_state.FLAGVAR = 0;
   }
   instance->FLAGVAR = 1;
   return(SUCCESS);
}

int ENDROUTINE()

{
   __ROv_global_debug_state.FLAGVAR = 0;
   return(SUCCESS);
}

int ENDROUTINEI(instance)

   __RO_Instance_Record *instance;

{
   instance->FLAGVAR = 0;
   return(SUCCESS);
}


