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