3 * $Id: err.c,v 1.2 2002/02/02 19:16:46 mdw Exp $
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Jog: Programming for a jogging machine.
14 * Jog is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Jog is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Jog; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/quis.h>
48 /*----- Static variables --------------------------------------------------*/
50 static FILE *logfp = 0;
51 static unsigned flags = 0;
55 /*----- Main code ---------------------------------------------------------*/
57 /* --- @err_abort@ --- *
59 * Arguments: @int reason@ = abort reason code
60 * @unsigned long err@ = abort error code
61 * @const char *msg@ = error message
65 * Use: Reports a fatal error.
68 void err_abortv(int reason, unsigned long err, const char *msg, va_list *ap)
70 fprintf(stderr, "%s: fatal error (code %d-%lu): ", QUIS, reason, err);
71 vfprintf(stderr, msg, *ap);
73 auerr_abort(reason, err);
77 void err_abort(int reason, unsigned long err, const char *msg, ...)
82 err_abortv(reason, err, msg, &ap);
86 /* --- @err_init@ --- *
92 * Use: Attempts to initialize the logging system. It is a
93 * catastrophic failure if logging can't start up.
96 static void err_exc(exc_extype ex, exc_exval v)
100 err_report(ERR_EXC, 0, ex, "out of memory");
103 err_report(ERR_EXC, 0, ex, "uncaught mLib exception");
113 lf = getenv("JOG_LOGFILE");
116 else if ((logfp = fopen(lf, "w")) == 0) {
117 err_abort(ERRABORT_LOGOPEN, errno,
118 "couldn't open logfile `%s': %s", lf, strerror(errno));
120 exc_uncaught(err_exc);
123 /* --- @err_report@ --- *
125 * Arguments: @int ctx@ = context code
126 * @int reason@ = reason code
127 * @unsigned long err@ = system error code
128 * @const char *msg@ = textual message to log
132 * Use: Reports an error. Doesn't abort anything unless something
133 * really serious happens.
136 void err_reportv(int ctx, int reason, unsigned long err,
137 const char *msg, va_list *ap)
143 if (ctx && !(flags & f_thread)) {
146 auerr(ctx, reason, err);
151 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
152 if (fputs(buf, logfp) == EOF ||
153 fprintf(stderr, " %s: ", QUIS) == EOF ||
154 vfprintf(logfp, msg, *ap) == EOF ||
155 (ctx && (fprintf(logfp, " (error %d", ctx) == EOF ||
156 (reason && fprintf(logfp, "-%d", reason) == EOF) ||
157 (err && fprintf(logfp, ":%lu", err) == EOF) ||
158 putc(')', logfp) == EOF)) ||
159 putc('\n', logfp) == EOF ||
160 fflush(logfp) == EOF) {
161 err_abort(ERRABORT_LOGWRITE, errno,
162 "error writing logfile: %s", strerror(errno));
166 void err_report(int ctx, int reason, unsigned long err, const char *msg, ...)
171 err_reportv(ctx, reason, err, msg, &ap);
175 /* --- @err_log@ --- *
177 * Arguments: @const char *msg@ = textual message to log
181 * Use: Logs a message.
184 void err_logv(const char *msg, va_list *ap)
186 err_reportv(0, 0, 0, msg, ap);
189 void err_log(const char *msg, ...)
194 err_reportv(0, 0, 0, msg, &ap);
198 /*----- That's all, folks -------------------------------------------------*/