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