2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
26 * bootstrap -- fundamental service initiator and port server
27 * Mike DeMoney, NeXT, Inc.
28 * Copyright, 1990. All rights reserved.
30 * error_log.c -- implementation of logging routines
32 * Routines may be safely invoked from multiple threads
36 #import <mach/mach_error.h>
39 #import <sys/syslimits.h>
43 #import "bootstrap_internal.h"
46 static pthread_mutex_t errlog_lock
= PTHREAD_MUTEX_INITIALIZER
;
47 static boolean_t stderr_open
= FALSE
;
48 static boolean_t log_stopped
= FALSE
;
51 init_errlog(boolean_t daemon
)
57 nfds
= getdtablesize();
58 for (fd
= 3; fd
< nfds
; fd
++)
61 openlog((char *)program_name
, LOG_PID
|LOG_CONS
, LOG_DAEMON
);
62 setlogmask(LOG_UPTO(LOG_DEBUG
)); /* we'll do our own filtering */
79 static void do_log(const int level
, const char *format
, va_list ap
)
81 if (!log_stopped
&& (debugging
|| level
<= LOG_NOTICE
)) {
82 pthread_mutex_lock(&errlog_lock
);
84 fprintf(stderr
, "%s[%d]%s: ",
85 level
== LOG_ALERT
? " FATAL" : "",
86 getpid(), program_name
);
87 vfprintf(stderr
, format
, ap
);
88 fprintf(stderr
, "\n");
90 vsyslog(level
, format
, ap
);
92 pthread_mutex_unlock(&errlog_lock
);
96 void debug(const char *format
, ...)
101 va_start(ap
, format
);
102 do_log(LOG_DEBUG
, format
, ap
);
107 void info(const char *format
, ...)
111 va_start(ap
, format
);
112 do_log(LOG_INFO
, format
, ap
);
116 void notice(const char *format
, ...)
120 va_start(ap
, format
);
121 do_log(LOG_NOTICE
, format
, ap
);
125 void error(const char *format
, ...)
129 va_start(ap
, format
);
130 do_log(LOG_CRIT
, format
, ap
);
134 void kern_error(kern_return_t result
, const char *format
, ...)
139 sprintf(buf
, "%s: %s(%d)", format
, mach_error_string(result
), result
);
141 va_start(ap
, format
);
142 do_log(LOG_CRIT
, buf
, ap
);
146 void unix_error(const char *format
, ...)
151 sprintf(buf
, "%s: %s(%d)", format
, strerror(errno
), errno
);
153 va_start(ap
, format
);
154 do_log(LOG_CRIT
, buf
, ap
);
158 void parse_error(const char *token_string
, const char *format
, ...)
163 sprintf(buf
, "%s unexpected: %s", token_string
, format
);
165 va_start(ap
, format
);
166 do_log(LOG_CRIT
, buf
, ap
);
170 void fatal(const char *format
, ...)
174 va_start(ap
, format
);
175 do_log(LOG_ALERT
, format
, ap
);
180 void kern_fatal(kern_return_t result
, const char *format
, ...)
185 sprintf(buf
, "%s: %s(%d)", format
, mach_error_string(result
), result
);
187 va_start(ap
, format
);
188 do_log(LOG_ALERT
, buf
, ap
);
193 void unix_fatal(const char *format
, ...)
198 sprintf(buf
, "%s: %s(%d)", format
, strerror(errno
), errno
);
200 va_start(ap
, format
);
201 do_log(LOG_ALERT
, buf
, ap
);