head	1.3;
access;
symbols
	OCT93:1.3;
locks;
comment	@ * @;


1.3
date	93.06.11.23.22.07;	author Rhialto;	state Exp;
branches;
next	1.2;

1.2
date	93.06.11.16.15.25;	author Rhialto;	state Exp;
branches;
next	1.1;

1.1
date	93.06.11.15.19.27;	author Rhialto;	state Exp;
branches;
next	;


desc
@Log debugging info, diagnostics, errors etc.
@


1.3
log
@Print control characters correctly.
@
text
@/* $Id: log.c,v 1.2 1993/06/11 16:15:25 Rhialto Exp $
 * $Log: log.c,v $
 * Revision 1.2  1993/06/11  16:15:25  Rhialto
 * First real RCS checkin
 *
 */
/*
 * This file is part of El Cheapo Fax. All modifications relative to the
 * base source are (C) Copyright 1993 by Olaf 'Rhialto' Seibert.
 * All rights reserved. The GNU General Public License applies.
 */
/*
  This file is part of the NetFax system.

  (c) Copyright 1989 by David M. Siegel and Sundar Narasimhan.
      All rights reserved.

    This program 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.

    This program 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; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include "c2proto.h"
#include "log.h"

static void fix_format(char *format_in, char *format_out, int len, int errno_save);

extern int errno;
static int log_level = LOG_DEBUG;
static FILE *logfile;

/*
 * We break this routine out from below to avoid performing
 * this scan if we're not using the format string.  That is,
 * the loglevel is too low to force the printout.
 */
static void fix_format(format_in, format_out, len, errno_save)
     char *format_in;
     char *format_out;
     int len;
     int errno_save;
{
    char *f = format_in;
    char *b = format_out;
    char *b_end = &format_out[len];
    char c;

    while ((c = *f++) != '\0' && b < b_end) {
	switch (c) {
	  case '%':
	    c = *f++;
	    switch (c) {
	      case 'm':
		sprintf(b, "error %d", errno_save);
		b += strlen(b);
		break;
	      default:
		*b++ = '%';
		*b++ = c;
		break;
	    }
	    break;
	  default:
	    *b++ = c;
	    break;
	}
    }

    *b = '\0';
}

/* VARARGS */
int log(int level, char *file, int line, char *format, ...)
{
    va_list ap;
    static char formatbuf[BUFSIZ];
    static char finalbuf[BUFSIZ];
    static char outbuf[BUFSIZ];
    char *outbuf_ptr = outbuf;
    char *slash, *ptr;
    int errno_save = errno;

    if (level > log_level)
      return (0);

    if ((slash = strrchr(file, '/')) != NULL)
      file = slash+1;

    va_start(ap, format);

    fix_format(format, formatbuf, sizeof(formatbuf), errno_save);
    fprintf(stdout, "(%.30s:%d) ", file, line);
    if (logfile)
	fprintf(logfile, "(%.30s:%d) ", file, line);
    vsprintf(finalbuf, formatbuf, ap);

    for (ptr = finalbuf; *ptr != NULL; ptr++) {
	switch (*ptr) {
	  case '\r':
	    *(outbuf_ptr++) = '\\';
	    *(outbuf_ptr++) = 'r';
	    break;
	  case '\n':
	    *(outbuf_ptr++) = '\\';
	    *(outbuf_ptr++) = 'n';
	    break;
	  case ETX:
	    strcpy(outbuf_ptr, "<ETX>");
	    outbuf_ptr += 5;
	    break;
	  case DLE:
	    strcpy(outbuf_ptr, "<DLE>");
	    outbuf_ptr += 5;
	    break;
	  case XON:
	    strcpy(outbuf_ptr, "<XON>");
	    outbuf_ptr += 5;
	    break;
	  case XOFF:
	    strcpy(outbuf_ptr, "<XOF>");
	    outbuf_ptr += 5;
	    break;
	  case CAN:
	    strcpy(outbuf_ptr, "<CAN>");
	    outbuf_ptr += 5;
	    break;
	  default:
	    if (*ptr < 32) {
		sprintf(outbuf_ptr, "<^%c>", *ptr + 64);
		outbuf_ptr += 4;
	    } else
		*(outbuf_ptr++) = *ptr;
	    break;
	}
    }
    *(outbuf_ptr++) = '\n';
    *(outbuf_ptr++) = '\0';

    fputs(outbuf, stdout);
    if (logfile)
	fputs(outbuf, logfile);

    va_end(ap);

    return (0);
}

void log_set_level(level)
     int level;
{
    if (level >= 10) {
	if (logfile == NULL)
	    logfile = fopen("faxlog", "a");
	level -= 10;
    }
    log_level = level;
}
@


1.2
log
@First real RCS checkin
@
text
@d1 5
a5 2
/* $Id$
 * $Log$
d142 1
a142 1
		sprintf(outbuf_ptr, "<^%c>", *ptr + 32);
@


1.1
log
@Initial revision
@
text
@d1 3
d40 1
a101 1
    /*fprintf(stderr, "(%.30s:%d) ", file, line);*/
d103 2
d138 5
a142 1
	    *(outbuf_ptr++) = *ptr;
a148 1
    /*fputs(outbuf, stderr);*/
d150 2
d161 5
@
