WINGs: Added 'const' attribute to function 'WMCreateHashTable'
[wmaker-crm.git] / WINGs / error.c
blob2d5a5881b0612ab93d64cef182747eee1fa190ca
1 /*
2 * Window Maker miscelaneous function library
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
22 #include "wconfig.h"
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include <WUtil.h>
31 #include <WINGsP.h>
34 void __wmessage(const char *func, const char *file, int line, int type, const char *msg, ...)
36 va_list args;
37 char *buf;
38 static int linemax = 0;
39 int truncated = 0;
41 if (linemax == 0) {
42 #ifdef HAVE_SYSCONF
43 linemax = sysconf(_SC_LINE_MAX);
44 if (linemax == -1) {
45 /* I'd like to know of this ever fires */
46 fprintf(stderr, "%s %d: sysconf(_SC_LINE_MAX) returned error\n",
47 __FILE__, __LINE__);
48 linemax = 512;
50 #else /* !HAVE_SYSCONF */
51 fprintf(stderr, "%s %d: Your system does not have sysconf(3); "
52 "let wmaker-dev@windowmaker.org know.\n", __FILE__, __LINE__);
53 linemax = 512;
54 #endif /* HAVE_SYSCONF */
57 buf = wmalloc(linemax);
59 fflush(stdout);
61 /* message format: <wings_progname>(function(file:line): <type?>: <message>"\n" */
62 strncat(buf, _WINGS_progname ? _WINGS_progname : "WINGs", linemax - 1);
63 snprintf(buf + strlen(buf), linemax - strlen(buf), "(%s(%s:%d))", func, file, line);
64 strncat(buf, ": ", linemax - 1 - strlen(buf));
66 switch (type) {
67 case WMESSAGE_TYPE_FATAL:
68 strncat(buf, _("fatal error: "), linemax - 1 - strlen(buf));
69 break;
70 case WMESSAGE_TYPE_ERROR:
71 strncat(buf, _("error: "), linemax - 1 - strlen(buf));
72 break;
73 case WMESSAGE_TYPE_WARNING:
74 strncat(buf, _("warning: "), linemax - 1 - strlen(buf));
75 break;
76 case WMESSAGE_TYPE_MESSAGE:
77 /* FALLTHROUGH */
78 default: /* should not happen, but doesn't hurt either */
79 break;
82 va_start(args, msg);
83 if (vsnprintf(buf + strlen(buf), linemax - strlen(buf), msg, args) >= linemax - strlen(buf))
84 truncated = 1;
86 va_end(args);
88 fputs(buf, stderr);
90 if (truncated)
91 fputs("*** message truncated ***", stderr);
93 fputs("\n", stderr);
95 wfree(buf);