2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
25 * bootstrap -- fundamental service initiator and port server
26 * Mike DeMoney, NeXT, Inc.
27 * Copyright, 1990. All rights reserved.
29 * error_log.c -- implementation of logging routines
31 * Routines may be safely invoked from multiple threads
35 #import <mach/mach_error.h>
38 #import <sys/syslimits.h>
42 #import "bootstrap_internal.h"
45 static pthread_mutex_t errlog_lock
= PTHREAD_MUTEX_INITIALIZER
;
46 static boolean_t stderr_open
= FALSE
;
47 static boolean_t log_stopped
= FALSE
;
50 init_errlog(boolean_t daemon
)
56 nfds
= getdtablesize();
57 for (fd
= 3; fd
< nfds
; fd
++)
60 openlog((char *)program_name
, LOG_PID
|LOG_CONS
, LOG_DAEMON
);
61 setlogmask(LOG_UPTO(LOG_DEBUG
)); /* we'll do our own filtering */
78 static void do_log(const int level
, const char *format
, va_list ap
)
80 if (!log_stopped
&& (debugging
|| level
<= LOG_NOTICE
)) {
81 pthread_mutex_lock(&errlog_lock
);
83 fprintf(stderr
, "%s[%d]%s: ",
84 level
== LOG_ALERT
? " FATAL" : "",
85 getpid(), program_name
);
86 vfprintf(stderr
, format
, ap
);
87 fprintf(stderr
, "\n");
89 vsyslog(level
, format
, ap
);
91 pthread_mutex_unlock(&errlog_lock
);
95 void debug(const char *format
, ...)
100 va_start(ap
, format
);
101 do_log(LOG_DEBUG
, format
, ap
);
106 void info(const char *format
, ...)
110 va_start(ap
, format
);
111 do_log(LOG_INFO
, format
, ap
);
115 void notice(const char *format
, ...)
119 va_start(ap
, format
);
120 do_log(LOG_NOTICE
, format
, ap
);
124 void error(const char *format
, ...)
128 va_start(ap
, format
);
129 do_log(LOG_CRIT
, format
, ap
);
133 void kern_error(kern_return_t result
, const char *format
, ...)
138 sprintf(buf
, "%s: %s(%d)", format
, mach_error_string(result
), result
);
140 va_start(ap
, format
);
141 do_log(LOG_CRIT
, buf
, ap
);
145 void unix_error(const char *format
, ...)
150 sprintf(buf
, "%s: %s(%d)", format
, strerror(errno
), errno
);
152 va_start(ap
, format
);
153 do_log(LOG_CRIT
, buf
, ap
);
157 void parse_error(const char *token_string
, const char *format
, ...)
162 sprintf(buf
, "%s unexpected: %s", token_string
, format
);
164 va_start(ap
, format
);
165 do_log(LOG_CRIT
, buf
, ap
);
169 void fatal(const char *format
, ...)
173 va_start(ap
, format
);
174 do_log(LOG_ALERT
, format
, ap
);
179 void kern_fatal(kern_return_t result
, const char *format
, ...)
184 sprintf(buf
, "%s: %s(%d)", format
, mach_error_string(result
), result
);
186 va_start(ap
, format
);
187 do_log(LOG_ALERT
, buf
, ap
);
192 void unix_fatal(const char *format
, ...)
197 sprintf(buf
, "%s: %s(%d)", format
, strerror(errno
), errno
);
199 va_start(ap
, format
);
200 do_log(LOG_ALERT
, buf
, ap
);