1
5
void log_quit(char *logfile, const char *fmt,... )
{
va_list ap;
va_start(ap, fmt);
err_prn(fmt, ap,logfile);
va_end(ap);
exit(1);
}
extern void err_prn( const char *fmt, va_list ap, char *logfile)
{
int save_err;
char buf[MAXLINELEN];
FILE *plf;
save_err = errno;
vsprintf(buf,fmt, ap);
sprintf( buf+strlen(buf), ": %s", strerror(save_err));
strcat(buf, "\n");
fflush(stdout);
if(logfile !=NULL){
if((plf=fopen(logfile, "a") ) != NULL){
fputs(buf, plf);
fclose(plf);
}else
fputs("failed to open log file \n", stderr);
}else fputs(buf, stderr);
fflush(NULL);
return;
}
Mã nguồn file liberr.c
Để tạo một thư viện tĩnh, bước đầu tiên là dịch đoạn mã của form đối tượng:
$gcc –H –c liberr.c –o liberr.o
tiếp theo:
$ar rcs liberr.a liberr.o
/*
* errtest.c
*/
#include <stdio.h>
#include <stdlib.h>
#include "liberr.h"
#define ERR_QUIT_SKIP 1
#define LOG_QUIT_SKIP 1
int main(void)
{