]> git.saurik.com Git - apple/objc4.git/blobdiff - runtime/objc-errors.m
objc4-237.tar.gz
[apple/objc4.git] / runtime / objc-errors.m
index 0718b917155d6a013ad8dd57d5e47d0db2bd7770..4a161116a9a307d137a587723c80aa8016272a0b 100644 (file)
@@ -3,60 +3,74 @@
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
- * Reserved.  This file contains Original Code and/or Modifications of
- * Original Code as defined in and that are subject to the Apple Public
- * Source License Version 1.1 (the "License").  You may not use this file
- * except in compliance with the License.  Please obtain a copy of the
- * License at http://www.apple.com/publicsource and read it before using
- * this file.
+ * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
+ * 
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this
+ * file.
  * 
  * The Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
 /*
  *     objc-errors.m
- *     Copyright 1988-1996, NeXT Software, Inc.
+ *     Copyright 1988-2001, NeXT Software, Inc., Apple Computer, Inc.
  */
 
-/*
-       NXLogObjcError was snarfed from "logErrorInc.c" in the kit.
-  
-       Contains code for writing error messages to stderr or syslog.
-  
-       This code is included in errors.m in the kit, and in pbs.c
-       so pbs can use it also.
-*/
-
-#if defined(WIN32)
-    #import <winnt-pdo.h>
-    #import <windows.h>
-    #import <sys/types.h>
-    #import <sys/stat.h>
-    #import <io.h>
-    #define syslog(a, b, c)    fprintf(stderr, b, c)
-#else 
-    #import <syslog.h>
-#endif
-
-    #if defined(NeXT_PDO)
-        #if !defined(WIN32)
-            #include   <syslog.h>      // major head banging in attempt to find syslog
-            #import    <stdarg.h>
-            #include   <unistd.h>      // close
-        #endif
-        #import        <fcntl.h>       // file open flags
-    #endif
+
+#include <stdarg.h>
+#include <unistd.h>
+#include <syslog.h>
+#include <sys/fcntl.h>
 
 #import "objc-private.h"
 
+static volatile void _objc_trap(void);
+
+
+static int hasTerminal()
+{
+    static char hasTerm = -1;
+
+    if (hasTerm == -1) {
+       int fd = open("/dev/tty", O_RDWR, 0);
+       if (fd >= 0) {
+           (void)close(fd);
+           hasTerm = 1;
+       } else
+           hasTerm = 0;
+    }
+    return hasTerm;
+}
+
+void _objc_syslog(const char *format, ...)
+{
+    va_list ap;
+    char bigBuffer[4*1024];
+
+    va_start(ap, format);
+    vsnprintf(bigBuffer, sizeof(bigBuffer), format, ap);
+    va_end(ap);
+
+
+    if (hasTerminal()) {
+       fwrite(bigBuffer, sizeof(char), strlen(bigBuffer), stderr);
+       if (bigBuffer[strlen(bigBuffer)-1] != '\n')
+           fputc('\n', stderr);
+    } else {
+       syslog(LOG_ERR, "%s", bigBuffer);
+    }
+}
 /*     
  *     this routine handles errors that involve an object (or class).
  */
@@ -81,28 +95,27 @@ volatile void _objc_error(id self, const char *fmt, va_list ap)
 { 
     char bigBuffer[4*1024];
 
-    vsprintf (bigBuffer, fmt, ap);
-    _NXLogError ("objc: %s: %s", object_getClassName (self), bigBuffer);
+    vsnprintf (bigBuffer, sizeof(bigBuffer), fmt, ap);
+    _objc_syslog ("objc: %s: %s", object_getClassName (self), bigBuffer);
 
-#if defined(WIN32)
-    RaiseException(0xdead, EXCEPTION_NONCONTINUABLE, 0, NULL);
-#else
-    abort();           /* generates a core file */
-#endif
+    _objc_trap();
 }
 
 /*     
  *     this routine handles severe runtime errors...like not being able
  *     to read the mach headers, allocate space, etc...very uncommon.
  */
-volatile void _objc_fatal(const char *msg)
+volatile void _objc_fatal(const char *fmt, ...)
 {
-    _NXLogError("objc: %s\n", msg);
-#if defined(WIN32)
-    RaiseException(0xdead, EXCEPTION_NONCONTINUABLE, 0, NULL);
-#else
-    exit(1);
-#endif
+    va_list ap; 
+    char bigBuffer[4*1024];
+
+    va_start (ap,fmt); 
+    vsnprintf (bigBuffer, sizeof(bigBuffer), fmt, ap);
+    _objc_syslog ("objc: %s", bigBuffer);
+    va_end (ap);
+
+    _objc_trap();
 }
 
 /*
@@ -115,8 +128,22 @@ void _objc_inform(const char *fmt, ...)
     char bigBuffer[4*1024];
 
     va_start (ap,fmt); 
-    vsprintf (bigBuffer, fmt, ap);
-    _NXLogError ("objc: %s", bigBuffer);
+    vsnprintf (bigBuffer, sizeof(bigBuffer), fmt, ap);
+    _objc_syslog ("objc: %s", bigBuffer);
     va_end (ap);
 }
 
+
+/* Kill the process in a way that generates a crash log. 
+ * This is better than calling exit(). */
+static volatile void _objc_trap(void) 
+{
+#if defined(__ppc__) || defined(ppc)
+    asm("trap");
+#elif defined(__i386__) || defined(i386)
+    asm("int3");
+#else
+#warning _objc_trap not specified for this architecture; using _exit instead
+    _exit(1);
+#endif   
+}