]> git.saurik.com Git - apple/system_cmds.git/blob - mach_init.tproj/error_log.c
system_cmds-279.tar.gz
[apple/system_cmds.git] / mach_init.tproj / error_log.c
1 /*
2 * Copyright (c) 1999-2003 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 * bootstrap -- fundamental service initiator and port server
27 * Mike DeMoney, NeXT, Inc.
28 * Copyright, 1990. All rights reserved.
29 *
30 * error_log.c -- implementation of logging routines
31 *
32 * Routines may be safely invoked from multiple threads
33 */
34
35 #import <pthread.h>
36 #import <mach/mach_error.h>
37 #import <stdio.h>
38 #import <syslog.h>
39 #import <sys/syslimits.h>
40 #import <libc.h>
41 #import <errno.h>
42
43 #import "bootstrap_internal.h"
44 #import "error_log.h"
45
46 static pthread_mutex_t errlog_lock = PTHREAD_MUTEX_INITIALIZER;
47 static boolean_t stderr_open = FALSE;
48 static boolean_t log_stopped = FALSE;
49
50 void
51 init_errlog(boolean_t daemon)
52 {
53 int nfds, fd;
54
55 if (!daemon) {
56 stderr_open = TRUE;
57 nfds = getdtablesize();
58 for (fd = 3; fd < nfds; fd++)
59 close(fd);
60 } else {
61 openlog((char *)program_name, LOG_PID|LOG_CONS, LOG_DAEMON);
62 setlogmask(LOG_UPTO(LOG_DEBUG)); /* we'll do our own filtering */
63 }
64 }
65
66 void
67 stop_errlog(void)
68 {
69 log_stopped = TRUE;
70 }
71
72 void
73 close_errlog(void)
74 {
75 stop_errlog();
76 closelog();
77 }
78
79 static void do_log(const int level, const char *format, va_list ap)
80 {
81 if (!log_stopped && (debugging || level <= LOG_NOTICE)) {
82 pthread_mutex_lock(&errlog_lock);
83 if (stderr_open) {
84 fprintf(stderr, "%s[%d]%s: ",
85 level == LOG_ALERT ? " FATAL" : "",
86 getpid(), program_name);
87 vfprintf(stderr, format, ap);
88 fprintf(stderr, "\n");
89 } else {
90 vsyslog(level, format, ap);
91 }
92 pthread_mutex_unlock(&errlog_lock);
93 }
94 }
95
96 void debug(const char *format, ...)
97 {
98 if (debugging) {
99 va_list ap;
100
101 va_start(ap, format);
102 do_log(LOG_DEBUG, format, ap);
103 va_end(ap);
104 }
105 }
106
107 void info(const char *format, ...)
108 {
109 va_list ap;
110
111 va_start(ap, format);
112 do_log(LOG_INFO, format, ap);
113 va_end(ap);
114 }
115
116 void notice(const char *format, ...)
117 {
118 va_list ap;
119
120 va_start(ap, format);
121 do_log(LOG_NOTICE, format, ap);
122 va_end(ap);
123 }
124
125 void error(const char *format, ...)
126 {
127 va_list ap;
128
129 va_start(ap, format);
130 do_log(LOG_CRIT, format, ap);
131 va_end(ap);
132 }
133
134 void kern_error(kern_return_t result, const char *format, ...)
135 {
136 va_list ap;
137 char buf[1000];
138
139 sprintf(buf, "%s: %s(%d)", format, mach_error_string(result), result);
140
141 va_start(ap, format);
142 do_log(LOG_CRIT, buf, ap);
143 va_end(ap);
144 }
145
146 void unix_error(const char *format, ...)
147 {
148 va_list ap;
149 char buf[1000];
150
151 sprintf(buf, "%s: %s(%d)", format, strerror(errno), errno);
152
153 va_start(ap, format);
154 do_log(LOG_CRIT, buf, ap);
155 va_end(ap);
156 }
157
158 void parse_error(const char *token_string, const char *format, ...)
159 {
160 va_list ap;
161 char buf[1000];
162
163 sprintf(buf, "%s unexpected: %s", token_string, format);
164
165 va_start(ap, format);
166 do_log(LOG_CRIT, buf, ap);
167 va_end(ap);
168 }
169
170 void fatal(const char *format, ...)
171 {
172 va_list ap;
173
174 va_start(ap, format);
175 do_log(LOG_ALERT, format, ap);
176 va_end(ap);
177 exit(1);
178 }
179
180 void kern_fatal(kern_return_t result, const char *format, ...)
181 {
182 va_list ap;
183 char buf[1000];
184
185 sprintf(buf, "%s: %s(%d)", format, mach_error_string(result), result);
186
187 va_start(ap, format);
188 do_log(LOG_ALERT, buf, ap);
189 va_end(ap);
190 exit(1);
191 }
192
193 void unix_fatal(const char *format, ...)
194 {
195 va_list ap;
196 char buf[1000];
197
198 sprintf(buf, "%s: %s(%d)", format, strerror(errno), errno);
199
200 va_start(ap, format);
201 do_log(LOG_ALERT, buf, ap);
202 va_end(ap);
203 exit(1);
204 }