home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC Press 1997 July
/
Sezamfile97_1.iso
/
msdos
/
c
/
cbase11.a03
/
CBASE11.ZIP
/
XTEND
/
ERRLOG.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-01
|
2KB
|
109 lines
/*
* Copyright (c) 1989-1992 Citadel Software, Inc.
* All Rights Reserved
*/
/* #ident "@(#)errlog.c 1.7 - 93/01/01" */
#include <port.h>
/* standard headers */
#include <errno.h>
#ifdef AC_STDARG
#include <stdarg.h>
#endif
#include <stdio.h>
#ifdef AC_STRING
#include <string.h>
#endif
#include <time.h>
/* local headers */
#include "xtend.h"
/* private global data */
static char logfile[PATH_MAX + 1] = "errlog.txt";
/*man---------------------------------------------------------------------------
NAME
errlog - error log
SYNOPSIS
void errlog(file, line, msg, ...)
const char *file;
int line;
const char *msg;
DESCRIPTION
msg = NULL to set logfilename to file.
date time file[line]: msg error n: error string.
92-10-01 14:01:38 example.c[57]: error 35: File already exists.
default filename is errlog.txt.
EXAMPLES
errlog("errlog.txt", 0, NULL);
errlog(__FILE__, __LINE__, "Error writing to file");
------------------------------------------------------------------------------*/
void errlog(const char *file, int line, const char *msg, ...)
{
va_list ap = NULL; /* argument list pointer */
FILE * fp = NULL; /* log file stream */
int terrno = errno; /* tmp errno */
struct tm * tp = NULL; /* time struct pointer */
time_t utc = 0; /* universal coord. time */
/* set log file name */
if (msg == NULL) {
strncpy(logfile, file, sizeof(logfile));
logfile[sizeof(logfile) - 1] = NUL;
errno = terrno;
return;
}
/* open log file */
logfile[sizeof(logfile) - 1] = NUL;
fp = fopen(logfile, "a");
if (fp == NULL) {
errno = terrno;
return;
}
/* write message */
utc = time(NULL);
tp = localtime(&utc);
if (fprintf(fp, "%.2d-%.2d-%.2d", tp->tm_year, tp->tm_mon + 1, tp->tm_mday) < 0) {
errno = terrno;
return;
}
if (fprintf(fp, "% .2d:%.2d:%.2d", tp->tm_hour, tp->tm_min, tp->tm_sec) < 0) {
errno = terrno;
return;
}
if (fprintf(fp, " %.*s[%d]: ", FILENAME_MAX, file, line) < 0) {
errno = terrno;
return;
}
va_start(ap, msg);
vfprintf(fp, msg, ap);
va_end(ap);
fprintf(fp, " error %d: %s.", terrno, xstrerror(terrno));
/* newline */
fputc('\n', fp);
/* close log file */
if (fclose(fp) == EOF) {
errno = terrno;
return;
}
/* restore errno value */
errno = terrno;
return;
}