1 /* 2 3 Copyright 1988, 1998 The Open Group 4 5 Permission to use, copy, modify, distribute, and sell this software and its 6 documentation for any purpose is hereby granted without fee, provided that 7 the above copyright notice appear in all copies and that both that 8 copyright notice and this permission notice appear in supporting 9 documentation. 10 11 The above copyright notice and this permission notice shall be included 12 in all copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 OTHER DEALINGS IN THE SOFTWARE. 21 22 Except as contained in this notice, the name of The Open Group shall 23 not be used in advertising or otherwise to promote the sale, use or 24 other dealings in this Software without prior written authorization 25 from The Open Group. 26 27 */ 28 29 /* 30 * xdm - display manager daemon 31 * Author: Keith Packard, MIT X Consortium 32 * 33 * error.c 34 * 35 * Log display manager errors to a file as 36 * we generally do not have a terminal to talk to 37 */ 38 39 #include <stdio.h> 40 #include <stdarg.h> 41 42 #include "dm.h" 43 #include "dm_error.h" 44 45 /* This function does the actual log message writes. */ 46 static inline void _X_ATTRIBUTE_PRINTF(1,0) 47 LogVWrite(const char *fmt, va_list args) 48 { 49 char buf[1024]; 50 int len; 51 52 len = vsnprintf (buf, sizeof(buf), fmt, args); 53 if (len >= sizeof(buf)) { 54 len = sizeof(buf) - 1; 55 } 56 write(STDERR_FILENO, buf, len); 57 } 58 59 #define LogVarArgsWrite(fmt) \ 60 do { \ 61 va_list args; \ 62 va_start(args, fmt); \ 63 LogVWrite(fmt, args); \ 64 va_end(args); \ 65 } while(0) 66 67 #define LogHeader(type) \ 68 LogAppend("xdm %s (pid %ld): ", type, (long)getpid()) 69 70 /* Append more text to the log without a new header, right after 71 having called LogInfo or LogError */ 72 void 73 LogAppend(const char * fmt, ...) 74 { 75 LogVarArgsWrite(fmt); 76 } 77 78 void 79 LogInfo(const char * fmt, ...) 80 { 81 LogHeader("info"); 82 LogVarArgsWrite(fmt); 83 } 84 85 void 86 LogError (const char * fmt, ...) 87 { 88 LogHeader("error"); 89 LogVarArgsWrite(fmt); 90 } 91 92 void 93 LogPanic (const char * fmt, ...) 94 { 95 LogHeader("panic"); 96 LogVarArgsWrite(fmt); 97 _exit (1); 98 } 99 100 void 101 LogOutOfMem (const char * fmt, ...) 102 { 103 fprintf (stderr, "xdm: out of memory in routine "); 104 { 105 va_list args; 106 va_start(args, fmt); 107 vfprintf (stderr, fmt, args); 108 va_end(args); 109 } 110 fflush (stderr); 111 } 112 113 void 114 Debug (const char * fmt, ...) 115 { 116 if (debugLevel > 0) 117 { 118 LogVarArgsWrite(fmt); 119 } 120 } 121 122 void 123 InitErrorLog (void) 124 { 125 int i; 126 if (errorLogFile[0]) { 127 i = creat (errorLogFile, 0666); 128 if (i != -1) { 129 if (i != STDERR_FILENO) { 130 dup2 (i, STDERR_FILENO); 131 close (i); 132 } 133 } else 134 LogError ("Cannot open errorLogFile %s\n", errorLogFile); 135 } 136 } 137