2 martin 1.1 /*
3 * Copyright 2001 Red Hat Inc., Durham, North Carolina.
4 *
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation on the rights to use, copy, modify, merge,
11 * publish, distribute, sublicense, and/or sell copies of the Software,
12 * and to permit persons to whom the Software is furnished to do so,
13 * subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
23 martin 1.1 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 /*
30 * Authors:
31 * Rickard E. (Rik) Faith <faith@redhat.com>
32 *
33 */
34
35 /** \file
36 * This file encapsulated all of the logging functions that are used by
37 * DMX for informational, warning, and error messages. */
38
39 #include "dmx.h"
40 #include "dmxlog.h"
41 #include "dmxinput.h"
42 #ifdef XINPUT
|
45 martin 1.1 #endif
46
47 static dmxLogLevel dmxCurrentLogLevel = dmxDebug;
48
49 /** Set the default level for logging to #dmxLogLevel. Returns the
50 * previous log level. */
51 dmxLogLevel dmxSetLogLevel(dmxLogLevel newLevel)
52 {
53 dmxLogLevel oldLevel = dmxCurrentLogLevel;
54 if (newLevel > dmxFatal) newLevel = dmxFatal;
55 dmxCurrentLogLevel = newLevel;
56 return oldLevel;
57 }
58
59 /** Returns the log level set by #dmxLogLevel. */
60 dmxLogLevel dmxGetLogLevel(void)
61 {
62 return dmxCurrentLogLevel;
63 }
64
65 #ifdef DMX_LOG_STANDALONE
66 martin 1.1 /* When using this file as part of a stand-alone (i.e., non-X-Server
67 * program, then the ultimate output routines have to be defined. */
68
69 /** Provide an ErrorF function when used stand-alone. */
70 void ErrorF(const char *format, ...)
71 {
72 va_list args;
73
74 va_start(args, format);
75 vfprintf(stderr, format, args); /* RATS: We assume the format string
76 * is trusted, since it is always
77 * from a log message in our code. */
78 va_end(args);
79 }
80
81 /** Provide an VFatalError function when used stand-alone. */
82 static void VFatalError(const char *format, va_list args)
83 {
84 vfprintf(stderr, format, args); /* RATS: We assume the format string
85 * is trusted, since it is always
86 * from a log message in our code. */
87 martin 1.1 exit(1);
88 }
89
90 /** Provide an VErrorF function when used stand-alone. */
91 void VErrorF(const char *format, va_list args)
92 {
93 vfprintf(stderr, format, args); /* RATS: We assume the format string
94 * is trusted, since it is always
95 * from a log message in our code. */
96 }
97 #else
98 /** This function was removed between XFree86 4.3.0 and XFree86 4.4.0. */
99 extern void AbortServer(void);
100 static void VFatalError(const char *format, va_list args)
101 {
102 VErrorF(format, args);
103 ErrorF("\n");
104 #ifdef DDXOSFATALERROR
105 OsVendorFatalError();
106 #endif
107 AbortServer();
108 martin 1.1 /*NOTREACHED*/
109 }
110 #endif
111
112 /* Prints a consistent header for each line. */
113 static void dmxHeader(dmxLogLevel logLevel, DMXInputInfo *dmxInput,
114 DMXScreenInfo *dmxScreen)
115 {
116 const char *type = "??";
117
118 switch (logLevel) {
119 case dmxDebug: type = ".."; break;
120 case dmxInfo: type = "II"; break;
121 case dmxWarning: type = "**"; break;
122 case dmxError: type = "!!"; break;
123 case dmxFatal: type = "Fatal Error"; break;
124 }
125
126 if (dmxInput && dmxScreen) {
127 ErrorF("(%s) dmx[i%d/%s;o%d/%s]: ", type,
128 dmxInput->inputIdx, dmxInput->name,
129 martin 1.1 dmxScreen->index, dmxScreen->name);
130 } else if (dmxScreen) {
131 ErrorF("(%s) dmx[o%d/%s]: ", type,
132 dmxScreen->index, dmxScreen->name);
133 } else if (dmxInput) {
134 const char *pt = strchr(dmxInput->name, ',');
135 int len = (pt
136 ? (size_t)(pt-dmxInput->name)
137 : strlen(dmxInput->name));
138
139 ErrorF("(%s) dmx[i%d/%*.*s]: ", type,
140 dmxInput->inputIdx, len, len, dmxInput->name);
141 } else {
142 ErrorF("(%s) dmx: ", type);
143 }
144 }
145
146 /* Prints the error message with the appropriate low-level X output
147 * routine. */
148 static void dmxMessage(dmxLogLevel logLevel, const char *format, va_list args)
149 {
150 martin 1.1 if (logLevel == dmxFatal || logLevel >= dmxCurrentLogLevel) {
151 if (logLevel == dmxFatal) VFatalError(format, args);
152 else VErrorF(format, args);
153 }
154 }
155
156 /** Log the specified message at the specified \a logLevel. \a format
157 * can be a printf-like format expression. */
158 void dmxLog(dmxLogLevel logLevel, const char *format, ...)
159 {
160 va_list args;
161
162 dmxHeader(logLevel, NULL, NULL);
163 va_start(args, format);
164 dmxMessage(logLevel, format, args);
165 va_end(args);
166 }
167
168 /** Continue a log message without printing the message prefix. */
169 void dmxLogCont(dmxLogLevel logLevel, const char *format, ...)
170 {
171 martin 1.1 va_list args;
172
173 va_start(args, format);
174 dmxMessage(logLevel, format, args);
175 va_end(args);
176 }
177
178 #ifndef DMX_LOG_STANDALONE
179 /** Log an informational message (at level #dmxInfo) related to ouput.
180 * The message prefix will contain backend information from \a
181 * dmxScreen. */
182 void dmxLogOutput(DMXScreenInfo *dmxScreen, const char *format, ...)
183 {
184 va_list args;
185
186 dmxHeader(dmxInfo, NULL, dmxScreen);
187 va_start(args, format);
188 dmxMessage(dmxInfo, format, args);
189 va_end(args);
190 }
191
192 martin 1.1 /** Continue a message related to output without printing the message
193 * prefix. */
194 void dmxLogOutputCont(DMXScreenInfo *dmxScreen, const char *format, ...)
195 {
196 va_list args;
197
198 va_start(args, format);
199 dmxMessage(dmxInfo, format, args);
200 va_end(args);
201 }
202
203 /** Log a warning message (at level #dmxWarning) related to output.
204 * The message prefix will contain backend information from \a
205 * dmxScreen. */
206 void dmxLogOutputWarning(DMXScreenInfo *dmxScreen, const char *format, ...)
207 {
208 va_list args;
209
210 dmxHeader(dmxWarning, NULL, dmxScreen);
211 va_start(args, format);
212 dmxMessage(dmxWarning, format, args);
213 martin 1.1 va_end(args);
214 }
215
216 /** Log an informational message (at level #dmxInfo) related to input.
217 * The message prefix will contain information from \a dmxInput. */
218 void dmxLogInput(DMXInputInfo *dmxInput, const char *format, ...)
219 {
220 va_list args;
221
222 dmxHeader(dmxInfo, dmxInput, NULL);
223 va_start(args, format);
224 dmxMessage(dmxInfo, format, args);
225 va_end(args);
226 }
227
228 /** Continue a message related to input without printing the message
229 * prefix. */
230 void dmxLogInputCont(DMXInputInfo *dmxInput, const char *format, ...)
231 {
232 va_list args;
233
234 martin 1.1 va_start(args, format);
235 dmxMessage(dmxInfo, format, args);
236 va_end(args);
237 }
238
239 /** Print \a argc messages, each describing an element in \a argv. This
240 * is maingly for debugging purposes. */
241 void dmxLogArgs(dmxLogLevel logLevel, int argc, char **argv)
242 {
243 int i;
244 for (i = 0; i < argc; i++)
245 dmxLog(logLevel, " Arg[%d] = \"%s\"\n", i, argv[i]);
246 }
247
248 /** Print messages at level #dmxInfo describing the visuals in \a vi. */
249 void dmxLogVisual(DMXScreenInfo *dmxScreen, XVisualInfo *vi, int defaultVisual)
250 {
251 const char *class = "Unknown";
252
253 switch (vi->class) {
254 case StaticGray: class = "StaticGray "; break;
255 martin 1.1 case GrayScale: class = "GrayScale "; break;
256 case StaticColor: class = "StaticColor"; break;
257 case PseudoColor: class = "PseudoColor"; break;
258 case TrueColor: class = "TrueColor "; break;
259 case DirectColor: class = "DirectColor"; break;
260 }
261
262 if (dmxScreen) {
263 dmxLogOutput(dmxScreen,
264 "0x%02x %s %2db %db/rgb %3d 0x%04x 0x%04x 0x%04x%s\n",
265 vi->visualid, class, vi->depth, vi->bits_per_rgb,
266 vi->colormap_size,
267 vi->red_mask, vi->green_mask, vi->blue_mask,
268 defaultVisual ? " *" : "");
269 } else {
270 dmxLog(dmxInfo,
271 " 0x%02x %s %2db %db/rgb %3d 0x%04x 0x%04x 0x%04x%s\n",
272 vi->visualid, class, vi->depth, vi->bits_per_rgb,
273 vi->colormap_size,
274 vi->red_mask, vi->green_mask, vi->blue_mask,
275 defaultVisual ? " *" : "");
276 martin 1.1 }
277 }
278
279 #ifdef XINPUT
280 /** Translate a (normalized) XInput event \a type into a human-readable
281 * string. */
282 const char *dmxXInputEventName(int type)
283 {
284 switch (type) {
285 case XI_DeviceValuator: return "XI_DeviceValuator";
286 case XI_DeviceKeyPress: return "XI_DeviceKeyPress";
287 case XI_DeviceKeyRelease: return "XI_DeviceKeyRelease";
288 case XI_DeviceButtonPress: return "XI_DeviceButtonPress";
289 case XI_DeviceButtonRelease: return "XI_DeviceButtonRelease";
290 case XI_DeviceMotionNotify: return "XI_DeviceMotionNotify";
291 case XI_DeviceFocusIn: return "XI_DeviceFocusIn";
292 case XI_DeviceFocusOut: return "XI_DeviceFocusOut";
293 case XI_ProximityIn: return "XI_ProximityIn";
294 case XI_ProximityOut: return "XI_ProximityOut";
295 case XI_DeviceStateNotify: return "XI_DeviceStateNotify";
296 case XI_DeviceMappingNotify: return "XI_DeviceMappingNotify";
297 martin 1.1 case XI_ChangeDeviceNotify: return "XI_ChangeDeviceNotify";
298 case XI_DeviceKeystateNotify: return "XI_DeviceKeystateNotify";
299 case XI_DeviceButtonstateNotify: return "XI_DeviceButtonstateNotify";
300 default: return "unknown";
301 }
302 }
303
304 #endif
305 #endif
306
307 /** Translate an event \a type into a human-readable string. */
308 const char *dmxEventName(int type)
309 {
310 switch (type) {
311 case KeyPress: return "KeyPress";
312 case KeyRelease: return "KeyRelease";
313 case ButtonPress: return "ButtonPress";
314 case ButtonRelease: return "ButtonRelease";
315 case MotionNotify: return "MotionNotify";
316 case EnterNotify: return "EnterNotify";
317 case LeaveNotify: return "LeaveNotify";
318 martin 1.1 case FocusIn: return "FocusIn";
319 case FocusOut: return "FocusOut";
320 case KeymapNotify: return "KeymapNotify";
321 case Expose: return "Expose";
322 case GraphicsExpose: return "GraphicsExpose";
323 case NoExpose: return "NoExpose";
324 case VisibilityNotify: return "VisibilityNotify";
325 case CreateNotify: return "CreateNotify";
326 case DestroyNotify: return "DestroyNotify";
327 case UnmapNotify: return "UnmapNotify";
328 case MapNotify: return "MapNotify";
329 case MapRequest: return "MapRequest";
330 case ReparentNotify: return "ReparentNotify";
331 case ConfigureNotify: return "ConfigureNotify";
332 case ConfigureRequest: return "ConfigureRequest";
333 case GravityNotify: return "GravityNotify";
334 case ResizeRequest: return "ResizeRequest";
335 case CirculateNotify: return "CirculateNotify";
336 case CirculateRequest: return "CirculateRequest";
337 case PropertyNotify: return "PropertyNotify";
338 case SelectionClear: return "SelectionClear";
339 martin 1.1 case SelectionRequest: return "SelectionRequest";
340 case SelectionNotify: return "SelectionNotify";
341 case ColormapNotify: return "ColormapNotify";
342 case ClientMessage: return "ClientMessage";
343 case MappingNotify: return "MappingNotify";
344 default: return "<unknown>";
345 }
346 }
347
|