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 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 2002/02/02 19:16:46 mdw
33 * New audio subsystem.
35 * Revision 1.1 2002/01/25 19:34:45 mdw
40 /*----- Header files ------------------------------------------------------*/
54 #include <mLib/quis.h>
59 /*----- Static variables --------------------------------------------------*/
61 static FILE *logfp = 0;
62 static unsigned flags = 0;
66 /*----- Main code ---------------------------------------------------------*/
68 /* --- @err_abort@ --- *
70 * Arguments: @int reason@ = abort reason code
71 * @unsigned long err@ = abort error code
72 * @const char *msg@ = error message
76 * Use: Reports a fatal error.
79 void err_abortv(int reason, unsigned long err, const char *msg, va_list *ap)
81 fprintf(stderr, "%s: fatal error (code %d-%lu): ", QUIS, reason, err);
82 vfprintf(stderr, msg, *ap);
84 auerr_abort(reason, err);
88 void err_abort(int reason, unsigned long err, const char *msg, ...)
93 err_abortv(reason, err, msg, &ap);
97 /* --- @err_init@ --- *
103 * Use: Attempts to initialize the logging system. It is a
104 * catastrophic failure if logging can't start up.
107 static void err_exc(exc_extype ex, exc_exval v)
111 err_report(ERR_EXC, 0, ex, "out of memory");
114 err_report(ERR_EXC, 0, ex, "uncaught mLib exception");
124 lf = getenv("JOG_LOGFILE");
127 else if ((logfp = fopen(lf, "w")) == 0) {
128 err_abort(ERRABORT_LOGOPEN, errno,
129 "couldn't open logfile `%s': %s", lf, strerror(errno));
131 exc_uncaught(err_exc);
134 /* --- @err_report@ --- *
136 * Arguments: @int ctx@ = context code
137 * @int reason@ = reason code
138 * @unsigned long err@ = system error code
139 * @const char *msg@ = textual message to log
143 * Use: Reports an error. Doesn't abort anything unless something
144 * really serious happens.
147 void err_reportv(int ctx, int reason, unsigned long err,
148 const char *msg, va_list *ap)
154 if (ctx && !(flags & f_thread)) {
157 auerr(ctx, reason, err);
162 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
163 if (fputs(buf, logfp) == EOF ||
164 fprintf(stderr, " %s: ", QUIS) == EOF ||
165 vfprintf(logfp, msg, *ap) == EOF ||
166 (ctx && (fprintf(logfp, " (error %d", ctx) == EOF ||
167 (reason && fprintf(logfp, "-%d", reason) == EOF) ||
168 (err && fprintf(logfp, ":%lu", err) == EOF) ||
169 putc(')', logfp) == EOF)) ||
170 putc('\n', logfp) == EOF ||
171 fflush(logfp) == EOF) {
172 err_abort(ERRABORT_LOGWRITE, errno,
173 "error writing logfile: %s", strerror(errno));
177 void err_report(int ctx, int reason, unsigned long err, const char *msg, ...)
182 err_reportv(ctx, reason, err, msg, &ap);
186 /* --- @err_log@ --- *
188 * Arguments: @const char *msg@ = textual message to log
192 * Use: Logs a message.
195 void err_logv(const char *msg, va_list *ap)
197 err_reportv(0, 0, 0, msg, ap);
200 void err_log(const char *msg, ...)
205 err_reportv(0, 0, 0, msg, &ap);
209 /*----- That's all, folks -------------------------------------------------*/