/* FILE pldebug.c   	Wed Jul  6 16:49:47 PDT 1988	 w.klas       */
/* 	$Header: pldebug.c,v 1.2 89/02/02 16:21:23 aoki Exp $*/

#include "pldebug.h"
#include "xid.h"

static	int DebugFlag = 0;
static	int DebugErrorFlag = 0;
static	int DebugStatisticFlag = 0;
static 	int DebugFileFlag = 0;
static  int DebugStderrFlag = 0;

/***********************************************************************/
/* Note(msg, mode)              - print a message msg                  */
/***********************************************************************/

void
Note(msg, mode, xid)
char   	    	*msg;           /* the message string */
int    	    	mode;
TransactionId	xid;
{
    static  FILE    *file = NULL;
    int	    	    pid;
    char    	    *filename;
    
    pid = getpid();
    filename = SYNCH_LOG_FILE;
    if (DebugFileIsOn()) {
       if (file == NULL) {
       	   file = fopen(filename, "a");
    	   if (file == NULL) {
    	       fprintf(stderr, "fopen failed\n");
    	       exit(2);
    	   }
       }
       if (flock(fileno(file), LOCK_EX) == -1) {
    	   fprintf(stderr, "Note: setting a LOCK_EX failed\n");
    	   exit(2);
       }
       if (fseek(file, 01, 2) == -1) {
    	   fprintf(stderr, "Note: fseek failed\n");
    	   exit(2);
       }
    }
    switch (mode)   
    { case ERROR :  fprintf(stderr,"ERROR> %s\n", msg);
    	    	    if (DebugFileIsOn())
    	    	    	fprintf(file, "%d:ERROR> %s\n",pid, msg);
       	    	    break;
      case LOCK :   if (DebugIsOn()) {
                        if (DebugStderrIsOn())
      	    	    	    fprintf(stderr, "LOCK> %s - %s\n", msg, 
      	    	    	    	         TransactionIdFormString(xid));
    	    	    	if (DebugFileIsOn())
    	    	    	    fprintf(file, "%d:SHMLOCK> %s - %s\n", pid, msg, 
    	    	    	    	TransactionIdFormString(xid));
    	    	    }
       	    	    break;
      case LDEBUG : if (DebugIsOn()) {
                        if (DebugStderrIsOn())
                       	    fprintf(stderr, "DEBUG> %s\n", msg);
                       	if (DebugFileIsOn())
    	    	    	    fprintf(file, "%d:DEBUG> %s\n", pid, msg);
               	    }
               	    break;
      case DEBERR : if (DebugErrorIsOn()) {
                        if (DebugStderrIsOn())
            	    	    fprintf(stderr, "DEBUGERROR> %s\n", msg);
            	    	if (DebugFileIsOn())
    	    	    	    fprintf(file, "%d:DEBUGERROR> %s\n",pid, msg);
            	    }
            	break;
      case STATISTIC : if (DebugStatisticIsOn()) {
            	           fprintf(stderr,"STATISTIC> %s\n", msg);
            	    }
            	    break;
      default :	return;
    }
    if (DebugFileIsOn()) {
    	fflush(file);
    	if (flock(fileno(file), LOCK_UN) == -1) {
    	    fprintf(stderr, "Note: LOCK_UN failed\n");
    	    exit(2);
    	}
    }
    return;
}


/***********************************************************************/
/* DebugOn()            - turns trace mode on                          */
/***********************************************************************/
void
DebugOn()
{
        DebugFlag = 1;
        return;
}
/***********************************************************************/
/* DebugErrorOn()            - turns trace mode for errors on          */
/***********************************************************************/
void
DebugErrorOn()
{
        DebugErrorFlag = 1;
        return;
}
/***********************************************************************/
/* DebugFileOn()            - turns file trace mode for errors on      */
/***********************************************************************/
void
DebugFileOn()
{
        DebugFileFlag = 1;
        return;
}
/***********************************************************************/
/* DebugStatisticOn()        - turns statistic mode on        	       */
/***********************************************************************/
void
DebugStatisticOn()
{
        DebugStatisticFlag = 1;
        return;
}
/***********************************************************************/
/* DebugStderrOn()            - turns stderr trace mode  on 	       */
/***********************************************************************/
void
DebugStderrOn()
{
        DebugStderrFlag = 1;
        return;
}

/***********************************************************************/
/* DebugOff()           - turns trace mode off                         */
/***********************************************************************/
void
DebugOff()
{
        DebugFlag = 0;
        return;
}
/***********************************************************************/
/* DebugErrorOff()           - turns trace mode for errors off         */
/***********************************************************************/
void
DebugErrorOff()
{
        DebugErrorFlag = 0;
        return;
}
/***********************************************************************/
/* DebugFileOff()            - turns file trace mode for errors off    */
/***********************************************************************/
void
DebugFileOff()
{
        DebugFileFlag = 0;
        return;
}
/***********************************************************************/
/* DebugStderrOff()            - turns stderr trace mode  off 	       */
/***********************************************************************/
void
DebugStderrOff()
{
        DebugStderrFlag = 0;
        return;
}


/***********************************************************************/
/* DebugStatisticOff()        - turns statistic mode off       	       */
/***********************************************************************/
void
DebugStatisticOff()
{
        DebugStatisticFlag = 0;
        return;
}


/***********************************************************************/
/* DebugIsOn()          - returns 1, iff trace mode is on              */
/***********************************************************************/
int
DebugIsOn()
{       return(DebugFlag);
}

/***********************************************************************/
/* DebugErrorIsOn()      - returns 1, iff trace mode for errors is on  */
/***********************************************************************/
int
DebugErrorIsOn()
{       return(DebugErrorFlag);
}
/***********************************************************************/
/* DebugFileIsOn()      - returns 1, iff file trace mode  is on        */
/***********************************************************************/
int
DebugFileIsOn()
{       return(DebugFileFlag);
}
/***********************************************************************/
/* DebugStderrIsOn()           - returns 1, iff stderr trace mode is on*/
/***********************************************************************/
int
DebugStderrIsOn()
{
        return(DebugStderrFlag);
}

/***********************************************************************/
/* DebugStatisticIsOn()      - returns 1, iff statistic mode is on     */
/***********************************************************************/
int
DebugStatisticIsOn()
{       return(DebugStatisticFlag);
}


/***********************************************************************/
/* LockLogFileInit()      - creates a new trace file  	    	       */
/***********************************************************************/
void
LockLogFileInit()
{    
    char    	    *filename1, *filename2;
    int	    	    pid;
    FILE    	    *file;

    
    filename1 = SYNCH_LOG_FILE;
    filename2 = LOCK_LOG_FILE;
    if (DebugFileIsOn()) {
      	file = fopen(filename1, "w");
    	if (file == NULL) {
    	    fprintf(stderr, "fopen failed\n");
    	    exit(2);
       	}
       	if (flock(fileno(file), LOCK_EX) == -1) {
    	   fprintf(stderr, "Note: setting a LOCK_EX failed\n");
    	   exit(2);
       	}
       	if (fseek(file, 01, 2) == -1) {
    	   fprintf(stderr, "Note: fseek failed\n");
    	   exit(2);
       	}
    	pid = getpid();
    	fprintf(file, "%d:  Starting new session\n",pid );
    	fflush(file);
    	if (flock(fileno(file), LOCK_UN) == -1) {
    	    fprintf(stderr, "Note: LOCK_UN failed\n");
    	    exit(2);
    	}
    
      	file = fopen(filename2, "w");
    	if (file == NULL) {
    	    fprintf(stderr, "fopen failed\n");
    	    exit(2);
       	}
       	if (flock(fileno(file), LOCK_EX) == -1) {
    	   fprintf(stderr, "Note: setting a LOCK_EX failed\n");
    	   exit(2);
       	}
       	if (fseek(file, 01, 2) == -1) {
    	   fprintf(stderr, "Note: fseek failed\n");
    	   exit(2);
       	}
    	pid = getpid();
    	fprintf(file, "%d:  Starting new session\n",pid );
    	fprintf(file, "PID :  table type transaction         DB-ID:relation/page/line    locktype   mode   status\n");
/*    	fprintf(file, "PID :  table type transaction             relation/page/line    locktype   mode   status\n");
*/
    	fprintf(file, "=============================================================================================\n");
/*    	fprintf(file, "===================================================================================\n");
*/
    	fflush(file);
    	if (flock(fileno(file), LOCK_UN) == -1) {
    	    fprintf(stderr, "Note: LOCK_UN failed\n");
    	    exit(2);
    	}
    }
}






