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