wrlib: changed the function used to detecting MagickWand
[wmaker-crm.git] / WINGs / error.c
blobd61621fb48804909e1eaf58597de80c95577ab43
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>
33 #ifdef HAVE_SYSLOG_H
34 #include <syslog.h>
36 static Bool syslog_initialized = False;
39 static void syslog_open(const char *prog_name)
41 int options;
43 if (!prog_name)
44 prog_name = "WINGs";
46 options = LOG_PID;
47 openlog(prog_name, options, LOG_DAEMON);
48 syslog_initialized = True;
51 static void syslog_message(int prio, const char *prog_name, const char *msg)
53 if (!syslog_initialized)
54 syslog_open(prog_name);
56 //jump over the program name cause syslog is already displaying it
57 syslog(prio, "%s", msg + strlen(prog_name));
60 void w_syslog_close(void)
62 if (syslog_initialized) {
63 closelog();
64 syslog_initialized = False;
67 #endif
69 void __wmessage(const char *func, const char *file, int line, int type, const char *msg, ...)
71 va_list args;
72 char *buf;
73 static int linemax = 0;
74 int truncated = 0;
75 #ifdef HAVE_SYSLOG
76 int syslog_priority = LOG_INFO;
77 const char *syslog_prefix = "INFO";
78 #endif
80 if (linemax == 0) {
81 #ifdef HAVE_SYSCONF
82 linemax = sysconf(_SC_LINE_MAX);
83 if (linemax == -1) {
84 /* I'd like to know of this ever fires */
85 fprintf(stderr, "%s %d: sysconf(_SC_LINE_MAX) returned error\n",
86 __FILE__, __LINE__);
87 linemax = 512;
89 #else /* !HAVE_SYSCONF */
90 fprintf(stderr, "%s %d: Your system does not have sysconf(3); "
91 "let wmaker-dev@windowmaker.org know.\n", __FILE__, __LINE__);
92 linemax = 512;
93 #endif /* HAVE_SYSCONF */
96 buf = wmalloc(linemax);
98 fflush(stdout);
100 /* message format: <wings_progname>(function(file:line): <type?>: <message>"\n" */
101 strncat(buf, _WINGS_progname ? _WINGS_progname : "WINGs", linemax - 1);
102 snprintf(buf + strlen(buf), linemax - strlen(buf), "(%s(%s:%d))", func, file, line);
103 strncat(buf, ": ", linemax - 1 - strlen(buf));
105 switch (type) {
106 case WMESSAGE_TYPE_FATAL:
107 strncat(buf, _("fatal: "), linemax - 1 - strlen(buf));
108 #ifdef HAVE_SYSLOG
109 syslog_priority = LOG_CRIT;
110 syslog_prefix = "FATAL";
111 #endif
112 break;
113 case WMESSAGE_TYPE_ERROR:
114 strncat(buf, _("error: "), linemax - 1 - strlen(buf));
115 #ifdef HAVE_SYSLOG
116 syslog_priority = LOG_ERR;
117 syslog_prefix = "ERROR";
118 #endif
119 break;
120 case WMESSAGE_TYPE_WARNING:
121 strncat(buf, _("warning: "), linemax - 1 - strlen(buf));
122 #ifdef HAVE_SYSLOG
123 syslog_priority = LOG_WARNING;
124 syslog_prefix = "WARNING";
125 #endif
126 break;
127 case WMESSAGE_TYPE_MESSAGE:
128 /* FALLTHROUGH */
129 default: /* should not happen, but doesn't hurt either */
130 break;
133 va_start(args, msg);
134 if (vsnprintf(buf + strlen(buf), linemax - strlen(buf), msg, args) >= linemax - strlen(buf))
135 truncated = 1;
137 va_end(args);
139 fputs(buf, stderr);
140 #ifdef HAVE_SYSLOG
141 syslog_message(syslog_priority, _WINGS_progname ? _WINGS_progname : "WINGs", buf);
142 #endif
144 if (truncated)
145 fputs("*** message truncated ***", stderr);
147 fputs("\n", stderr);
149 wfree(buf);