]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-errors.m
objc4-267.tar.gz
[apple/objc4.git] / runtime / objc-errors.m
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * objc-errors.m
27 * Copyright 1988-2001, NeXT Software, Inc., Apple Computer, Inc.
28 */
29
30
31 #include <stdarg.h>
32 #include <unistd.h>
33 #include <syslog.h>
34 #include <sys/fcntl.h>
35
36 #import "objc-private.h"
37
38 static volatile void _objc_trap(void);
39
40
41 static int hasTerminal()
42 {
43 static char hasTerm = -1;
44
45 if (hasTerm == -1) {
46 int fd = open("/dev/tty", O_RDWR, 0);
47 if (fd >= 0) {
48 (void)close(fd);
49 hasTerm = 1;
50 } else
51 hasTerm = 0;
52 }
53 return hasTerm;
54 }
55
56 void _objc_syslog(const char *format, ...)
57 {
58 va_list ap;
59 char bigBuffer[4*1024];
60
61 va_start(ap, format);
62 vsnprintf(bigBuffer, sizeof(bigBuffer), format, ap);
63 va_end(ap);
64
65
66 if (hasTerminal()) {
67 fwrite(bigBuffer, sizeof(char), strlen(bigBuffer), stderr);
68 if (bigBuffer[strlen(bigBuffer)-1] != '\n')
69 fputc('\n', stderr);
70 } else {
71 syslog(LOG_ERR, "%s", bigBuffer);
72 }
73 }
74 /*
75 * this routine handles errors that involve an object (or class).
76 */
77 volatile void __objc_error(id rcv, const char *fmt, ...)
78 {
79 va_list vp;
80
81 va_start(vp,fmt);
82 (*_error)(rcv, fmt, vp);
83 va_end(vp);
84 _objc_error (rcv, fmt, vp); /* In case (*_error)() returns. */
85 }
86
87 /*
88 * this routine is never called directly...it is only called indirectly
89 * through "_error", which can be overriden by an application. It is
90 * not declared static because it needs to be referenced in
91 * "objc-globaldata.m" (this file organization simplifies the shlib
92 * maintenance problem...oh well). It is, however, a "private extern".
93 */
94 volatile void _objc_error(id self, const char *fmt, va_list ap)
95 {
96 char bigBuffer[4*1024];
97
98 vsnprintf (bigBuffer, sizeof(bigBuffer), fmt, ap);
99 _objc_syslog ("objc: %s: %s", object_getClassName (self), bigBuffer);
100
101 _objc_trap();
102 }
103
104 /*
105 * this routine handles severe runtime errors...like not being able
106 * to read the mach headers, allocate space, etc...very uncommon.
107 */
108 volatile void _objc_fatal(const char *fmt, ...)
109 {
110 va_list ap;
111 char bigBuffer[4*1024];
112
113 va_start (ap,fmt);
114 vsnprintf (bigBuffer, sizeof(bigBuffer), fmt, ap);
115 _objc_syslog ("objc: %s", bigBuffer);
116 va_end (ap);
117
118 _objc_trap();
119 }
120
121 /*
122 * this routine handles soft runtime errors...like not being able
123 * add a category to a class (because it wasn't linked in).
124 */
125 void _objc_inform(const char *fmt, ...)
126 {
127 va_list ap;
128 char bigBuffer[4*1024];
129
130 va_start (ap,fmt);
131 vsnprintf (bigBuffer, sizeof(bigBuffer), fmt, ap);
132 _objc_syslog ("objc: %s", bigBuffer);
133 va_end (ap);
134 }
135
136
137 /* Kill the process in a way that generates a crash log.
138 * This is better than calling exit(). */
139 static volatile void _objc_trap(void)
140 {
141 #if defined(__ppc__) || defined(ppc)
142 asm("trap");
143 #elif defined(__i386__) || defined(i386)
144 asm("int3");
145 #else
146 #warning _objc_trap not specified for this architecture; using _exit instead
147 _exit(1);
148 #endif
149 }