3 * $Id: err.c,v 1.1 2002/01/25 19:34:45 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.1 2002/01/25 19:34:45 mdw
37 /*----- Header files ------------------------------------------------------*/
51 #include <mLib/quis.h>
56 /*----- Static variables --------------------------------------------------*/
58 static FILE *logfp = 0;
59 static unsigned flags = 0;
63 /*----- Main code ---------------------------------------------------------*/
65 /* --- @err_abort@ --- *
67 * Arguments: @int reason@ = abort reason code
68 * @unsigned long err@ = abort error code
69 * @const char *msg@ = error message
73 * Use: Reports a fatal error.
76 void err_abortv(int reason, unsigned long err, const char *msg, va_list *ap)
78 fprintf(stderr, "%s: fatal error (code %d-%lu): ", QUIS, reason, err);
79 vfprintf(stderr, msg, *ap);
81 /* au_abort(reason, err); */
85 void err_abort(int reason, unsigned long err, const char *msg, ...)
90 err_abortv(reason, err, msg, &ap);
94 /* --- @err_init@ --- *
100 * Use: Attempts to initialize the logging system. It is a
101 * catastrophic failure if logging can't start up.
104 static void err_exc(exc_extype ex, exc_exval v)
108 err_report(ERR_EXC, 0, ex, "out of memory");
111 err_report(ERR_EXC, 0, ex, "uncaught mLib exception");
121 lf = getenv("JOG_LOGFILE");
124 else if ((logfp = fopen(lf, "w")) == 0) {
125 err_abort(ERRABORT_LOGOPEN, errno,
126 "couldn't open logfile `%s': %s", lf, strerror(errno));
128 exc_uncaught(err_exc);
131 /* --- @err_report@ --- *
133 * Arguments: @int ctx@ = context code
134 * @int reason@ = reason code
135 * @unsigned long err@ = system error code
136 * @const char *msg@ = textual message to log
140 * Use: Reports an error. Doesn't abort anything unless something
141 * really serious happens.
144 void err_reportv(int ctx, int reason, unsigned long err,
145 const char *msg, va_list *ap)
151 if (ctx && !(flags & f_thread)) {
154 /* au_misc(AU_ERROR, ctx, reason, err); */
159 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
160 if (fputs(buf, logfp) == EOF ||
161 fprintf(stderr, " %s: ", QUIS) == EOF ||
162 vfprintf(logfp, msg, *ap) == EOF ||
163 (ctx && (fprintf(logfp, " (error %d", ctx) == EOF ||
164 (reason && fprintf(logfp, "-%d", reason) == EOF) ||
165 (err && fprintf(logfp, ":%lu", err) == EOF) ||
166 putc(')', logfp) == EOF)) ||
167 putc('\n', logfp) == EOF ||
168 fflush(logfp) == EOF) {
169 err_abort(ERRABORT_LOGWRITE, errno,
170 "error writing logfile: %s", strerror(errno));
174 void err_report(int ctx, int reason, unsigned long err, const char *msg, ...)
179 err_reportv(ctx, reason, err, msg, &ap);
183 /* --- @err_log@ --- *
185 * Arguments: @const char *msg@ = textual message to log
189 * Use: Logs a message.
192 void err_logv(const char *msg, va_list *ap)
194 err_reportv(0, 0, 0, msg, ap);
197 void err_log(const char *msg, ...)
202 err_reportv(0, 0, 0, msg, &ap);
206 /*----- That's all, folks -------------------------------------------------*/