ide_errlog.c 2.71 KB
/*
 * $Revision: 1.2 $
 */

/* 
 * ide_errlog.c:
 *
 *   Diagnostic error reporting interface function for Host based diagnostics.
 *
 * FUNCTIONS DEFINED:
 *
 *	errlog		... Selectively prints diagnostic error messages.
 *    	
 * Silicon Graphics, Inc. (C) copyright 1989  (all rights reserved).
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <varargs.h>
#include <sys/types.h>
#include <string.h>
#include "diag.h"

extern FILE *logfile;

/*
 * errlog(): Error message filter function.  Uses the varargs libc feature so
 *	     that we can pass this function a variable number of arguments.
 */
/*VARARGS*/
void
errlog(va_alist)
va_dcl
{
    register int errdesc, errstart, errend, errlevel;
    register char *fmt;
    va_list args;
    time_t clock;
    char buf[20];
    int tm;
    char errmsg[256], tmp[256];
    static last_errend = 1;

    va_start(args);
    /*
     * The first arg is the error level for this message.  
     * The second arg is the standard printf format string.
     * Any subsequent args must be supplied to satisfy the format requirements.
     */
    errdesc = va_arg(args, int);
    errlevel = errdesc & 0xf;
    errend = !(errdesc & 0x10);
    errstart = !(errdesc & 0x20);

    /*
     * Update the communication structure fields which tell the IDE server
     * which test levels generated errors.  Only do this if the current
     * error level is actually an error message.
     */
    if ((errlevel == ERR_SEVERE) || (errlevel == ERR_SIMPLE)) {
	if (pGlobalComm->errNums[errIndex] < ideSubTestNum) {
	    errIndex++;
	    pGlobalComm->errNums[errIndex] = ideSubTestNum;
	}
    }
    fmt = va_arg(args, char *);

    errmsg[0] = '\0';
    if (errstart) {
	if (!last_errend) strcat(errmsg, "\n");
	switch (errlevel) {
	  case ERR_SEVERE:
	  case ERR_SIMPLE:
	    sprintf(errmsg, "%sERROR:\t(%d)\t", errmsg, ideSubTestNum);
	    break;

	  case INFO:
	    sprintf(errmsg, "%sINFO:\t", errmsg);
	    break;

	  case DEBUG:
	    sprintf(errmsg, "%sDEBUG:\t", errmsg);
	    break;
	}
    }

    vsprintf(tmp, fmt, args);	    /* print the user's message.	    */
    strcat(errmsg, tmp);
    strcat(errmsg, "\n");

    if (printmask & errlevel) {
	last_errend = errend;
	printf("%s", errmsg);
    }
    fflush(stdout);

    if (logmask & errlevel) {
    	if (errlevel != INFO || getenv("IDE_TIMESTAMP_INFO") && errstart) {
	    /*
	     * time stamp the log entries
	     */
	    clock = time(0);
	    tm = localtime(&clock);
#ifdef __sgi__
	    ascftime(buf, "%D(%T)", tm);
#else
	    strftime(buf, sizeof buf, "%D(%T)", tm);
#endif
	    strcat(errmsg, buf);
	    strcat(errmsg, "\n");
	}
	fprintf(logfile, "%s", errmsg);
    }
    fflush(logfile);

    if (exitmask & errlevel)
	diagexit();
    va_end(args);
}