# HG changeset patch # User tuomov # Date 1076951343 -3600 # Node ID fc585645ad056cc3c9bf692b5c8569aacbbd5090 # Parent a4033700e35c3d872c7479f308190de687ef52d6 trunk: changeset 1316 Oops. Errorlog hadn't been added. diff -r a4033700e35c -r fc585645ad05 errorlog.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/errorlog.c Mon Feb 16 18:09:03 2004 +0100 @@ -0,0 +1,127 @@ +/* + * libtu/errorlog.c + * + * Copyright (c) Tuomo Valkonen 1999-2004. + * + * You may distribute and modify this library under the terms of either + * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. + */ + +#include +#include +#include + +#include "util.h" +#include "types.h" +#include "output.h" +#include "misc.h" +#include "errorlog.h" + +static ErrorLog *current_log=NULL; + +static void add_to_log(ErrorLog *el, const char *message, int l) +{ + if(message==NULL) + return; + + /* Also write to stderr */ + fwrite(message, sizeof(char), l, stderr); + + if(el==NULL) + return; + + if(el->file!=NULL){ + el->errors=TRUE; + fwrite(message, sizeof(char), l, el->file); + return; + } + + if(el->msgs==NULL){ + el->msgs=ALLOC_N(char, ERRORLOG_MAX_SIZE); + if(el->msgs==NULL){ + fprintf(stderr, "%s: %s\n", prog_execname(), strerror(errno)); + return; + } + el->msgs[0]=0; + el->msgs_len=0; + } + + el->errors=TRUE; + + if(l+el->msgs_len>ERRORLOG_MAX_SIZE-1){ + int n=0; + if(lmsgs, el->msgs+el->msgs_len-n, n); + } + memcpy(el->msgs+n, message+l-(ERRORLOG_MAX_SIZE-1-n), + ERRORLOG_MAX_SIZE-1-n); + el->msgs[ERRORLOG_MAX_SIZE]='\0'; + el->msgs_len=ERRORLOG_MAX_SIZE-1; + }else{ + memcpy(el->msgs+el->msgs_len, message, l); + el->msgs[el->msgs_len+l]='\0'; + el->msgs_len+=l; + } +} + + +static void log_warn_handler(const char *message) +{ + const char *p=strchr(message, '\n'); + static int lineno=0; + int alternat=0; + + add_to_log(current_log, lineno==0 ? ">> " : " ", 3); + + if(p!=NULL){ + add_to_log(current_log, message, p-message+1); + lineno++; + log_warn_handler(p+1); + lineno--; + return; + } + + add_to_log(current_log, message, strlen(message)); + add_to_log(current_log, "\n", 1); +} + + +void errorlog_begin_file(ErrorLog *el, FILE *file) +{ + el->msgs=NULL; + el->msgs_len=0; + el->file=file; + el->prev=current_log; + el->errors=FALSE; + el->old_handler=set_warn_handler(log_warn_handler); + current_log=el; +} + + +void errorlog_begin(ErrorLog *el) +{ + errorlog_begin_file(el, NULL); +} + + +bool errorlog_end(ErrorLog *el) +{ + current_log=el->prev; + set_warn_handler(el->old_handler); + el->prev=NULL; + el->old_handler=NULL; + return el->errors; +} + + +void errorlog_deinit(ErrorLog *el) +{ + if(el->msgs!=NULL) + free(el->msgs); + el->msgs=NULL; + el->msgs_len=0; + el->file=NULL; + el->errors=FALSE; +} + diff -r a4033700e35c -r fc585645ad05 errorlog.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/errorlog.h Mon Feb 16 18:09:03 2004 +0100 @@ -0,0 +1,38 @@ +/* + * libtu/errorlog.h + * + * Copyright (c) Tuomo Valkonen 1999-2004. + * + * You may distribute and modify this library under the terms of either + * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. + */ + +#ifndef LIBTU_ERRORLOG_H +#define LIBTU_ERRORLOG_H + +#include + +#include "types.h" +#include "obj.h" +#include "output.h" + +#define ERRORLOG_MAX_SIZE (1024*4) + +INTRSTRUCT(ErrorLog); +DECLSTRUCT(ErrorLog){ + char *msgs; + int msgs_len; + FILE *file; + bool errors; + ErrorLog *prev; + WarnHandler *old_handler; +}; + +/* el is assumed to be uninitialised */ +extern void errorlog_begin(ErrorLog *el); +extern void errorlog_begin_file(ErrorLog *el, FILE *file); +/* For errorlog_end el Must be the one errorlog_begin was last called with */ +extern bool errorlog_end(ErrorLog *el); +extern void errorlog_deinit(ErrorLog *el); + +#endif /* LIBTU_ERRORLOG_H */